public
Fork of giraffesoft/attribute_fu
Description: rails multi-model forms made easy!
Homepage: http://jamesgolick.com/attribute_fu
Clone URL: git://github.com/cjkihlbom/attribute_fu.git
Fixed add_associated_link helper so that it produces unique IDs for the 
fields
cjkihlbom (author)
Wed Aug 27 02:27:13 -0700 2008
commit  9e8e07b21cdbe0b029bdca5c54373a95582c0d85
tree    2f4ccddcd75903f7a6a9db257270b137cd8c45a2
parent  254728ee063c067c93624c1a1937084ed09a77b7
...
89
90
91
92
 
93
94
95
...
89
90
91
 
92
93
94
95
0
@@ -89,7 +89,7 @@ module AttributeFu
0
       
0
       @template.link_to_function(name, opts) do |page|
0
         page << "if (typeof #{variable} == 'undefined') #{variable} = 0;"
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}}))"
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}}).gsub(/__number_/, #{variable}))"
0
       end
0
     end
0
     
...
1
2
3
4
5
6
7
8
 
 
 
 
9
10
11
12
13
14
15
 
 
 
16
...
1
 
 
 
 
 
 
 
2
3
4
5
6
 
 
 
 
 
 
7
8
9
10
0
@@ -1,15 +1,9 @@
0
 development:
0
- adapter: mysql
0
- encoding: utf8
0
- database: attribute_fu_test
0
- username: root
0
- password:
0
- socket: /tmp/mysql.sock
0
-
0
+ adapter: sqlite3
0
+ database: db/development.sqlite3
0
+ timeout: 5000
0
+
0
 test:
0
- adapter: mysql
0
- encoding: utf8
0
- database: attribute_fu_test
0
- username: root
0
- password:
0
- socket: /tmp/mysql.sock
0
+ adapter: sqlite3
0
+ database: db/test.sqlite3
0
+ timeout: 5000
0
\ No newline at end of file
...
160
161
162
163
 
164
165
166
...
198
199
200
201
 
202
203
204
...
236
237
238
239
 
240
241
242
...
160
161
162
 
163
164
165
166
...
198
199
200
 
201
202
203
204
...
236
237
238
 
239
240
241
242
0
@@ -160,7 +160,7 @@ class AssociatedFormHelperTest < Test::Unit::TestCase
0
     should "produce the following link" do
0
       # this is a way of testing the whole link
0
       assert_equal %{
0
- <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>
0
+ <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}).gsub(/__number_/, attribute_fu_comment_count)); return false;\">Add Comment</a>
0
       }.strip, @erbout
0
     end
0
   end
0
@@ -198,7 +198,7 @@ class AssociatedFormHelperTest < Test::Unit::TestCase
0
     should "produce the following link" do
0
       # this is a way of testing the whole link
0
       assert_equal %{
0
- <a href=\"#\" onclick=\"if (typeof attribute_fu_comment_count == 'undefined') attribute_fu_comment_count = 0;\nnew Insertion.Bottom('something_comments', new Template(null).evaluate({'number': --attribute_fu_comment_count})); return false;\">Add Comment</a>
0
+ <a href=\"#\" onclick=\"if (typeof attribute_fu_comment_count == 'undefined') attribute_fu_comment_count = 0;\nnew Insertion.Bottom('something_comments', new Template(null).evaluate({'number': --attribute_fu_comment_count}).gsub(/__number_/, attribute_fu_comment_count)); return false;\">Add Comment</a>
0
       }.strip, @erbout
0
     end
0
   end
0
@@ -236,7 +236,7 @@ class AssociatedFormHelperTest < Test::Unit::TestCase
0
     should "produce the following link" do
0
       # this is a way of testing the whole link
0
       assert_equal %{
0
- <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>
0
+ <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}).gsub(/__number_/, attribute_fu_comment_count)); return false;\">Add Comment</a>
0
       }.strip, @erbout
0
     end
0
   end

Comments

  • cjkihlbom, can you explain exactly what is happening in this commit? I’m fairly sure this is doing what I’m trying to get from attribute_fu, which is to create unique identifiers for elements in partials brought in from the add_associated_link. I just can’t figure out how to get that unique id.

  • Sure, here’s the gist of it:

    The problem:

    fields_for sanitizes object names when generating IDs. 'project[todo_attributes][new][#{number}]' becomes 'project_todo_attributes__new_____number_', causing the Template, which looks for the string '#{number}' to not replace the number in the ID with the variable count. That means that all associated fields generated by clicking the link will have the same IDs.

    The fix (hack):

    gsub the '__number_' with the variable count after the Template evaluation.
    Not the cleanest solution, but it works.