From b0d83f444b21f98e68ae0f57302ee1088a1b573a Mon Sep 17 00:00:00 2001 From: ccocchi Date: Fri, 13 Jul 2012 19:15:04 +0200 Subject: [PATCH] Add include_json_root to configuration. --- lib/rabl-rails.rb | 3 +++ lib/rabl-rails/compiler.rb | 22 ++++------------------ lib/rabl-rails/renderers/base.rb | 4 +++- lib/rabl-rails/renderers/json.rb | 1 + test/compiler_test.rb | 6 ++++++ test/deep_nesting_test.rb | 2 +- test/renderers/json_renderer_test.rb | 13 +++++++++++++ 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/rabl-rails.rb b/lib/rabl-rails.rb index 77c6c4c..289746b 100644 --- a/lib/rabl-rails.rb +++ b/lib/rabl-rails.rb @@ -21,6 +21,9 @@ module RablRails mattr_accessor :cache_templates @@cache_templates = true + + mattr_accessor :include_json_root + @@include_json_root = true def configure yield self diff --git a/lib/rabl-rails/compiler.rb b/lib/rabl-rails/compiler.rb index 6f1fd83..de7a6eb 100644 --- a/lib/rabl-rails/compiler.rb +++ b/lib/rabl-rails/compiler.rb @@ -25,21 +25,10 @@ module RablRails # object :@user, :root => :author # def object(data, options = {}) - data, name = extract_data_and_name(data) - @template.data = data - @template.root_name = options[:root] || name - end - - # - # Sets a collection to be used as data for the template - # Example: - # collection :@users - # collection :@users, :root => :morons - # - def collection(data, options = {}) - object(data) + @template.data, @template.root_name = extract_data_and_name(data) @template.root_name = options[:root] if options[:root] end + alias_method :collection, :object # # Includes the attribute or method in the output @@ -136,11 +125,8 @@ module RablRails def extract_data_and_name(name_or_data) case name_or_data when Symbol - if name_or_data.to_s.start_with?('@') - [name_or_data, nil] - else - [name_or_data, name_or_data] - end + str = name_or_data.to_s + str.start_with?('@') ? [name_or_data, str[1..-1]] : [name_or_data, name_or_data] when Hash name_or_data.first else diff --git a/lib/rabl-rails/renderers/base.rb b/lib/rabl-rails/renderers/base.rb index 6eebe49..e89b342 100644 --- a/lib/rabl-rails/renderers/base.rb +++ b/lib/rabl-rails/renderers/base.rb @@ -3,9 +3,11 @@ module RablRails class PartialError < StandardError; end class Base + attr_accessor :options def initialize(context) # :nodoc: @_context = context + @options = {} setup_render_context end @@ -19,7 +21,7 @@ module RablRails collection_or_resource = @_context.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) - output_hash = { template.root_name => output_hash } if template.root_name + options[:root_name] = template.root_name format_output(output_hash) end diff --git a/lib/rabl-rails/renderers/json.rb b/lib/rabl-rails/renderers/json.rb index f903476..29f528c 100644 --- a/lib/rabl-rails/renderers/json.rb +++ b/lib/rabl-rails/renderers/json.rb @@ -2,6 +2,7 @@ module RablRails module Renderers class JSON < Base def format_output(hash) + hash = { options[:root_name] => hash } if options[:root_name] && RablRails.include_json_root ActiveSupport::JSON.encode(hash) end end diff --git a/test/compiler_test.rb b/test/compiler_test.rb index 944fc56..277504a 100644 --- a/test/compiler_test.rb +++ b/test/compiler_test.rb @@ -149,4 +149,10 @@ class CompilerTest < ActiveSupport::TestCase assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source) assert_equal false, t.data end + + test "name extraction from argument" do + assert_equal [:@users, 'users'], @compiler.send(:extract_data_and_name, :@users) + assert_equal [:users, :users], @compiler.send(:extract_data_and_name, :users) + assert_equal [:@users, :authors], @compiler.send(:extract_data_and_name, :@users => :authors) + end end \ No newline at end of file diff --git a/test/deep_nesting_test.rb b/test/deep_nesting_test.rb index 7fc8bed..7aac9e8 100644 --- a/test/deep_nesting_test.rb +++ b/test/deep_nesting_test.rb @@ -40,7 +40,7 @@ class DeepNestingTest < ActiveSupport::TestCase end } - assert_equal(ActiveSupport::JSON.encode({ + assert_equal(ActiveSupport::JSON.encode(:user => { :id => 1, :name => 'foobar', :posts => [{ diff --git a/test/renderers/json_renderer_test.rb b/test/renderers/json_renderer_test.rb index 84a508f..aa62713 100644 --- a/test/renderers/json_renderer_test.rb +++ b/test/renderers/json_renderer_test.rb @@ -115,4 +115,17 @@ class TestJsonRenderer < ActiveSupport::TestCase assert_equal %q({"users":[]}), render_json_output end + + test "render object with root node" do + @template.root_name = :author + @template.source = { :id => :id, :name => :name } + assert_equal %q({"author":{"id":1,"name":"foobar"}}), render_json_output + end + + test "render object with root options set to false" do + RablRails.include_json_root = false + @template.root_name = :author + @template.source = { :id => :id, :name => :name } + assert_equal %q({"id":1,"name":"foobar"}), render_json_output + end end \ No newline at end of file