Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/spectator/mmmenu
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Snitko committed Nov 29, 2010
2 parents 4945c96 + 2999235 commit f9d467a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
41 changes: 39 additions & 2 deletions README.markdown
Expand Up @@ -33,7 +33,7 @@ when you installed the plugin. Let's take a look at what's inside this helper:

def build_mmmenu(menu)
menu.item_markup(0, :active_markup => 'class="current"') do
|link, text, options| "<li><a href=\"#{link}\" #{options}>#{text}</a></li>\n"
|link, text, options| "<li><a href=\"#{link}\" #{options[:active]}>#{text}</a></li>\n"
end
menu.level_markup(0) { |menu| '<ul class="menu">' + menu + '</ul>' }
menu.level_markup(1) { |menu| '<ul class="submenu">' + menu + '</ul>' }
Expand All @@ -43,7 +43,44 @@ when you installed the plugin. Let's take a look at what's inside this helper:
You can see now, that `#item_markup` method defines the html markup for menu item,
and `#level_markup` does the same for menu level wrapper. They may contain as much levels
as you want and you don't need to define a markup for each level: the deepest level markup
defined will be used for all of the deeper levels. Now go ahead and change this method the way you like.
defined will be used for all of the deeper levels.

Moreover, you might want to highlight `li` tag and pass some class to `a` tag, you can
do it like this:

def build_mmmenu(menu)
menu.item_markup(0, :active_markup => 'class="current"') do
|link, text, options| "<li #{options[:active]}><a href=\"#{link}\" class=\"#{options[:html]}\">#{text}</a></li>\n"
end
menu.build
end

@menu = Mmmenu.new(:request => @request) do |m|
m.add 'Item1', '/items1', :match_subpaths => true
m.add 'Item2', '/items2', :html => 'class="special_link"' do |subm|
subm.add 'New', '/item2/new'
subm.add 'Edit', '/item2/edit', :html => 'huh'
end
end

Or, in combination with powered by Rails `#content_tag` and `#link_to` like this:

def build_mmmenu(menu)
menu.item_markup(0, :active_markup => { :class => :current }) do
|link, text, options| content_tag(:li, link_to(text, link, options[:html]), options[:active])
end
menu.build
end

@menu = Mmmenu.new(:request => @request) do |m|
m.add 'Item1', '/items1', :match_subpaths => true
m.add 'Item2', '/items2', :html => { :class => :special_link } do |subm|
subm.add 'New', '/item2/new'
subm.add 'Edit', '/item2/edit'
end
end

Now go ahead and change this method the way you like.

Finally, let's take a closer look at some of the options and what they mean.
---------------------------------------------------------------------
Expand Down
9 changes: 5 additions & 4 deletions lib/mmmenu.rb
Expand Up @@ -47,7 +47,7 @@ def build_level(items=@items, level=0)
raise("Mmmenu object #{self} is empty, no items defined!") if items.nil? or items.empty?
items.each do |item|

option_current = nil
options = {}
child_menu = build_level(item[:children], level+1) if item[:children]
child_output = child_menu[:output] if child_menu

Expand All @@ -63,12 +63,13 @@ def build_level(items=@items, level=0)
) and !has_active_item

then
option_current = item_markup[:active] and has_active_item = true
options[:active] = item_markup[:active] and has_active_item = true

end
#############################################################

item_output = item_markup[:basic].call(item[:href], item[:title], option_current)
options.merge!(:html => item[:html]) if item[:html]
item_output = item_markup[:basic].call(item[:href], item[:title], options)
output += "#{item_output}#{child_output}"
end

Expand Down Expand Up @@ -122,7 +123,7 @@ def build_item_markup(level)
unless @item_markup.empty?
{ :basic => @item_markup.last[:block], :active => @item_markup.last[:active_markup] }
else
{ :basic => lambda { |link,text,options| "#{text} #{link} #{options}\n" }, :active => 'current' }
{ :basic => lambda { |link,text,options| "#{text} #{link} #{options[:active]} #{options[:html]}\n" }, :active => 'current' }
end
end
end
Expand Down
35 changes: 27 additions & 8 deletions spec/lib/mmmenu_spec.rb
Expand Up @@ -25,10 +25,10 @@

it "renders one level" do
@menu.item_markup(0, :active_markup => 'current') do |link, text, options|
"#{text}: #{link} #{options}\n"
"#{text}: #{link} #{options[:active]}\n"
end
@menu.item_markup(2, :active_markup => 'current') do |link, text, options|
" #{text}: #{link} #{options}\n"
" #{text}: #{link} #{options[:active]}\n"
end
@menu.level_markup(0) { |menu| menu }
(@menu.build.chomp(" \n") + "\n").should == <<END
Expand All @@ -54,7 +54,7 @@
request.should_receive(:params).once.and_return({})
@menu = Mmmenu.new(:items => items, :request => request )
@menu.item_markup(0, :active_markup => 'current') do |link, text, options|
"#{text}: #{link} #{options}\n"
"#{text}: #{link} #{options[:active]}\n"
end
@menu.build.should == <<END
item1: /item1
Expand All @@ -75,7 +75,7 @@
request.should_receive(:method).once.and_return('get')
@menu = Mmmenu.new(:items => items, :request => request )
@menu.item_markup(0, :active_markup => 'current') do |link, text, options|
"#{text}: #{link} #{options}\n"
"#{text}: #{link} #{options[:active]}\n"
end
@menu.build.should == <<END
item1: /item1 current
Expand All @@ -95,10 +95,29 @@
end

@menu.build.should == <<END
Item1 /items1 current
Item2 /items2\s
New /item2/new\s
Edit /item2/edit\s
Item1 /items1 current\s
Item2 /items2\s\s
New /item2/new\s\s
Edit /item2/edit\s\s
END

end

it "creates menu items in a block using nice DSL and additional options" do

@menu = Mmmenu.new(:request => @request) do |m|
m.add 'Item1', '/items1', :match_subpaths => true
m.add 'Item2', '/items2', :html => 'whatever' do |subm|
subm.add 'New', '/item2/new'
subm.add 'Edit', '/item2/edit', :html => 'huh'
end
end

@menu.build.should == <<END
Item1 /items1 current\s
Item2 /items2 whatever
New /item2/new\s\s
Edit /item2/edit huh
END

end
Expand Down

0 comments on commit f9d467a

Please sign in to comment.