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