Skip to content

Commit

Permalink
[partials] Fix issue with child nil case on extends
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquena committed Oct 30, 2012
1 parent 7e94c23 commit ef781e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 0.7.6 (unreleased)

* Fix render behavior by separating data_object and data_name in engine
* Fix regression with 'child' behavior with nil on extends (with tests)

## 0.7.5

Expand Down
1 change: 1 addition & 0 deletions lib/rabl/partials.rb
Expand Up @@ -21,6 +21,7 @@ def partial(file, options={}, &block)
# options must have :source (rabl file contents)
# options can have :source_location (source filename)
def object_to_hash(object, options={}, &block)
return object if object.nil?
return [] if is_collection?(object) && object.blank? # empty collection
engine_options = options.reverse_merge(:format => "hash", :view_path => @_view_path, :root => (options[:root] || false))
Rabl::Engine.new(options[:source], engine_options).render(@_scope, :object => object, &block)
Expand Down
33 changes: 19 additions & 14 deletions test/renderer_test.rb
Expand Up @@ -132,31 +132,36 @@


asserts 'handles extends with child as nil' do
File.open(tmp_path + "test.json.rabl", "w") do |f|
File.open(tmp_path + "foo.json.rabl", "w") do |f|
f.puts %q{
attributes :age
node(:foo) { |f| f.bar }
object @foo
node(:test) do |foo|
{
test: "#{foo.attribute}"
}
end
}
end

File.open(tmp_path + "user.json.rabl", "w") do |f|
File.open(tmp_path + "bar.json.rabl", "w") do |f|
f.puts %q{
object @user
attributes :age
child @foo do
node(:bar) { |f| f.demo }
extends 'test'
object false
node do
{
test_attribute: 'test_value'
}
end
child(@foos => :foo_collection) do
extends 'foo'
end
}
end

sc = Object.new
sc.instance_variable_set :@user, nil
sc.instance_variable_set :@foo, nil
user = User.new(:name => 'irvine')
renderer = Rabl::Renderer.new('user', user, :view_path => tmp_path, :scope => sc)
sc.instance_variable_set :@foos, nil
renderer = Rabl::Renderer.new('bar', false, :view_path => tmp_path, :scope => sc)
JSON.parse(renderer.render)
end.equals JSON.parse("{\"user\":{\"age\":24}}")
end.equals JSON.parse("{\"test_attribute\":\"test_value\", \"foo_collection\":null}")

asserts 'handles extends with custom node and object set false' do
File.open(tmp_path + "test.json.rabl", "w") do |f|
Expand Down

0 comments on commit ef781e0

Please sign in to comment.