Pulled in changes from zacstewart (pull #42), but with extra documentation and test.
This commit is contained in:
parent
5047cdf0f1
commit
ae21d63679
|
@ -156,6 +156,7 @@ It takes four parameters:
|
|||
- this setting still works but `data-association-insertion-method` takes precedence. may be removed in a future version.
|
||||
- `partial`: explicitly declare the name of the partial that will be used
|
||||
- `render_options` : options passed through to the form-builder function (e.g. `simple_fields_for`, `semantic_fields_for` or `fields_for`).
|
||||
If it contains a `:locals` option containing a hash, that is handed to the partial.
|
||||
|
||||
Optionally you could also leave out the name and supply a block that is captured to give the name (if you want to do something more complicated).
|
||||
|
||||
|
@ -168,6 +169,13 @@ be handed down as follows:
|
|||
= link_to_add_association 'add something', f, :something, :render_options => {:wrapper => 'inline' }
|
||||
````
|
||||
|
||||
If you want to specify locals that needed to handed down to the partial, write
|
||||
|
||||
````haml
|
||||
= link_to_add_association 'add something', f, :something, :render_options => {:locals => {:sherlock => 'Holmes' }}
|
||||
````
|
||||
|
||||
|
||||
#### :partial
|
||||
|
||||
To overrule the default partial name, e.g. because it shared between multiple views, write
|
||||
|
|
|
@ -33,9 +33,11 @@ module Cocoon
|
|||
# :nodoc:
|
||||
def render_association(association, f, new_object, render_options={}, custom_partial=nil)
|
||||
partial = setup_partial(custom_partial, association)
|
||||
locals = render_options.delete(:locals)
|
||||
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(partial, :f => builder, :dynamic => true)
|
||||
partial_options = {:f => builder, :dynamic => true}.merge(locals)
|
||||
render(partial, partial_options)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -46,6 +48,7 @@ module Cocoon
|
|||
# - *association* : the associated objects, e.g. :tasks, this should be the name of the <tt>has_many</tt> relation.
|
||||
# - *html_options*: html options to be passed to <tt>link_to</tt> (see <tt>link_to</tt>)
|
||||
# - *:render_options* : options passed to `simple_fields_for, semantic_fields_for or fields_for`
|
||||
# - *:locals* : the locals hash in the :render_options is handed to the partial
|
||||
# - *:partial* : explicitly override the default partial name
|
||||
# - *&block*: see <tt>link_to</tt>
|
||||
|
||||
|
|
|
@ -73,6 +73,18 @@ describe Cocoon do
|
|||
end
|
||||
end
|
||||
|
||||
context "with extra render-options to pass locals to the partial" do
|
||||
it "uses the correct plural" do
|
||||
@tester.unstub(:render_association)
|
||||
@form_obj.should_receive(:fields_for) { | association, new_object, options_hash, &block| block.call }
|
||||
@tester.should_receive(:render).with("person_fields", {:f=>nil, :dynamic=>true, :alfred=>"Judoka"}).and_return ("partiallll")
|
||||
result = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline', :locals => {:alfred => 'Judoka'}})
|
||||
result.to_s.should == '<a href="#" class="add_fields" data-association="person" data-associations="people" data-template="partiallll">add something</a>'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
context "when using formtastic" do
|
||||
before(:each) do
|
||||
@tester.unstub(:render_association)
|
||||
|
|
Loading…
Reference in New Issue