diff --git a/README.markdown b/README.markdown index 235ed2a..3811e15 100644 --- a/README.markdown +++ b/README.markdown @@ -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 diff --git a/lib/cocoon/view_helpers.rb b/lib/cocoon/view_helpers.rb index d20f88d..9dff70e 100644 --- a/lib/cocoon/view_helpers.rb +++ b/lib/cocoon/view_helpers.rb @@ -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 has_many relation. # - *html_options*: html options to be passed to link_to (see link_to) # - *: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 link_to diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 818bc79..e20cf14 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -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 == 'add something' + end + end + + + context "when using formtastic" do before(:each) do @tester.unstub(:render_association)