Class: Releaf::Content::Route
- Inherits:
-
Object
- Object
- Releaf::Content::Route
- Defined in:
- lib/releaf/content/route.rb
Instance Attribute Summary collapse
-
#default_controller ⇒ Object
Returns the value of attribute default_controller.
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#node ⇒ Object
Returns the value of attribute node.
-
#node_class ⇒ Object
Returns the value of attribute node_class.
-
#node_id ⇒ Object
Returns the value of attribute node_id.
-
#path ⇒ Object
Returns the value of attribute path.
-
#site ⇒ Object
Returns the value of attribute site.
Class Method Summary collapse
- .default_controller(node_content_class) ⇒ Object
-
.for(node_class, node_content_class, default_controller) ⇒ Array
Return routes for given class that implement ActsAsNode.
Instance Method Summary collapse
- #controller_and_action_for(method_or_path, options) ⇒ Object
- #name(route_options) ⇒ Object
- #options_for(method_or_path, options) ⇒ Object
-
#params(method_or_path, options = {}) ⇒ Hash
Return node route params which can be used in Rails route options.
- #path_for(method_or_path, options) ⇒ Object
Instance Attribute Details
#default_controller ⇒ Object
Returns the value of attribute default_controller.
3 4 5 |
# File 'lib/releaf/content/route.rb', line 3 def default_controller @default_controller end |
#locale ⇒ Object
Returns the value of attribute locale.
3 4 5 |
# File 'lib/releaf/content/route.rb', line 3 def locale @locale end |
#node ⇒ Object
Returns the value of attribute node.
3 4 5 |
# File 'lib/releaf/content/route.rb', line 3 def node @node end |
#node_class ⇒ Object
Returns the value of attribute node_class.
3 4 5 |
# File 'lib/releaf/content/route.rb', line 3 def node_class @node_class end |
#node_id ⇒ Object
Returns the value of attribute node_id.
3 4 5 |
# File 'lib/releaf/content/route.rb', line 3 def node_id @node_id end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/releaf/content/route.rb', line 3 def path @path end |
#site ⇒ Object
Returns the value of attribute site.
3 4 5 |
# File 'lib/releaf/content/route.rb', line 3 def site @site end |
Class Method Details
.default_controller(node_content_class) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/releaf/content/route.rb', line 5 def self.default_controller(node_content_class) if node_content_class <= ActionController::Base node_content_class.name.underscore.sub(/_controller$/, '') else node_content_class.name.pluralize.underscore end end |
.for(node_class, node_content_class, default_controller) ⇒ Array
Return routes for given class that implement ActsAsNode
58 59 60 61 62 63 64 65 66 |
# File 'lib/releaf/content/route.rb', line 58 def self.for(node_class, node_content_class, default_controller) node_class = node_class.constantize if node_class.is_a? String Releaf::Content::BuildRouteObjects.call( node_class: node_class, node_content_class: node_content_class, default_controller: default_controller) rescue ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid [] end |
Instance Method Details
#controller_and_action_for(method_or_path, options) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/releaf/content/route.rb', line 78 def controller_and_action_for(method_or_path, ) if method_or_path.start_with?('#') "#{default_controller}#{method_or_path}" elsif method_or_path.include?('#') method_or_path elsif [:to].try!(:start_with?, '#') "#{default_controller}#{[:to]}" elsif [:to].try!(:include?, '#') [:to] else "#{default_controller}##{method_or_path}" end end |
#name(route_options) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/releaf/content/route.rb', line 40 def name( ) return nil unless [:as].present? # prepend :as with locale and site to prevent duplicate route names name_parts = [ [:as] ] name_parts.unshift( [:locale] ) if [:locale].present? name_parts.unshift( [:site] ) if [:site].present? name_parts.join('_') end |
#options_for(method_or_path, options) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/releaf/content/route.rb', line 26 def ( method_or_path, ) = .merge({ to: controller_and_action_for(method_or_path, ), node_class: node_class.name, node_id: node_id.to_s, locale: locale }) [:site] = site if site.present? [:as] = name( ) end |
#params(method_or_path, options = {}) ⇒ Hash
Return node route params which can be used in Rails route options
18 19 20 21 22 23 24 |
# File 'lib/releaf/content/route.rb', line 18 def params(method_or_path, = {}) method_or_path = method_or_path.to_s [ path_for(method_or_path, ), (method_or_path, ) ] end |
#path_for(method_or_path, options) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/releaf/content/route.rb', line 68 def path_for(method_or_path, ) if method_or_path.include?('#') path elsif .key?(:to) "#{path}/#{method_or_path}" else path end end |