routable.rb 3.6 KB
Newer Older
1 2
# frozen_string_literal: true

3
# Store object full path in separate table for easy lookup and uniq validation
4
# Object must have name and path db fields and respond to parent and parent_changed? methods.
5 6 7 8
module Routable
  extend ActiveSupport::Concern

  included do
9 10 11
    # Remove `inverse_of: source` when upgraded to rails 5.2
    # See https://github.com/rails/rails/pull/28808
    has_one :route, as: :source, autosave: true, dependent: :destroy, inverse_of: :source # rubocop:disable Cop/ActiveRecordDependent
12
    has_many :redirect_routes, as: :source, autosave: true, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
13

14
    validates :route, presence: true
15

16 17
    scope :with_route, -> { includes(:route) }

18 19
    after_validation :set_path_errors

20 21 22 23 24
    before_validation do
      if full_path_changed? || full_name_changed?
        prepare_route
      end
    end
25 26 27 28 29 30 31 32 33 34
  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.
35
    def find_by_full_path(path, follow_redirects: false)
36 37
      increment_counter(:routable_find_by_full_path, 'Number of calls to Routable.find_by_full_path')

38 39 40 41 42 43 44 45 46
      if Feature.enabled?(:routable_two_step_lookup)
        # Case sensitive match first (it's cheaper and the usual case)
        # If we didn't have an exact match, we perform a case insensitive search
        found = joins(:route).find_by(routes: { path: path }) || where_full_path_in([path]).take
      else
        order_sql = Arel.sql("(CASE WHEN routes.path = #{connection.quote(path)} THEN 0 ELSE 1 END)")
        found = where_full_path_in([path]).reorder(order_sql).take
      end

47
      return found if found
48

49
      if follow_redirects
N
Nick Thomas 已提交
50
        joins(:redirect_routes).find_by("LOWER(redirect_routes.path) = LOWER(?)", path)
51
      end
52 53 54 55 56 57
    end

    # Builds a relation to find multiple objects by their full paths.
    #
    # Usage:
    #
58
    #     Klass.where_full_path_in(%w{gitlab-org/gitlab-ce gitlab-org/gitlab-ee})
59 60
    #
    # Returns an ActiveRecord::Relation.
61
    def where_full_path_in(paths)
N
Nick Thomas 已提交
62
      return none if paths.empty?
63

64
      increment_counter(:routable_where_full_path_in, 'Number of calls to Routable.where_full_path_in')
65

N
Nick Thomas 已提交
66 67
      wheres = paths.map do |path|
        "(LOWER(routes.path) = LOWER(#{connection.quote(path)}))"
68 69
      end

N
Nick Thomas 已提交
70
      joins(:route).where(wheres.join(' OR '))
71
    end
72

73 74 75
    # Temporary instrumentation of method calls
    def increment_counter(counter, description)
      @counters[counter] ||= Gitlab::Metrics.counter(counter, description)
76

77
      @counters[counter].increment
78 79 80
    rescue
      # ignore the error
    end
81 82
  end

83
  def full_name
84
    route&.name || build_full_name
85 86 87
  end

  def full_path
88
    route&.path || build_full_path
89 90
  end

Z
Zeger-Jan van de Weg 已提交
91 92 93 94
  def full_path_components
    full_path.split('/')
  end

95 96 97 98 99 100 101 102
  def build_full_path
    if parent && path
      parent.full_path + '/' + path
    else
      path
    end
  end

103 104 105 106 107
  # Group would override this to check from association
  def owned_by?(user)
    owner == user
  end

108 109
  private

110 111 112 113 114
  def set_path_errors
    route_path_errors = self.errors.delete(:"route.path")
    self.errors[:path].concat(route_path_errors) if route_path_errors
  end

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  def full_name_changed?
    name_changed? || parent_changed?
  end

  def full_path_changed?
    path_changed? || parent_changed?
  end

  def build_full_name
    if parent && name
      parent.human_name + ' / ' + name
    else
      name
    end
  end

  def prepare_route
132
    route || build_route(source: self)
133 134
    route.path = build_full_path
    route.name = build_full_name
135 136
  end
end