Permalink
Browse files

locals should take extra keys more than just object.

  • Loading branch information...
siong1987 committed Jul 27, 2012
1 parent a714fdd commit dbcd85a9ca47cf9ea8cd10c10aedf3ee03ae2bd5
Showing with 10 additions and 2 deletions.
  1. +7 −0 lib/rabl/renderer.rb
  2. +3 −2 test/renderer_test.rb
View
@@ -31,6 +31,13 @@ def initialize(source, object = nil, options = {})
@options = options
@object = object
+
+ if @options[:locals]
+ @options[:locals].delete(:object) if @object
+ @options[:locals].each do |k,v|
+ instance_variable_set(:"@#{k}", v)
+ end
+ end
engine.source = self.process_source(source)
end
View
@@ -61,13 +61,14 @@
asserts 'passes :locals to render' do
source = %q{
attribute :name, :as => 'city'
+ node(:zipcode) { @zipcode }
}
user = User.new(:name => 'irvine')
- renderer = Rabl::Renderer.new(source, nil, { :format => 'json', :locals => {:object => user} })
+ renderer = Rabl::Renderer.new(source, nil, { :format => 'json', :locals => {:object => user, :zipcode => "92602"} })
renderer.render.split("").sort
- end.equals "{\"user\":{\"city\":\"irvine\"}}".split("").sort
+ end.equals "{\"user\":{\"city\":\"irvine\",\"zipcode\":\"92602\"}}".split("").sort
asserts 'loads source from file' do
File.open(tmp_path + "test.json.rabl", "w") do |f|

0 comments on commit dbcd85a

Please sign in to comment.