Module: Refinery::Helpers::MenuHelper

Defined in:
lib/refinery/helpers/menu_helper.rb

Instance Method Summary collapse

Instance Method Details

#cache_if(condition, name = {}, &block) ⇒ Object

Adds conditional caching



6
7
8
9
10
11
12
13
14
15
# File 'lib/refinery/helpers/menu_helper.rb', line 6

def cache_if(condition, name = {}, &block)
  if condition
    cache(name, &block)
  else
    yield
  end

  # for <%= style helpers vs <% style
  nil
end

#css_for_menu_branch(menu_branch, menu_branch_counter, sibling_count = nil, collection = [], selected_item = nil) ⇒ Object

This was extracted from app/views/shared/_menu_branch.html.erb to remove the complexity of that template by reducing logic in the view.



19
20
21
22
23
24
25
# File 'lib/refinery/helpers/menu_helper.rb', line 19

def css_for_menu_branch(menu_branch, menu_branch_counter, sibling_count = nil, collection = [], selected_item = nil)
  css = []
  css << "selected" if selected_page_or_descendant_page_selected?(menu_branch, collection, selected_item)
  css << "first" if menu_branch_counter == 0
  css << "last" if menu_branch_counter == (sibling_count ||= menu_branch.shown_siblings.size)
  css
end

#descendant_page_selected?(page, collection = [], selected_item = nil) ⇒ Boolean

Determines whether any page underneath the supplied page is the current page according to rails. Just calls selected_page? for each descendant of the supplied page. if you pass a collection it won’t check its own descendants but use the collection supplied.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/refinery/helpers/menu_helper.rb', line 30

def descendant_page_selected?(page, collection = [], selected_item = nil)
  return false unless page.has_descendants? or (selected_item && !selected_item.in_menu?)

  descendants = if collection.present? and (!selected_item or (selected_item && selected_item.in_menu?))
    collection.select{ |item| item.parent_id == page.id }
  else
    page.descendants
  end

  descendants.any? do |descendant|
    selected_item ? selected_item == descendant : selected_page?(descendant)
  end
end

#selected_page?(page) ⇒ Boolean

Determine whether the supplied page is the currently open page according to Refinery. Also checks whether Rails thinks it is selected after that using current_page?

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/refinery/helpers/menu_helper.rb', line 53

def selected_page?(page)
  path = request.path

  # Ensure we match the path without the locale, if present.
  if defined?(::Refinery::I18n) and ::Refinery::I18n.enabled? and path =~ %r{^/#{::I18n.locale}}
    path = path.split(%r{^/#{::I18n.locale}}).last
    path = "/" if path.blank?
  end

  # Match path based on cascading rules.
  (path =~ Regexp.new(page.menu_match) if page.menu_match.present?) or
    (path == page.link_url) or
    (path == page.nested_path) or
    current_page?(page)
end

#selected_page_or_descendant_page_selected?(page, collection = [], selected_item = nil) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/refinery/helpers/menu_helper.rb', line 44

def selected_page_or_descendant_page_selected?(page, collection = [], selected_item = nil)
  selected = false
  selected = selected_item ? selected_item === page : selected_page?(page)
  selected = descendant_page_selected?(page, collection, selected_item) unless selected
  selected
end