routable.rb 5.04 KB
Newer Older
1
# Store object full path in separate table for easy lookup and uniq validation
2
# Object must have name and path db fields and respond to parent and parent_changed? methods.
3 4 5 6
module Routable
  extend ActiveSupport::Concern

  included do
7 8
    has_one :route, as: :source, autosave: true, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
    has_many :redirect_routes, as: :source, autosave: true, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
9

10
    validates :route, presence: true
11

12 13
    scope :with_route, -> { includes(:route) }

14 15
    after_validation :set_path_errors

16 17 18 19 20
    before_validation do
      if full_path_changed? || full_name_changed?
        prepare_route
      end
    end
21 22 23 24 25 26 27 28 29 30
  end

  class_methods do
    # Finds a single object by full path match in routes table.
    #
    # Usage:
    #
    #     Klass.find_by_full_path('gitlab-org/gitlab-ce')
    #
    # Returns a single object, or nil.
31
    def find_by_full_path(path, follow_redirects: false)
32 33 34 35
      # On MySQL we want to ensure the ORDER BY uses a case-sensitive match so
      # any literal matches come first, for this we have to use "BINARY".
      # Without this there's still no guarantee in what order MySQL will return
      # rows.
36 37 38 39 40 41 42 43
      #
      # Why do we do this?
      #
      # Even though we have Rails validation on Route for unique paths
      # (case-insensitive), there are old projects in our DB (and possibly
      # clients' DBs) that have the same path with different cases.
      # See https://gitlab.com/gitlab-org/gitlab-ce/issues/18603. Also note that
      # our unique index is case-sensitive in Postgres.
44 45
      binary = Gitlab::Database.mysql? ? 'BINARY' : ''
      order_sql = "(CASE WHEN #{binary} routes.path = #{connection.quote(path)} THEN 0 ELSE 1 END)"
46 47
      found = where_full_path_in([path]).reorder(order_sql).take
      return found if found
48

49 50 51 52
      if follow_redirects
        if Gitlab::Database.postgresql?
          joins(:redirect_routes).find_by("LOWER(redirect_routes.path) = LOWER(?)", path)
        else
53
          joins(:redirect_routes).find_by(redirect_routes: { path: path })
54 55
        end
      end
56 57 58 59 60 61
    end

    # Builds a relation to find multiple objects by their full paths.
    #
    # Usage:
    #
62
    #     Klass.where_full_path_in(%w{gitlab-org/gitlab-ce gitlab-org/gitlab-ee})
63 64
    #
    # Returns an ActiveRecord::Relation.
65
    def where_full_path_in(paths)
66 67 68 69 70 71
      wheres = []
      cast_lower = Gitlab::Database.postgresql?

      paths.each do |path|
        path = connection.quote(path)

72 73 74 75 76 77
        where =
          if cast_lower
            "(LOWER(routes.path) = LOWER(#{path}))"
          else
            "(routes.path = #{path})"
          end
78 79 80 81 82 83 84 85 86 87 88 89

        wheres << where
      end

      if wheres.empty?
        none
      else
        joins(:route).where(wheres.join(' OR '))
      end
    end
  end

90 91
  def full_name
    if route && route.name.present?
92
      @full_name ||= route.name # rubocop:disable Gitlab/ModuleWithInstanceVariables
93 94 95 96 97 98 99
    else
      update_route if persisted?

      build_full_name
    end
  end

100 101 102 103
  # Every time `project.namespace.becomes(Namespace)` is called for polymorphic_path,
  # a new instance is instantiated, and we end up duplicating the same query to retrieve
  # the route. Caching this per request ensures that even if we have multiple instances,
  # we will not have to duplicate work, avoiding N+1 queries in some cases.
104
  def full_path
105
    return uncached_full_path unless RequestStore.active? && persisted?
106

107 108 109
    RequestStore[full_path_key] ||= uncached_full_path
  end

110 111 112 113
  def full_path_components
    full_path.split('/')
  end

114 115
  def expires_full_path_cache
    RequestStore.delete(full_path_key) if RequestStore.active?
116
    @full_path = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
117 118
  end

119 120 121 122 123 124 125 126
  def build_full_path
    if parent && path
      parent.full_path + '/' + path
    else
      path
    end
  end

127 128 129 130 131
  # Group would override this to check from association
  def owned_by?(user)
    owner == user
  end

132 133
  private

134 135 136 137 138
  def set_path_errors
    route_path_errors = self.errors.delete(:"route.path")
    self.errors[:path].concat(route_path_errors) if route_path_errors
  end

139
  def uncached_full_path
140
    if route && route.path.present?
141
      @full_path ||= route.path # rubocop:disable Gitlab/ModuleWithInstanceVariables
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    else
      update_route if persisted?

      build_full_path
    end
  end

  def full_name_changed?
    name_changed? || parent_changed?
  end

  def full_path_changed?
    path_changed? || parent_changed?
  end

157 158 159 160
  def full_path_key
    @full_path_key ||= "routable/full_path/#{self.class.name}/#{self.id}"
  end

161 162 163 164 165 166 167 168 169
  def build_full_name
    if parent && name
      parent.human_name + ' / ' + name
    else
      name
    end
  end

  def update_route
170 171
    return if Gitlab::Database.read_only?

172 173 174 175 176
    prepare_route
    route.save
  end

  def prepare_route
177
    route || build_route(source: self)
178 179
    route.path = build_full_path
    route.name = build_full_name
180 181
    @full_path = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
    @full_name = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
182 183
  end
end