Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pass along options from add_associated link to link_to_function, so y…

…ou can add things like html class names

git-svn-id: http://svn.jamesgolick.com/attribute_fu/trunk@56 80b79608-713f-0410-8737-d8c0d0c1b50c
  • Loading branch information...
commit eb2607ec9ebfcb985b6263cd7bf09ff5e100e1a5 1 parent 56ad7ce
james authored
View
10 lib/attribute_fu/associated_form_helper.rb
@@ -74,17 +74,19 @@ def remove_link(name, *args)
# * <tt>:container</tt> - specify the DOM id of the container in which to insert the new element.
# * <tt>:name</tt> - specify an alternate class name for the associated model (underscored)
#
+ # Any additional options are forwarded to link_to_function. See its documentation for available options.
+ #
def add_associated_link(name, object, opts = {})
- associated_name = opts[:name] || object.class.name.underscore
+ associated_name = opts.delete(:name) || object.class.name.underscore
variable = "attribute_fu_#{associated_name}_count"
opts.symbolize_keys!
- partial = opts[:partial] || associated_name
- container = opts[:container] || associated_name.pluralize
+ partial = opts.delete(:partial) || associated_name
+ container = opts.delete(:container) || associated_name.pluralize
form_builder = self # because the value of self changes in the block
- @template.link_to_function name do |page|
+ @template.link_to_function(name, opts) do |page|
page << "if (typeof #{variable} == 'undefined') #{variable} = 0;"
page << "new Insertion.Bottom('#{container}', new Template("+form_builder.render_associated_form(object, :fields_for => { :javascript => true }, :partial => partial).to_json+").evaluate({'number': --#{variable}}))"
end
View
8 test/test/unit/associated_form_helper_test.rb
@@ -130,7 +130,7 @@ def setup
_erbout = ''
fields_for(:photo) do |f|
f.stubs(:render_associated_form).with(comment, :fields_for => {:javascript => true}, :partial => 'comment')
- _erbout.concat f.add_associated_link("Add Comment", comment)
+ _erbout.concat f.add_associated_link("Add Comment", comment, :class => 'something')
end
@erbout = _erbout
@@ -153,10 +153,14 @@ def setup
assert_match "attribute_fu_comment_count", @erbout
end
+ should "pass along the additional options to the link_to_function call" do
+ assert_match 'class="something"', @erbout
+ end
+
should "produce the following link" do
# this is a way of testing the whole link
assert_equal %{
- <a href=\"#\" onclick=\"if (typeof attribute_fu_comment_count == 'undefined') attribute_fu_comment_count = 0;\nnew Insertion.Bottom('comments', new Template(null).evaluate({'number': --attribute_fu_comment_count})); return false;\">Add Comment</a>
+ <a class=\"something\" href=\"#\" onclick=\"if (typeof attribute_fu_comment_count == 'undefined') attribute_fu_comment_count = 0;\nnew Insertion.Bottom('comments', new Template(null).evaluate({'number': --attribute_fu_comment_count})); return false;\">Add Comment</a>
}.strip, @erbout
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.