Module: Routable
- Extended by:
- ActiveSupport::Concern
- Includes:
- CaseSensitivity
- 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
-
.find_by_full_path(path, follow_redirects: false, route_scope: nil) ⇒ Object
Finds a Routable object by its full path, without knowing the class.
Instance Method Summary collapse
- #build_full_path ⇒ Object
- #full_name ⇒ Object
- #full_path ⇒ Object
- #full_path_components ⇒ Object
-
#owned_by?(user) ⇒ Boolean
Group would override this to check from association.
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_path ⇒ Object
127 128 129 130 131 132 133 |
# File 'app/models/concerns/routable.rb', line 127 def build_full_path if parent && path "#{parent.full_path}/#{path}" else path end end |
#full_name ⇒ Object
115 116 117 |
# File 'app/models/concerns/routable.rb', line 115 def full_name full_attribute(:name) end |
#full_path ⇒ Object
119 120 121 |
# File 'app/models/concerns/routable.rb', line 119 def full_path full_attribute(:path) end |
#full_path_components ⇒ Object
123 124 125 |
# File 'app/models/concerns/routable.rb', line 123 def full_path_components full_path.split('/') end |
#owned_by?(user) ⇒ Boolean
Group would override this to check from association
136 137 138 |
# File 'app/models/concerns/routable.rb', line 136 def owned_by?(user) owner == user end |