Added argument to list_of to be able to pass attributes to generated list item elements.
Closes #507 Signed-off-by: Norman Clarke <norman@njclarke.com>
This commit is contained in:
parent
13eeaf2ead
commit
296484c82c
|
@ -13,6 +13,9 @@
|
|||
|
||||
* Generate object references based on `#to_key` if it exists in preference to `#id`.
|
||||
|
||||
* Helper `list_of` takes an extra argument that is rendered into list item attributes
|
||||
(thanks [Iain Barnett](http://iainbarnett.me.uk/))
|
||||
|
||||
* Fix parser to allow lines ending with `some_method?` to be a Ruby multinline
|
||||
(thanks to [Brad Ediger](https://github.com/bradediger))
|
||||
|
||||
|
|
|
@ -147,7 +147,7 @@ MESSAGE
|
|||
# <li>hello</li>
|
||||
# <li>yall</li>
|
||||
#
|
||||
# And
|
||||
# And:
|
||||
#
|
||||
# = list_of({:title => 'All the stuff', :description => 'A book about all the stuff.'}) do |key, val|
|
||||
# %h3= key.humanize
|
||||
|
@ -164,10 +164,33 @@ MESSAGE
|
|||
# <p>A book about all the stuff.</p>
|
||||
# </li>
|
||||
#
|
||||
# While:
|
||||
#
|
||||
# = list_of(["Home", "About", "Contact", "FAQ"], {class: "nav", role: "nav"}) do |item|
|
||||
# %a{ href="#" }= item
|
||||
#
|
||||
# Produces:
|
||||
#
|
||||
# <li class='nav' role='nav'>
|
||||
# <a href='#'>Home</a>
|
||||
# </li>
|
||||
# <li class='nav' role='nav'>
|
||||
# <a href='#'>About</a>
|
||||
# </li>
|
||||
# <li class='nav' role='nav'>
|
||||
# <a href='#'>Contact</a>
|
||||
# </li>
|
||||
# <li class='nav' role='nav'>
|
||||
# <a href='#'>FAQ</a>
|
||||
# </li>
|
||||
#
|
||||
# `[[class", "nav"], [role", "nav"]]` could have been used instead of `{class: "nav", role: "nav"}` (or any enumerable collection where each pair of items responds to #to_s)
|
||||
#
|
||||
# @param enum [Enumerable] The list of objects to iterate over
|
||||
# @param [Enumerable<#to_s,#to_s>] opts Each key/value pair will become an attribute pair for each list item element.
|
||||
# @yield [item] A block which contains Haml code that goes within list items
|
||||
# @yieldparam item An element of `enum`
|
||||
def list_of(enum, &block)
|
||||
def list_of(enum, opts={}, &block)
|
||||
to_return = enum.collect do |i|
|
||||
result = capture_haml(i, &block)
|
||||
|
||||
|
@ -178,7 +201,7 @@ MESSAGE
|
|||
result = result.strip
|
||||
end
|
||||
|
||||
"<li>#{result}</li>"
|
||||
%Q!<li#{opts.empty? ? "" : " ".<<(opts.map{|k,v| "#{k}='#{v}'" }.join(" "))}>#{result}</li>!
|
||||
end
|
||||
to_return.join("\n")
|
||||
end
|
||||
|
|
|
@ -30,7 +30,7 @@ class HelperTest < Test::Unit::TestCase
|
|||
on(name) || []
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def setup
|
||||
@base = ActionView::Base.new
|
||||
@base.controller = ActionController::Base.new
|
||||
|
@ -68,6 +68,10 @@ class HelperTest < Test::Unit::TestCase
|
|||
assert_equal("<li>[1]</li>\n", render("= list_of([[1]]) do |i|\n = i.inspect"))
|
||||
assert_equal("<li>\n <h1>Fee</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fi</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fo</h1>\n <p>A word!</p>\n</li>\n<li>\n <h1>Fum</h1>\n <p>A word!</p>\n</li>\n",
|
||||
render("= list_of(['Fee', 'Fi', 'Fo', 'Fum']) do |title|\n %h1= title\n %p A word!"))
|
||||
assert_equal("<li c='3' d='4'>1</li>\n<li c='3' d='4'>2</li>\n", render("= list_of([1, 2], {c: 3, d: 4}) do |i|\n = i"))
|
||||
assert_equal("<li c='3' d='4'>[1]</li>\n", render("= list_of([[1]], {c: 3, d: 4}) do |i|\n = i.inspect"))
|
||||
assert_equal("<li c='3' d='4'>\n <h1>Fee</h1>\n <p>A word!</p>\n</li>\n<li c='3' d='4'>\n <h1>Fi</h1>\n <p>A word!</p>\n</li>\n<li c='3' d='4'>\n <h1>Fo</h1>\n <p>A word!</p>\n</li>\n<li c='3' d='4'>\n <h1>Fum</h1>\n <p>A word!</p>\n</li>\n",
|
||||
render("= list_of(['Fee', 'Fi', 'Fo', 'Fum'], {c: 3, d: 4}) do |title|\n %h1= title\n %p A word!"))
|
||||
end
|
||||
|
||||
def test_buffer_access
|
||||
|
@ -110,14 +114,14 @@ HAML
|
|||
ActionView::Base.new.render(:inline => "<%= concat('foo') %>")
|
||||
rescue ArgumentError, NameError
|
||||
proper_behavior = true
|
||||
end
|
||||
end
|
||||
assert(proper_behavior)
|
||||
end
|
||||
|
||||
|
||||
def test_action_view_included
|
||||
assert(Haml::Helpers.action_view?)
|
||||
end
|
||||
|
||||
|
||||
def test_form_tag
|
||||
# This is usually provided by ActionController::Base.
|
||||
def @base.protect_against_forgery?; false; end
|
||||
|
@ -359,7 +363,7 @@ HAML
|
|||
end
|
||||
|
||||
def test_capture_deals_properly_with_collections
|
||||
Haml::Helpers.module_eval do
|
||||
Haml::Helpers.module_eval do
|
||||
def trc(collection, &block)
|
||||
collection.each do |record|
|
||||
haml_concat capture_haml(record, &block)
|
||||
|
|
Loading…
Reference in New Issue