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.
-
#parent_loaded? ⇒ Boolean
Overriden in the Project model parent_id condition prevents issues with parent reassignment.
- #route_loaded? ⇒ Boolean
Class Method Details
.find_by_full_path(path, follow_redirects: false, route_scope: nil) ⇒ Object
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
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_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
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
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
125 126 127 |
# File 'app/models/concerns/routable.rb', line 125 def parent_loaded? association(:parent).loaded? end |
#route_loaded? ⇒ Boolean
129 130 131 |
# File 'app/models/concerns/routable.rb', line 129 def route_loaded? association(:route).loaded? end |