From 04c231570bf49323d84771ecf6ed0b10db461faf Mon Sep 17 00:00:00 2001 From: fl00r Date: Mon, 12 Mar 2012 23:07:10 +0400 Subject: [PATCH 1/2] Few minor fixes: - Gemfile.lock is kicked out - rdoc/task fix Added support of in associations --- .gitignore | 1 + Gemfile.lock | 125 ---------------------------------- Rakefile | 2 +- lib/cocoon/view_helpers.rb | 12 +++- spec/cocoon_spec.rb | 7 ++ spec/dummy/app/models/post.rb | 1 + 6 files changed, 21 insertions(+), 127 deletions(-) delete mode 100644 Gemfile.lock diff --git a/.gitignore b/.gitignore index 4fcd820..fa4ca05 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ spec/dummy/tmp/ .rvmrc .idea coverage/ +Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index a99b03e..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,125 +0,0 @@ -GEM - remote: http://rubygems.org/ - specs: - actionmailer (3.1.3) - actionpack (= 3.1.3) - mail (~> 2.3.0) - actionpack (3.1.3) - activemodel (= 3.1.3) - activesupport (= 3.1.3) - builder (~> 3.0.0) - erubis (~> 2.7.0) - i18n (~> 0.6) - rack (~> 1.3.5) - rack-cache (~> 1.1) - rack-mount (~> 0.8.2) - rack-test (~> 0.6.1) - sprockets (~> 2.0.3) - activemodel (3.1.3) - activesupport (= 3.1.3) - builder (~> 3.0.0) - i18n (~> 0.6) - activerecord (3.1.3) - activemodel (= 3.1.3) - activesupport (= 3.1.3) - arel (~> 2.2.1) - tzinfo (~> 0.3.29) - activeresource (3.1.3) - activemodel (= 3.1.3) - activesupport (= 3.1.3) - activesupport (3.1.3) - multi_json (~> 1.0) - arel (2.2.1) - builder (3.0.0) - diff-lcs (1.1.3) - erubis (2.7.0) - generator_spec (0.8.4) - rails (>= 3.0, < 4.0) - rspec-rails - git (1.2.5) - hike (1.2.1) - i18n (0.6.0) - jeweler (1.6.4) - bundler (~> 1.0) - git (>= 1.2.5) - rake - json (1.6.3) - json_pure (1.6.3) - mail (2.3.0) - i18n (>= 0.4.0) - mime-types (~> 1.16) - treetop (~> 1.4.8) - mime-types (1.17.2) - multi_json (1.0.4) - polyglot (0.3.3) - rack (1.3.5) - rack-cache (1.1) - rack (>= 0.4) - rack-mount (0.8.3) - rack (>= 1.0.0) - rack-ssl (1.3.2) - rack - rack-test (0.6.1) - rack (>= 1.0) - rails (3.1.3) - actionmailer (= 3.1.3) - actionpack (= 3.1.3) - activerecord (= 3.1.3) - activeresource (= 3.1.3) - activesupport (= 3.1.3) - bundler (~> 1.0) - railties (= 3.1.3) - railties (3.1.3) - actionpack (= 3.1.3) - activesupport (= 3.1.3) - rack-ssl (~> 1.3.2) - rake (>= 0.8.7) - rdoc (~> 3.4) - thor (~> 0.14.6) - rake (0.9.2.2) - rdoc (3.11) - json (~> 1.4) - rspec (2.7.0) - rspec-core (~> 2.7.0) - rspec-expectations (~> 2.7.0) - rspec-mocks (~> 2.7.0) - rspec-core (2.7.1) - rspec-expectations (2.7.0) - diff-lcs (~> 1.1.2) - rspec-mocks (2.7.0) - rspec-rails (2.7.0) - actionpack (~> 3.0) - activesupport (~> 3.0) - railties (~> 3.0) - rspec (~> 2.7.0) - simplecov (0.5.4) - multi_json (~> 1.0.3) - simplecov-html (~> 0.5.3) - simplecov-html (0.5.3) - sprockets (2.0.3) - hike (~> 1.2) - rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) - sqlite3 (1.3.5) - sqlite3-ruby (1.3.3) - sqlite3 (>= 1.3.3) - thor (0.14.6) - tilt (1.3.3) - treetop (1.4.10) - polyglot - polyglot (>= 0.3.1) - tzinfo (0.3.31) - -PLATFORMS - ruby - -DEPENDENCIES - actionpack (>= 3.0.0) - generator_spec - jeweler - json_pure - rails (>= 3.0.0) - rspec (>= 2.6.0) - rspec-rails (>= 2.6.0) - simplecov - sqlite3-ruby diff --git a/Rakefile b/Rakefile index aa84d67..8b35f85 100644 --- a/Rakefile +++ b/Rakefile @@ -7,7 +7,7 @@ rescue LoadError end require 'rake' -require 'rake/rdoctask' +require 'rdoc/task' require 'rspec/core' require 'rspec/core/rake_task' diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index e5d7d90..c661a01 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -65,12 +65,22 @@ module Cocoon 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 = create_object(f, association) html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options)).html_safe link_to(name, '#', html_options ) end end + # creates new association object with its conditions, like + # `` has_many :admin_comments, class_name: "Comment", conditions: { author: "Admin" } + # will create new Comment with author "Admin" + + def create_object(f, association) + assoc = f.object.class.reflect_on_association(association) + conditions = assoc.conditions.flatten + new_object = assoc.klass.new(*conditions) + end + end end diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index dbdfbaf..40e7586 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -18,6 +18,13 @@ describe Cocoon do @tester.stub(:render_association).and_return('form') end + context "association with conditions" do + it "should create correct association" do + result = @tester.create_object(@form_obj, :admin_comments) + result.author.should == "Admin" + end + end + context "without a block" do it "should accept a name" do result = @tester.link_to_add_association('add something', @form_obj, :comments) diff --git a/spec/dummy/app/models/post.rb b/spec/dummy/app/models/post.rb index 2fccaf5..5d6bbd4 100644 --- a/spec/dummy/app/models/post.rb +++ b/spec/dummy/app/models/post.rb @@ -1,4 +1,5 @@ class Post < ActiveRecord::Base has_many :comments + has_many :admin_comments, class_name: "Comment", :conditions => { :author => "Admin" } has_many :people end From 3f5e89361f47be81ba6a358f5cc4e3a34ab08ff7 Mon Sep 17 00:00:00 2001 From: fl00r Date: Mon, 12 Mar 2012 23:30:11 +0400 Subject: [PATCH 2/2] Added custom partial feature --- lib/cocoon/view_helpers.rb | 23 +++++++++++++++++------ spec/cocoon_spec.rb | 25 +++++++++++++++++-------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index c661a01..1d30e31 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -31,10 +31,11 @@ module Cocoon end # :nodoc: - def render_association(association, f, new_object, render_options={}) + def render_association(association, f, new_object, render_options={}, custom_partial=nil) + partial = setup_partial(custom_partial, association) method_name = f.respond_to?(:semantic_fields_for) ? :semantic_fields_for : (f.respond_to?(:simple_fields_for) ? :simple_fields_for : :fields_for) f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder| - render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true) + render(partial, :f => builder, :dynamic => true) end end @@ -51,22 +52,24 @@ module Cocoon f = args[0] association = args[1] html_options = args[2] || {} + options = args[3] || {} link_to_add_association(capture(&block), f, association, html_options) else name = args[0] f = args[1] association = args[2] html_options = args[3] || {} + options = args[4] || {} - render_options = html_options.delete(:render_options) - render_options ||={} + render_options = html_options.delete(:render_options) + render_options ||= {} 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 = create_object(f, association) - html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options)).html_safe + html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options, options[:partial])).html_safe link_to(name, '#', html_options ) end @@ -75,12 +78,20 @@ module Cocoon # creates new association object with its conditions, like # `` has_many :admin_comments, class_name: "Comment", conditions: { author: "Admin" } # will create new Comment with author "Admin" - + def create_object(f, association) assoc = f.object.class.reflect_on_association(association) conditions = assoc.conditions.flatten new_object = assoc.klass.new(*conditions) end + def setup_partial(partial, association) + if partial + partial + else + association.to_s.singularize + "_fields" + end + end + end end diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 40e7586..962a898 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -18,13 +18,6 @@ describe Cocoon do @tester.stub(:render_association).and_return('form') end - context "association with conditions" do - it "should create correct association" do - result = @tester.create_object(@form_obj, :admin_comments) - result.author.should == "Admin" - end - end - context "without a block" do it "should accept a name" do result = @tester.link_to_add_association('add something', @form_obj, :comments) @@ -67,7 +60,7 @@ describe Cocoon do context "with extra render-options for rendering the child relation" do it "should use the correct plural" do - @tester.should_receive(:render_association).with(:people, @form_obj, anything, {:wrapper => 'inline'}) + @tester.should_receive(:render_association).with(:people, @form_obj, anything, {:wrapper => 'inline'}, nil) result = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline'}) result.to_s.should == 'add something' end @@ -140,6 +133,22 @@ describe Cocoon do result.to_s.should == "remove some long name" end end + + context "association with conditions" do + it "should create correct association" do + result = @tester.create_object(@form_obj, :admin_comments) + result.author.should == "Admin" + end + end + + context "should create proper partial" do + it "should create correct association" do + result = @tester.setup_partial(nil, :admin_comments) + result.should == "admin_comment_fields" + result = @tester.setup_partial("comment_fields", :admin_comments) + result.should == "comment_fields" + end + end end end