From 36fe3ec56ef415cfe575e5dc0274b8056996ab75 Mon Sep 17 00:00:00 2001 From: ccocchi Date: Tue, 24 Jul 2012 18:29:03 +0200 Subject: [PATCH 1/2] Remove test against undefined variable --- lib/rabl-rails/renderers/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rabl-rails/renderers/base.rb b/lib/rabl-rails/renderers/base.rb index 2d04abd..c96ce02 100644 --- a/lib/rabl-rails/renderers/base.rb +++ b/lib/rabl-rails/renderers/base.rb @@ -108,7 +108,7 @@ module RablRails # def setup_render_context @_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 From 0034e054868451eb510e02ec1822ee7a67248dfc Mon Sep 17 00:00:00 2001 From: ccocchi Date: Wed, 25 Jul 2012 18:23:41 +0200 Subject: [PATCH 2/2] 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 --- lib/rabl-rails/renderers/base.rb | 4 ++-- test/deep_nesting_test.rb | 5 ++--- test/renderers/json_renderer_test.rb | 9 ++++----- test/test_helper.rb | 9 +++++++-- 4 files changed, 15 insertions(+), 12 deletions(-) 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