diff --git a/lib/rabl-rails/renderers/base.rb b/lib/rabl-rails/renderers/base.rb index c96ce02..e053a19 100644 --- a/lib/rabl-rails/renderers/base.rb +++ b/lib/rabl-rails/renderers/base.rb @@ -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| diff --git a/test/deep_nesting_test.rb b/test/deep_nesting_test.rb index ac9265a..e689233 100644 --- a/test/deep_nesting_test.rb +++ b/test/deep_nesting_test.rb @@ -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 diff --git a/test/renderers/json_renderer_test.rb b/test/renderers/json_renderer_test.rb index 8b376d5..c04052d 100644 --- a/test/renderers/json_renderer_test.rb +++ b/test/renderers/json_renderer_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 1a054dc..26b144e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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