Module: Routable

Extended by:
ActiveSupport::Concern
Includes:
CaseSensitivity
Included in:
Namespace, Project
Defined in:
app/models/concerns/routable.rb

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: nil) ⇒ Object

Finds a Routable object by its full path, without knowing the class.

Usage:

Routable.find_by_full_path('groupname')             # -> Group
Routable.find_by_full_path('groupname/projectname') # -> Project

Returns a single object, or nil.



18
19
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
# File 'app/models/concerns/routable.rb', line 18

def self.find_by_full_path(path, follow_redirects: false, route_scope: nil)
  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.
  path_condition = { path: path }

  source_type_condition = route_scope ? { source_type: route_scope.klass.base_class } : {}

  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 unless route_scope

  route_scope.find_by(id: route.source_id)
end

Instance Method Details

#build_full_pathObject



137
138
139
140
141
142
143
# File 'app/models/concerns/routable.rb', line 137

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

#full_nameObject



115
116
117
# File 'app/models/concerns/routable.rb', line 115

def full_name
  full_attribute(:name)
end

#full_pathObject



119
120
121
# File 'app/models/concerns/routable.rb', line 119

def full_path
  full_attribute(:path)
end

#full_path_componentsObject



133
134
135
# File 'app/models/concerns/routable.rb', line 133

def full_path_components
  full_path.split('/')
end

#owned_by?(user) ⇒ Boolean

Group would override this to check from association

Returns:

  • (Boolean)


146
147
148
# File 'app/models/concerns/routable.rb', line 146

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)


125
126
127
# File 'app/models/concerns/routable.rb', line 125

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

#route_loaded?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'app/models/concerns/routable.rb', line 129

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