Permalink
Browse files

Dropifies also array elements

  • Loading branch information...
1 parent cb0ac80 commit 037d21ba73461db0792c40999c6ff307f65a3fc6 @madadam committed Mar 4, 2010
Showing with 23 additions and 4 deletions.
  1. +11 −4 lib/liquidizer/controller_extensions.rb
  2. +12 −0 test/controller_extensions_test.rb
View
15 lib/liquidizer/controller_extensions.rb
@@ -130,8 +130,7 @@ def assigns_for_liquify
assign_name = name[/^@(.*)$/, 1] # strip @
next memo if assign_name.starts_with?('_') # skip "private" ivars
- value = instance_variable_get(name)
- value = dropify(value) unless value.respond_to?(:to_liquid)
+ value = dropify(instance_variable_get(name))
memo[assign_name] = value if value
memo
@@ -142,8 +141,16 @@ def assigns_for_liquify
#
# Foo::Bar -> Foo::BarDrop
def dropify(value)
- drop_class = infer_drop_class(value)
- drop_class && drop_class.new(value)
+ if value.respond_to?(:to_liquid)
+ if value.is_a?(Array)
+ value.map { |element| dropify(element) }
+ else
+ value
+ end
+ else
+ drop_class = infer_drop_class(value)
+ drop_class && drop_class.new(value)
+ end
end
def infer_drop_class(value)
View
12 test/controller_extensions_test.rb
@@ -15,6 +15,7 @@ class PostsController < BaseController
def index
@title = 'Hello blog!'
+ @posts = [Post.new(:title => 'First post'), Post.new(:title => 'Second post')]
end
def show
@@ -229,6 +230,17 @@ def teardown
get :show
assert_select 'h1 strong', 'Liquidizer is awesome!'
end
+
+ test 'dropifies array elements' do
+ setup_controller(PostsController)
+
+ LiquidTemplate.create!(:name => 'posts/index',
+ :content => '{% for post in posts %} {{ post.title }} {% endfor %}')
+
+ get :index
+ assert_select 'em', 'First post'
+ assert_select 'em', 'Second post'
+ end
private

0 comments on commit 037d21b

Please sign in to comment.