Module: Staticpress::Plugins::Menu

Defined in:
lib/staticpress/plugins/menu.rb

Defined Under Namespace

Classes: MenuItem

Instance Method Summary collapse

Instance Method Details

#convert_to_menu_item(page) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/staticpress/plugins/menu.rb', line 36

def convert_to_menu_item(page)
  parent_position = page.meta.menu.position.to_s
  matcher = /^#{parent_position.gsub('.', '\.')}\.(?<sub_position>-?\d{1,})$/
  sub_items = menu_pages.select do |sub_page|
    matcher.match sub_page.meta.menu.position.to_s
  end.map do |sub_page|
    convert_to_menu_item sub_page
  end
  MenuItem.new parent_position.split('.').last, page.meta.menu.text || page.title, page.url_path, sub_items
end


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/staticpress/plugins/menu.rb', line 7

def menu(max_depth, menu_items = root_menu_items, menu_options = {})
  if (menu_items.count > 0) && !max_depth.zero?
    tag :menu, { :type => :toolbar }.merge(menu_options) do
      menu_items.sort.map do |item|
        tag :li do
          a = tag :a, :href => item.url_path do
            item.text
          end
          remaining_depth = (max_depth < 0) ? max_depth : max_depth - 1
          sub = menu(remaining_depth, item.sub_items, menu_options) || ''
          a + sub
        end
      end.join
    end
  end
end


47
48
49
# File 'lib/staticpress/plugins/menu.rb', line 47

def menu_depth(position)
  position.split('.').count - 1
end


28
29
30
31
32
33
34
# File 'lib/staticpress/plugins/menu.rb', line 28

def menu_pages
  @menu_pages ||= lambda do
    Staticpress::Content::Page.published.reject do |page|
      page.meta.menu.nil?
    end
  end.call
end

#root_menuObject



3
4
5
# File 'lib/staticpress/plugins/menu.rb', line 3

def root_menu
  menu 1
end

#root_menu_itemsObject



51
52
53
54
55
56
57
# File 'lib/staticpress/plugins/menu.rb', line 51

def root_menu_items
  menu_pages.select do |page|
    menu_depth(page.meta.menu.position.to_s).zero?
  end.map do |page|
    convert_to_menu_item page
  end
end


24
25
26
# File 'lib/staticpress/plugins/menu.rb', line 24

def sub_menu_for(page)
  convert_to_menu_item(page).sub_items
end