Don't need to retrieve data variable from context

since it's has already been copied from assigns.

Modify test Context class to reflect this change
This commit is contained in:
ccocchi 2012-07-25 18:23:41 +02:00
parent 36fe3ec56e
commit 0034e05486
4 changed files with 15 additions and 12 deletions

View File

@ -18,7 +18,7 @@ module RablRails
# method defined by the renderer.
#
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) :
render_resource(collection_or_resource, template.source)
options[:root_name] = template.root_name
@ -54,7 +54,7 @@ module RablRails
when Hash
current_value = value.dup
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
current_value.each_pair { |k, v|

View File

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

View File

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

View File

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