Add include_json_root to configuration.

This commit is contained in:
ccocchi 2012-07-13 19:15:04 +02:00
parent 57366b7fd6
commit b0d83f444b
7 changed files with 31 additions and 20 deletions

View File

@ -22,6 +22,9 @@ module RablRails
mattr_accessor :cache_templates
@@cache_templates = true
mattr_accessor :include_json_root
@@include_json_root = true
def configure
yield self
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 => [{

View File

@ -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