Skip to content

Commit

Permalink
Accept String value for render_partial :as option
Browse files Browse the repository at this point in the history
[#6222 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information
amatsuda authored and spastorino committed Feb 1, 2011
1 parent 6bd9fac commit 5dd803e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/partials.rb
Expand Up @@ -40,7 +40,7 @@ module ActionView
# With the <tt>:as</tt> option we can specify a different name for said local variable. For example, if we
# wanted it to be +agreement+ instead of +contract+ we'd do:
#
# <%= render :partial => "contract", :as => :agreement %>
# <%= render :partial => "contract", :as => 'agreement' %>
#
# The <tt>:object</tt> option can be used to directly specify which object is rendered into the partial;
# useful when the template's object is elsewhere, in a different ivar or in a local variable for instance.
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/renderer/partial_renderer.rb
Expand Up @@ -108,7 +108,7 @@ def find_partial
locals << @variable_counter if @collection
find_template(path, locals)
end
end
end

def find_template(path=@path, locals=@locals.keys)
prefixes = path.include?(?/) ? [] : @view.controller_prefixes
Expand Down Expand Up @@ -159,7 +159,7 @@ def partial_path(object = @object)
end

def retrieve_variable(path)
variable = @options[:as] || path[%r'_?(\w+)(\.\w+)*$', 1].to_sym
variable = @options[:as].try(:to_sym) || path[%r'_?(\w+)(\.\w+)*$', 1].to_sym
variable_counter = :"#{variable}_counter" if @collection
[variable, variable_counter]
end
Expand Down
7 changes: 6 additions & 1 deletion actionpack/test/template/render_test.rb
Expand Up @@ -146,7 +146,12 @@ def test_render_partial_collection
assert_equal "Hello: davidHello: mary", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), Customer.new("mary") ])
end

def test_render_partial_collection_as
def test_render_partial_collection_as_by_string
assert_equal "david david davidmary mary mary",
@view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => 'customer')
end

def test_render_partial_collection_as_by_symbol
assert_equal "david david davidmary mary mary",
@view.render(:partial => "test/customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer)
end
Expand Down

0 comments on commit 5dd803e

Please sign in to comment.