Merge branch 'master' into render-method

This commit is contained in:
ccocchi 2012-07-25 18:37:40 +02:00
commit 63b50b2a31
4 changed files with 16 additions and 13 deletions

View File

@ -18,7 +18,7 @@ module RablRails
# method defined by the renderer. # method defined by the renderer.
# #
def render(template) def render(template)
collection_or_resource = @_context.instance_variable_get(template.data) if template.data collection_or_resource = instance_variable_get(template.data) if template.data
output_hash = collection_or_resource.respond_to?(:each) ? render_collection(collection_or_resource, template.source) : output_hash = collection_or_resource.respond_to?(:each) ? render_collection(collection_or_resource, template.source) :
render_resource(collection_or_resource, template.source) render_resource(collection_or_resource, template.source)
options[:root_name] = template.root_name options[:root_name] = template.root_name
@ -54,7 +54,7 @@ module RablRails
when Hash when Hash
current_value = value.dup current_value = value.dup
data_symbol = current_value.delete(:_data) data_symbol = current_value.delete(:_data)
object = data_symbol.nil? ? data : data_symbol.to_s.start_with?('@') ? @_context.instance_variable_get(data_symbol) : data.send(data_symbol) object = data_symbol.nil? ? data : data_symbol.to_s.start_with?('@') ? instance_variable_get(data_symbol) : data.send(data_symbol)
if key.to_s.start_with?('_') # glue if key.to_s.start_with?('_') # glue
current_value.each_pair { |k, v| current_value.each_pair { |k, v|
@ -108,7 +108,7 @@ module RablRails
# #
def setup_render_context def setup_render_context
@_context.instance_variable_get(:@_assigns).each_pair { |k, v| @_context.instance_variable_get(:@_assigns).each_pair { |k, v|
instance_variable_set("@#{k}", v) unless k.start_with?('_') || k == @data instance_variable_set("@#{k}", v) unless k.start_with?('_')
} }
end end
end end

View File

@ -22,9 +22,8 @@ class DeepNestingTest < ActiveSupport::TestCase
@user.stub(:respond_to?).with(:each).and_return(false) @user.stub(:respond_to?).with(:each).and_return(false)
@context = Context.new @context = Context.new
@context.stub(:instance_variable_get).with(:@user).and_return(@user) @context.assigns['user'] = @user
@context.stub(:instance_variable_get).with(:@virtual_path).and_return('users/show') @context.virtual_path = 'users/show'
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
@context.stub(:lookup_context).and_return(mock(:find_template => mock(:source => %{ object :@comment\n attribute :content }))) @context.stub(:lookup_context).and_return(mock(:find_template => mock(:source => %{ object :@comment\n attribute :content })))
end end

View File

@ -7,8 +7,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
@data.stub(:respond_to?).with(:each).and_return(false) @data.stub(:respond_to?).with(:each).and_return(false)
@context = Context.new @context = Context.new
@context.stub(:instance_variable_get).with(:@data).and_return(@data) @context.assigns['data'] = @data
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
@template = RablRails::CompiledTemplate.new @template = RablRails::CompiledTemplate.new
@template.data = :@data @template.data = :@data
@ -24,7 +23,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
end end
test "render collection with empty template" do test "render collection with empty template" do
@context.stub(:instance_variable_get).with(:@data).and_return([@data]) @context.assigns['data'] = [@data]
@template.source = {} @template.source = {}
assert_equal %q([{}]), render_json_output assert_equal %q([{}]), render_json_output
end end
@ -52,7 +51,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
test "render collection with attributes" do test "render collection with attributes" do
@data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')] @data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')]
@context.stub(:instance_variable_get).with(:@data).and_return(@data) @context.assigns['data'] = @data
@template.source = { :uid => :id, :name => :name, :gender => :sex } @template.source = { :uid => :id, :name => :name, :gender => :sex }
assert_equal %q([{"uid":1,"name":"foo","gender":"male"},{"uid":2,"name":"bar","gender":"female"}]), render_json_output assert_equal %q([{"uid":1,"name":"foo","gender":"male"},{"uid":2,"name":"bar","gender":"female"}]), render_json_output
end end
@ -87,8 +86,8 @@ class TestJsonRenderer < ActiveSupport::TestCase
test "partial should be evaluated at rendering time" do test "partial should be evaluated at rendering time" do
# Set assigns # Set assigns
@context.stub(:instance_variable_get).with(:@_assigns).and_return({'user' => @data})
@data.stub(:respond_to?).with(:empty?).and_return(false) @data.stub(:respond_to?).with(:empty?).and_return(false)
@context.assigns['user'] = @data
# Stub Library#get # Stub Library#get
t = RablRails::CompiledTemplate.new t = RablRails::CompiledTemplate.new

View File

@ -31,10 +31,15 @@ module ActiveSupport
end end
class Context class Context
attr_accessor :virtual_path attr_writer :virtual_path
def initialize def initialize
@_assigns = {} @_assigns = {}
@virtual_path = nil
end
def assigns
@_assigns
end end
def params def params