Module: Refinery::MenuHelper

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

Instance Method Summary collapse

Instance Method Details

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

Adds conditional caching



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

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

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

#descendant_page_selected?(page) ⇒ 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 unless it first quickly determines that there are no descendants.

Returns:

  • (Boolean)


32
33
34
# File 'app/helpers/refinery/menu_helper.rb', line 32

def descendant_page_selected?(page)
  (page.rgt != page.lft + 1) && page.descendants.any? { |descendant| selected_page?(descendant) }
end

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



18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/refinery/menu_helper.rb', line 18

def menu_branch_css(local_assigns)
  options = local_assigns.dup
  options.update(:sibling_count => options[:menu_branch].shown_siblings.length) unless options[:sibling_count]

  css = []
  css << Refinery::Core.menu_css[:selected] if selected_page_or_descendant_page_selected?(local_assigns[:menu_branch]) unless Refinery::Core.menu_css[:selected].nil?
  css << Refinery::Core.menu_css[:first] if options[:menu_branch_counter] == 0 unless Refinery::Core.menu_css[:first].nil?
  css << Refinery::Core.menu_css[:last] if options[:menu_branch_counter] == options[:sibling_count] unless Refinery::Core.menu_css[:last].nil?
  css
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)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/refinery/menu_helper.rb', line 42

def selected_page?(page)
  path = request.path.sub("//", "/")
  path = path.force_encoding('utf-8') if path.respond_to?(:force_encoding)

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

  # First try to match against a "menu match" value, if available.
  return true if page.try(:menu_match).present? && path =~ Regexp.new(page.menu_match)

  # Find the first url that is a string.
  url = [page.url]
  url << ['', page.url[:path]].compact.flatten.join('/') if page.url.respond_to?(:keys)
  url = url.last.match(%r{^/#{::I18n.locale.to_s}(/.*)}) ? $1 : url.detect{|u| u.is_a?(String)}

  # Now use all possible vectors to try to find a valid match,
  # finally passing to rails' "current_page?" method.
  [path, URI.decode(path)].include?(url) || path == "/#{page.original_id}"
end

#selected_page_or_descendant_page_selected?(page) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/helpers/refinery/menu_helper.rb', line 36

def selected_page_or_descendant_page_selected?(page)
  selected_page?(page) || descendant_page_selected?(page)
end