Added the code from rfc2616: push in plurals from ruby, with added (simple) tests
This commit is contained in:
parent
0a7e2ece80
commit
bf34b7494b
@ -59,6 +59,7 @@ module Cocoon
|
|||||||
|
|
||||||
html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
|
html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
|
||||||
html_options[:'data-association'] = association.to_s.singularize
|
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
|
new_object = f.object.class.reflect_on_association(association).klass.new
|
||||||
html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object))
|
html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object))
|
||||||
|
@ -2,6 +2,7 @@ $(document).ready(function() {
|
|||||||
|
|
||||||
$('.add_fields').live('click', function() {
|
$('.add_fields').live('click', function() {
|
||||||
var assoc = $(this).attr('data-association');
|
var assoc = $(this).attr('data-association');
|
||||||
|
var assocs = $(this).attr('data-associations');
|
||||||
var content = $(this).attr('data-template');
|
var content = $(this).attr('data-template');
|
||||||
var insertionPosition = $(this).attr('data-association-insertion-position');
|
var insertionPosition = $(this).attr('data-association-insertion-position');
|
||||||
var insertionNode = $(this).attr('data-association-insertion-node');
|
var insertionNode = $(this).attr('data-association-insertion-node');
|
||||||
@ -9,7 +10,7 @@ $(document).ready(function() {
|
|||||||
var new_id = new Date().getTime();
|
var new_id = new Date().getTime();
|
||||||
var new_content = content.replace(regexp_braced, '[' + new_id + ']');
|
var new_content = content.replace(regexp_braced, '[' + new_id + ']');
|
||||||
if (new_content == content) {
|
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 + ']');
|
new_content = content.replace(regexp_braced, '[' + new_id + ']');
|
||||||
}
|
}
|
||||||
if (insertionNode) {
|
if (insertionNode) {
|
||||||
|
@ -21,12 +21,12 @@ describe Cocoon do
|
|||||||
context "without a block" do
|
context "without a block" do
|
||||||
it "should accept a name" do
|
it "should accept a name" do
|
||||||
result = @tester.link_to_add_association('add something', @form_obj, :comments)
|
result = @tester.link_to_add_association('add something', @form_obj, :comments)
|
||||||
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-template="form&lt;tag&gt;">add something</a>'
|
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">add something</a>'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should accept html options and pass them to link_to" do
|
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 = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'})
|
||||||
result.to_s.should == '<a href="#" class="something silly add_fields" data-association="comment" data-template="form&lt;tag&gt;">add something</a>'
|
result.to_s.should == '<a href="#" class="something silly add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">add something</a>'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,14 +35,22 @@ describe Cocoon do
|
|||||||
result = @tester.link_to_add_association(@form_obj, :comments) do
|
result = @tester.link_to_add_association(@form_obj, :comments) do
|
||||||
"some long name"
|
"some long name"
|
||||||
end
|
end
|
||||||
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-template="form&lt;tag&gt;">some long name</a>'
|
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">some long name</a>'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should accept html options and pass them to link_to" do
|
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
|
result = @tester.link_to_add_association(@form_obj, :comments, {:class => 'floppy disk'}) do
|
||||||
"some long name"
|
"some long name"
|
||||||
end
|
end
|
||||||
result.to_s.should == '<a href="#" class="floppy disk add_fields" data-association="comment" data-template="form&lt;tag&gt;">some long name</a>'
|
result.to_s.should == '<a href="#" class="floppy disk add_fields" data-association="comment" data-associations="comments" data-template="form&lt;tag&gt;">some long name</a>'
|
||||||
|
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 == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form&lt;tag&gt;">add something</a>'
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
2
spec/dummy/app/models/person.rb
Normal file
2
spec/dummy/app/models/person.rb
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
class Person < ActiveRecord::Base
|
||||||
|
end
|
@ -1,3 +1,4 @@
|
|||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
has_many :comments
|
has_many :comments
|
||||||
|
has_many :people
|
||||||
end
|
end
|
||||||
|
15
spec/dummy/db/migrate/20110420222224_create_people.rb
Normal file
15
spec/dummy/db/migrate/20110420222224_create_people.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user