Class: Releaf::Content::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/releaf/content/route.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_controllerObject

Returns the value of attribute default_controller.



3
4
5
# File 'lib/releaf/content/route.rb', line 3

def default_controller
  @default_controller
end

#localeObject

Returns the value of attribute locale.



3
4
5
# File 'lib/releaf/content/route.rb', line 3

def locale
  @locale
end

#nodeObject

Returns the value of attribute node.



3
4
5
# File 'lib/releaf/content/route.rb', line 3

def node
  @node
end

#node_classObject

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_idObject

Returns the value of attribute node_id.



3
4
5
# File 'lib/releaf/content/route.rb', line 3

def node_id
  @node_id
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/releaf/content/route.rb', line 3

def path
  @path
end

#siteObject

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

Parameters:

  • node_class (Class)

    class name to load related nodes

  • node_content_class (Class)

    class name to load related nodes

  • default_controller (String)

Returns:

  • (Array)

    array of Content::Route objects



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, options)
  if method_or_path.start_with?('#')
    "#{default_controller}#{method_or_path}"
  elsif method_or_path.include?('#')
    method_or_path
  elsif options[:to].try!(:start_with?, '#')
    "#{default_controller}#{options[:to]}"
  elsif options[:to].try!(:include?, '#')
    options[: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( route_options )
  return nil unless route_options[:as].present?

  # prepend :as with locale and site to prevent duplicate route names
  name_parts = [ route_options[:as] ]

  name_parts.unshift( route_options[:locale] ) if route_options[:locale].present?
  name_parts.unshift( route_options[:site] ) if route_options[: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 options_for( method_or_path, options )
  route_options = options.merge({
    to:         controller_and_action_for(method_or_path, options),
    node_class: node_class.name,
    node_id:    node_id.to_s,
    locale:     locale
  })

  route_options[:site] = site if site.present?
  route_options[:as] = name( route_options )

  route_options
end

#params(method_or_path, options = {}) ⇒ Hash

Return node route params which can be used in Rails route options

Parameters:

  • method_or_path (String)

    string with action and controller for route (Ex. home#index)

  • options (Hash) (defaults to: {})

    options to merge with internally built params. Passed params overrides route params.

Returns:

  • (Hash)

    route options. Will return at least node “node_id” and “locale” keys.



18
19
20
21
22
23
24
# File 'lib/releaf/content/route.rb', line 18

def params(method_or_path, options = {})
  method_or_path = method_or_path.to_s
  [
    path_for(method_or_path, options),
    options_for(method_or_path, options)
  ]
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, options)
  if method_or_path.include?('#')
    path
  elsif options.key?(:to)
    "#{path}/#{method_or_path}"
  else
    path
  end
end