Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add an option to provide an alternate javascript expression with whic…

…h to select the container to insert new forms in to with add_associated_link

git-svn-id: http://svn.jamesgolick.com/attribute_fu/trunk@71 80b79608-713f-0410-8737-d8c0d0c1b50c
  • Loading branch information...
commit d2e65304fe8d69e42af96b053c75a48d72c8a302 1 parent 5abaa86
james authored
View
7 lib/attribute_fu/associated_form_helper.rb
@@ -72,6 +72,7 @@ def remove_link(name, *args)
# Options are:
# * <tt>:partial</tt> - specify the name of the partial in which the form is located.
# * <tt>:container</tt> - specify the DOM id of the container in which to insert the new element.
+ # * <tt>:expression</tt> - specify a javascript expression with which to select the container to insert the new form in to (i.e. $(this).up('.tasks'))
# * <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.
@@ -81,14 +82,14 @@ def add_associated_link(name, object, opts = {})
variable = "attribute_fu_#{associated_name}_count"
opts.symbolize_keys!
- partial = opts.delete(:partial) || associated_name
- container = opts.delete(:container) || associated_name.pluralize
+ partial = opts.delete(:partial) || associated_name
+ container = opts.delete(:expression) || "'#{opts.delete(:container) || associated_name.pluralize}'"
form_builder = self # because the value of self changes in the block
@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}}))"
+ 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
end
View
38 test/test/unit/associated_form_helper_test.rb
@@ -203,6 +203,44 @@ def setup
end
end
+ context "add associated link with expression parameter" do
+ setup do
+ comment = @photo.comments.build
+
+ _erbout = ''
+ fields_for(:photo) do |f|
+ f.stubs(:render_associated_form).with(comment, :fields_for => {:javascript => true}, :partial => 'some_other_partial')
+ _erbout.concat f.add_associated_link("Add Comment", comment, :expression => '$(this).up(".something_comments")', :partial => 'some_other_partial')
+ end
+
+ @erbout = _erbout
+ end
+
+ should "create link" do
+ assert_match ">Add Comment</a>", @erbout
+ end
+
+ should "use the javascript expression provided instead of passing the ID in" do
+ assert_match "Insertion.Bottom($(this).up(&quot;.something_comments&quot;)", @erbout
+ end
+
+ should "wrap the partial in a prototype template" do
+ assert_match "new Template", @erbout
+ assert_match "evaluate", @erbout
+ end
+
+ should "name the variable correctly" do
+ assert_match "attribute_fu_comment_count", @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($(this).up(&quot;.something_comments&quot;), new Template(null).evaluate({'number': --attribute_fu_comment_count})); return false;\">Add Comment</a>
+ }.strip, @erbout
+ end
+ end
+
context "render_associated_form" do
setup do
comment = @photo.comments.build
Please sign in to comment.
Something went wrong with that request. Please try again.