diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb
index be78f2a..5ee55a8 100644
--- a/lib/cocoon/view_helpers.rb
+++ b/lib/cocoon/view_helpers.rb
@@ -59,6 +59,7 @@ module Cocoon
html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
html_options[:'data-association'] = association.to_s.singularize
+ html_options[:'data-associations'] = association.to_s.pluralize
new_object = f.object.class.reflect_on_association(association).klass.new
html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object))
diff --git a/lib/generators/cocoon/install/templates/cocoon.js b/lib/generators/cocoon/install/templates/cocoon.js
index 6e1f64c..a00909f 100644
--- a/lib/generators/cocoon/install/templates/cocoon.js
+++ b/lib/generators/cocoon/install/templates/cocoon.js
@@ -2,6 +2,7 @@ $(document).ready(function() {
$('.add_fields').live('click', function() {
var assoc = $(this).attr('data-association');
+ var assocs = $(this).attr('data-associations');
var content = $(this).attr('data-template');
var insertionPosition = $(this).attr('data-association-insertion-position');
var insertionNode = $(this).attr('data-association-insertion-node');
@@ -9,7 +10,7 @@ $(document).ready(function() {
var new_id = new Date().getTime();
var new_content = content.replace(regexp_braced, '[' + new_id + ']');
if (new_content == content) {
- regexp_braced = new RegExp('\\[new_' + assoc + 's\\]', 'g');
+ regexp_braced = new RegExp('\\[new_' + assocs + '\\]', 'g');
new_content = content.replace(regexp_braced, '[' + new_id + ']');
}
if (insertionNode) {
diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb
index b88bbac..b52ea8f 100644
--- a/spec/cocoon_spec.rb
+++ b/spec/cocoon_spec.rb
@@ -21,12 +21,12 @@ describe Cocoon do
context "without a block" do
it "should accept a name" do
result = @tester.link_to_add_association('add something', @form_obj, :comments)
- result.to_s.should == 'add something'
+ result.to_s.should == 'add something'
end
it "should accept html options and pass them to link_to" do
result = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'})
- result.to_s.should == 'add something'
+ result.to_s.should == 'add something'
end
end
@@ -35,14 +35,22 @@ describe Cocoon do
result = @tester.link_to_add_association(@form_obj, :comments) do
"some long name"
end
- result.to_s.should == 'some long name'
+ result.to_s.should == 'some long name'
end
it "should accept html options and pass them to link_to" do
result = @tester.link_to_add_association(@form_obj, :comments, {:class => 'floppy disk'}) do
"some long name"
end
- result.to_s.should == 'some long name'
+ result.to_s.should == 'some long name'
+ end
+
+ end
+
+ context "with an irregular plural" do
+ it "should use the correct plural" do
+ result = @tester.link_to_add_association('add something', @form_obj, :people)
+ result.to_s.should == 'add something'
end
end
diff --git a/spec/dummy/app/models/person.rb b/spec/dummy/app/models/person.rb
new file mode 100644
index 0000000..2f2e286
--- /dev/null
+++ b/spec/dummy/app/models/person.rb
@@ -0,0 +1,2 @@
+class Person < ActiveRecord::Base
+end
diff --git a/spec/dummy/app/models/post.rb b/spec/dummy/app/models/post.rb
index 6ad2382..2fccaf5 100644
--- a/spec/dummy/app/models/post.rb
+++ b/spec/dummy/app/models/post.rb
@@ -1,3 +1,4 @@
class Post < ActiveRecord::Base
has_many :comments
+ has_many :people
end
diff --git a/spec/dummy/db/migrate/20110420222224_create_people.rb b/spec/dummy/db/migrate/20110420222224_create_people.rb
new file mode 100644
index 0000000..97abd64
--- /dev/null
+++ b/spec/dummy/db/migrate/20110420222224_create_people.rb
@@ -0,0 +1,15 @@
+class CreatePeople < ActiveRecord::Migration
+ def self.up
+ create_table :people do |t|
+ t.string :name
+ t.string :description
+ t.integer :post_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :people
+ end
+end