Added the code from rfc2616: push in plurals from ruby, with added (simple) tests

This commit is contained in:
nathanvda 2011-04-24 14:36:50 +02:00
parent 0a7e2ece80
commit bf34b7494b
6 changed files with 33 additions and 5 deletions

View File

@ -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))

View File

@ -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) {

View File

@ -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 == '<a href="#" class="add_fields" data-association="comment" data-template="form&amp;lt;tag&amp;gt;">add something</a>'
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-associations="comments" data-template="form&amp;lt;tag&amp;gt;">add something</a>'
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 == '<a href="#" class="something silly add_fields" data-association="comment" data-template="form&amp;lt;tag&amp;gt;">add something</a>'
result.to_s.should == '<a href="#" class="something silly add_fields" data-association="comment" data-associations="comments" data-template="form&amp;lt;tag&amp;gt;">add something</a>'
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 == '<a href="#" class="add_fields" data-association="comment" data-template="form&amp;lt;tag&amp;gt;">some long name</a>'
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-associations="comments" data-template="form&amp;lt;tag&amp;gt;">some long name</a>'
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 == '<a href="#" class="floppy disk add_fields" data-association="comment" data-template="form&amp;lt;tag&amp;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&amp;lt;tag&amp;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&amp;lt;tag&amp;gt;">add something</a>'
end
end

View File

@ -0,0 +1,2 @@
class Person < ActiveRecord::Base
end

View File

@ -1,3 +1,4 @@
class Post < ActiveRecord::Base
has_many :comments
has_many :people
end

View 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