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
|
||||
@@cache_templates = true
|
||||
|
||||
mattr_accessor :include_json_root
|
||||
@@include_json_root = true
|
||||
|
||||
def configure
|
||||
yield self
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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 => [{
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue