Class: NavNode

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/nav_node.rb

Overview

Each Navable object has got an associated NavNode, i.e. an object representing the information relevant to the position of the Navable object within the navigational structure.

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

#readonly?

Instance Method Details

#ancestor_navablesObject

This returns the Navable ancestors of the Navable associated with this NavNode as an Array.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/nav_node.rb', line 130

def ancestor_navables
  path = []
  current_navable = self.navable
  until current_navable.nil?
    new_navable = nil
    new_navable ||= current_navable.status_group_in_primary_corporation if current_navable.kind_of? User
    new_navable ||= current_navable.parents.select do |parent|
      parent.respond_to? :nav_node
    end.first
    path << new_navable if new_navable
    current_navable = new_navable
  end
  path.reverse
end

#ancestor_navables_and_ownObject

This returns the Navable ancestors of the Navable associated with this NavNode as an Array plus this NavNode’s Navable as last element.



148
149
150
# File 'app/models/nav_node.rb', line 148

def ancestor_navables_and_own
  ancestor_navables + [ self.navable ]
end

#ancestor_nodesObject

ancestor_nodes returns an Array of the NavNodes of the ancestors of the Navable associated with this NavNode.



155
156
157
158
159
# File 'app/models/nav_node.rb', line 155

def ancestor_nodes
  @ancestor_nodes ||= self.ancestor_navables.collect do |ancestor|
    ancestor.nav_node
  end
end

#ancestor_nodes_and_selfObject

ancestor_nodes_and_self returns an Array of the NavNodes of the ancestors of the Navable associated with this NavNode plus this NavNode as last element.



164
165
166
# File 'app/models/nav_node.rb', line 164

def ancestor_nodes_and_self
  ancestor_nodes + [ self ]
end

The breadcrumb_item is the string representing the Navable in a breadcrumb navigation.

For example: example.com > Products > Phones

--------

The String “Products” is the breadcrumb_item of the @products_page. It defaults to the Navable’s title and can be customized using the setter method breadcrumb_item=.



41
42
43
# File 'app/models/nav_node.rb', line 41

def breadcrumb_item
  super || self.navable.title
end

breadcrumbs returns an Array of breadcrumb Hashes representing the route to the Navable associated with this NavNode.

Example:

Breadcrumb:  Example.com  >  Products  >  Phones
Url:         http://example.com/products/phones

  @phones_page.nav_node.breadcrumbs  
    # => [ {title: "Example.com", navable: @root_page, slim: false},
           {title: "Products", navable: @products_page, slim: false},
           {title: "Phones", navable: @phones_page, slim: false} ]


117
118
119
120
121
122
123
124
125
126
# File 'app/models/nav_node.rb', line 117

def breadcrumbs
  breadcrumbs_to_return = []
  navables = self.ancestor_navables_and_own
  for navable in navables do
    breadcrumbs_to_return << { title: navable.nav_node.breadcrumb_item, 
                               navable: navable, 
                               slim: navable.nav_node.slim_breadcrumb }
  end
  return breadcrumbs_to_return
end

#hidden_menuObject

The hidden_menu attribute says if the Navable should be hidden from the vertical menu.

By default,

* Pages are shown in the menu
* Groups are shown in the menu
*   exception: The :officers_parent groups are hidden in the menu.
* Users are hidden in the menu
* Events are hidden in the menu
* Workflows are hidden in the menu

You can override the setting for a Navable by using the setter method hidden_menu= on the NavNode.



67
68
69
70
71
72
73
74
75
# File 'app/models/nav_node.rb', line 67

def hidden_menu
  hidden = super
  hidden = true if self.navable.kind_of? User if hidden.nil?
  hidden = true if self.navable.kind_of? Event if hidden.nil?
  hidden = true if self.navable.title == I18n.t(:officers_parent) if hidden.nil?
  hidden = true if self.navable.kind_of?(Page) && (self.navable.type == "BlogPost")
  hidden = false if hidden.nil?
  return hidden
end

The menu_item is the string representing the Navable in the vertical menu. It defaults to the Navable’s title and can be customized using the setter method menu_item=.



49
50
51
# File 'app/models/nav_node.rb', line 49

def menu_item
  super || self.navable.title
end

#slim_breadcrumbObject

slim_breadcrumbs marks if the Navable should be hidden from the breadcrumb navigation in order to save space.

By default, no element is hidden from the breadcrumb navigation. To hide an element, just set

@some_page.nav_node.update_attribute(:slim_breadcrumb, true)


85
86
87
# File 'app/models/nav_node.rb', line 85

def slim_breadcrumb
  super || false
end

#urlObject

url returns the joined url_components of this NavNode’s Navable and its ancestors resulting in the generated url of the Navable.

Example:

Breadcrumb:  Example.com  >  Products  >  Phones
Url:         http://example.com/products/phones

A possible trailing slash is removed from the url. Thus, the example’s url does end on ‘phones’ rather than ‘phones/’.



99
100
101
102
103
# File 'app/models/nav_node.rb', line 99

def url
  url = ancestor_nodes_and_self.collect do |nav_node|
    nav_node.url_component
  end.join.gsub( /(\/)$/, '' ) # The gsub call removes the trailing slash.
end

#url_componentObject

The url_component represents the part of the url, which is contributed by the Navable object.

If you have the following url

http://example.com/products/phones/ ,

and the current Navable is the Page @products_page, then its url_component is ‘products/’.

@products_page = Page.find_by_title("Products")
@products_page.nav_node.url_component  # => "products/"

The default url_component uses the Navable’s title. But you can override the url_component of a Navable just by setting it.

@nav_node = @products_page.nav_node
@nav_node.url_component = "our_products/"
@nav_node.save


29
30
31
# File 'app/models/nav_node.rb', line 29

def url_component
  super || "#{self.navable.title.parameterize}/"
end