Add include_json_root to configuration.
This commit is contained in:
parent
57366b7fd6
commit
b0d83f444b
|
@ -22,6 +22,9 @@ module RablRails
|
||||||
mattr_accessor :cache_templates
|
mattr_accessor :cache_templates
|
||||||
@@cache_templates = true
|
@@cache_templates = true
|
||||||
|
|
||||||
|
mattr_accessor :include_json_root
|
||||||
|
@@include_json_root = true
|
||||||
|
|
||||||
def configure
|
def configure
|
||||||
yield self
|
yield self
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,21 +25,10 @@ module RablRails
|
||||||
# object :@user, :root => :author
|
# object :@user, :root => :author
|
||||||
#
|
#
|
||||||
def object(data, options = {})
|
def object(data, options = {})
|
||||||
data, name = extract_data_and_name(data)
|
@template.data, @template.root_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.root_name = options[:root] if options[:root]
|
@template.root_name = options[:root] if options[:root]
|
||||||
end
|
end
|
||||||
|
alias_method :collection, :object
|
||||||
|
|
||||||
#
|
#
|
||||||
# Includes the attribute or method in the output
|
# Includes the attribute or method in the output
|
||||||
|
@ -136,11 +125,8 @@ module RablRails
|
||||||
def extract_data_and_name(name_or_data)
|
def extract_data_and_name(name_or_data)
|
||||||
case name_or_data
|
case name_or_data
|
||||||
when Symbol
|
when Symbol
|
||||||
if name_or_data.to_s.start_with?('@')
|
str = name_or_data.to_s
|
||||||
[name_or_data, nil]
|
str.start_with?('@') ? [name_or_data, str[1..-1]] : [name_or_data, name_or_data]
|
||||||
else
|
|
||||||
[name_or_data, name_or_data]
|
|
||||||
end
|
|
||||||
when Hash
|
when Hash
|
||||||
name_or_data.first
|
name_or_data.first
|
||||||
else
|
else
|
||||||
|
|
|
@ -3,9 +3,11 @@ module RablRails
|
||||||
class PartialError < StandardError; end
|
class PartialError < StandardError; end
|
||||||
|
|
||||||
class Base
|
class Base
|
||||||
|
attr_accessor :options
|
||||||
|
|
||||||
def initialize(context) # :nodoc:
|
def initialize(context) # :nodoc:
|
||||||
@_context = context
|
@_context = context
|
||||||
|
@options = {}
|
||||||
setup_render_context
|
setup_render_context
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -19,7 +21,7 @@ module RablRails
|
||||||
collection_or_resource = @_context.instance_variable_get(template.data) if template.data
|
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) :
|
output_hash = collection_or_resource.respond_to?(:each) ? render_collection(collection_or_resource, template.source) :
|
||||||
render_resource(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)
|
format_output(output_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ module RablRails
|
||||||
module Renderers
|
module Renderers
|
||||||
class JSON < Base
|
class JSON < Base
|
||||||
def format_output(hash)
|
def format_output(hash)
|
||||||
|
hash = { options[:root_name] => hash } if options[:root_name] && RablRails.include_json_root
|
||||||
ActiveSupport::JSON.encode(hash)
|
ActiveSupport::JSON.encode(hash)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -149,4 +149,10 @@ class CompilerTest < ActiveSupport::TestCase
|
||||||
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
|
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
|
||||||
assert_equal false, t.data
|
assert_equal false, t.data
|
||||||
end
|
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
|
end
|
|
@ -40,7 +40,7 @@ class DeepNestingTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_equal(ActiveSupport::JSON.encode({
|
assert_equal(ActiveSupport::JSON.encode(:user => {
|
||||||
:id => 1,
|
:id => 1,
|
||||||
:name => 'foobar',
|
:name => 'foobar',
|
||||||
:posts => [{
|
:posts => [{
|
||||||
|
|
|
@ -115,4 +115,17 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
||||||
|
|
||||||
assert_equal %q({"users":[]}), render_json_output
|
assert_equal %q({"users":[]}), render_json_output
|
||||||
end
|
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
|
end
|
Loading…
Reference in New Issue