Module: Routable

Overview

Store object full path in separate table for easy lookup and uniq validation Object must have name and path db fields and respond to parent and parent_changed? methods.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_full_path(path, follow_redirects: false, route_scope: Route, redirect_route_scope: RedirectRoute, optimize_routable: Routable.optimize_routable_enabled?) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/concerns/routable.rb', line 20

def self.find_by_full_path(
  path,
  follow_redirects: false,
  route_scope: Route,
  redirect_route_scope: RedirectRoute,
  optimize_routable: Routable.optimize_routable_enabled?
)

  return unless path.present?

  # Convert path to string to prevent DB error: function lower(integer) does not exist
  path = path.to_s

  # 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
  #
  # We need to qualify the columns with the table name, to support both direct lookups on
  # Route/RedirectRoute, and scoped lookups through the Routable classes.
  if optimize_routable
    path_condition = { path: path }

    source_type_condition = if route_scope == Route
                              {}
                            else
                              { source_type: route_scope.klass.base_class }
                            end

    route =
      Route.where(source_type_condition).find_by(path_condition) ||
      Route.where(source_type_condition).iwhere(path_condition).take

    if follow_redirects
      route ||= RedirectRoute.where(source_type_condition).iwhere(path_condition).take
    end

    return unless route
    return route.source if route_scope == Route

    route_scope.find_by(id: route.source_id)
  else
    Gitlab::Database.allow_cross_joins_across_databases(url:
      "https://gitlab.com/gitlab-org/gitlab/-/issues/420046") do
      route =
        route_scope.find_by(routes: { path: path }) ||
        route_scope.iwhere(Route.arel_table[:path] => path).take

      if follow_redirects
        route ||= redirect_route_scope.iwhere(RedirectRoute.arel_table[:path] => path).take
      end

      next unless route

      route.is_a?(Routable) ? route : route.source
    end
  end
end

.optimize_routable_enabled?Boolean

rubocop:enable Metrics/PerceivedComplexity rubocop:enable Metrics/CyclomaticComplexity

Returns:

  • (Boolean)


79
80
81
# File 'app/models/concerns/routable.rb', line 79

def self.optimize_routable_enabled?
  Feature.enabled?(:optimize_find_routable, Feature.current_request)
end

Instance Method Details

#build_full_pathObject



178
179
180
181
182
183
184
# File 'app/models/concerns/routable.rb', line 178

def build_full_path
  if parent && path
    parent.full_path + '/' + path
  else
    path
  end
end

#full_nameObject



156
157
158
# File 'app/models/concerns/routable.rb', line 156

def full_name
  full_attribute(:name)
end

#full_pathObject



160
161
162
# File 'app/models/concerns/routable.rb', line 160

def full_path
  full_attribute(:path)
end

#full_path_componentsObject



174
175
176
# File 'app/models/concerns/routable.rb', line 174

def full_path_components
  full_path.split('/')
end

#owned_by?(user) ⇒ Boolean

Group would override this to check from association

Returns:

  • (Boolean)


187
188
189
# File 'app/models/concerns/routable.rb', line 187

def owned_by?(user)
  owner == user
end

#parent_loaded?Boolean

Overriden in the Project model parent_id condition prevents issues with parent reassignment

Returns:

  • (Boolean)


166
167
168
# File 'app/models/concerns/routable.rb', line 166

def parent_loaded?
  association(:parent).loaded?
end

#route_loaded?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'app/models/concerns/routable.rb', line 170

def route_loaded?
  association(:route).loaded?
end