Merge pull request #45 from fl00r/master
Added support of "conditions" in associations
This commit is contained in:
commit
52525b0715
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,3 +7,4 @@ spec/dummy/tmp/
|
||||
.rvmrc
|
||||
.idea
|
||||
coverage/
|
||||
Gemfile.lock
|
||||
|
125
Gemfile.lock
125
Gemfile.lock
@ -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
|
2
Rakefile
2
Rakefile
@ -7,7 +7,7 @@ rescue LoadError
|
||||
end
|
||||
|
||||
require 'rake'
|
||||
require 'rake/rdoctask'
|
||||
require 'rdoc/task'
|
||||
|
||||
require 'rspec/core'
|
||||
require 'rspec/core/rake_task'
|
||||
|
@ -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,12 +52,14 @@ 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 ||= {}
|
||||
@ -65,12 +68,30 @@ 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
|
||||
html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options)).html_safe
|
||||
new_object = create_object(f, association)
|
||||
html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options, options[:partial])).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
|
||||
|
||||
def setup_partial(partial, association)
|
||||
if partial
|
||||
partial
|
||||
else
|
||||
association.to_s.singularize + "_fields"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -60,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 == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="form<tag>">add something</a>'
|
||||
end
|
||||
@ -133,6 +133,22 @@ describe Cocoon do
|
||||
result.to_s.should == "<input id=\"Post__destroy\" name=\"Post[_destroy]\" type=\"hidden\" /><a href=\"#\" class=\"add_some_class remove_fields dynamic\" data-something=\"bla\">remove some long name</a>"
|
||||
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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user