Skip to content

Commit

Permalink
add support for alternate names in render_associated_form
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.jamesgolick.com/attribute_fu/trunk@37 80b79608-713f-0410-8737-d8c0d0c1b50c
  • Loading branch information
james committed Nov 29, 2007
1 parent 5fab1cf commit cce07bd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/attribute_fu/associated_form_helper.rb
Expand Up @@ -45,16 +45,17 @@ def add_associated_link(name, object, opts = {})
end
end

def render_associated_form(associated, fields_for_args = {}, render_args = {})
def render_associated_form(associated, args = {})
associated = associated.is_a?(Array) ? associated : [associated] # preserve association proxy if this is one

unless associated.empty?
associated_name = associated.first.class.name.underscore

render_args.symbolize_keys!
args.symbolize_keys!
partial = args[:partial] || associated.first.class.name.underscore
local_assign_name = args[:partial] ? partial.split('/').last.split('.').first : associated.first.class.name.underscore

associated.map do |element|
fields_for_associated(element, fields_for_args) do |f|
@template.render({:partial => "#{associated_name}", :locals => {associated_name.to_sym => element, :f => f}.merge(render_args.delete(:locals) || {})}.merge(render_args))
fields_for_associated(element, args[:fields_for]) do |f|
@template.render({:partial => "#{partial}", :locals => {local_assign_name.to_sym => element, :f => f}.merge(args[:locals] || {})}.merge(args[:render] || {}))
end
end
end
Expand Down
43 changes: 42 additions & 1 deletion test/test/unit/associated_form_helper_test.rb
Expand Up @@ -150,7 +150,6 @@ def setup

_erbout = ''
fields_for(:photo) do |f|
# self.stubs(:render).with(:partial => "some_other_partial", :locals => {:comment => comment, :f => f}) # which object am I really supposed to mock here????
f.stubs(:render_associated_form).with(comment, :javascript => true)
_erbout.concat f.add_associated_link("Add Comment", comment, :container => 'something_comments', :partial => 'some_other_partial')
end
Expand Down Expand Up @@ -183,6 +182,48 @@ def setup
end
end

context "render_associated_form" do
setup do
comment = @photo.comments.build

associated_form_builder = mock()

_erbout = ''
fields_for(:photo) do |f|
f.stubs(:fields_for_associated).yields(associated_form_builder)
expects(:render).with(:partial => "comment", :locals => { :comment => comment, :f => associated_form_builder })
_erbout.concat f.render_associated_form(comment).to_s
end

@erbout = _erbout
end

should "extract the correct parameters for render" do
# assertions in mock
end
end

context "render_associated_form with specified partial name" do
setup do
comment = @photo.comments.build

associated_form_builder = mock()

_erbout = ''
fields_for(:photo) do |f|
f.stubs(:fields_for_associated).yields(associated_form_builder)
expects(:render).with(:partial => "somewhere/something.html.erb", :locals => { :something => comment, :f => associated_form_builder })
_erbout.concat f.render_associated_form(comment, :partial => "somewhere/something.html.erb").to_s
end

@erbout = _erbout
end

should "extract the correct parameters for render" do
# assertions in mock
end
end

private
def assoc_output(comment, &block)
_erbout = ''
Expand Down

0 comments on commit cce07bd

Please sign in to comment.