Add partial and root for child nodes
This commit is contained in:
parent
1a033a60c2
commit
0011b57f8f
|
@ -5,6 +5,7 @@ require 'active_support/json'
|
|||
require 'active_support/core_ext/class/attribute_accessors'
|
||||
|
||||
require 'rabl-fast-json/version'
|
||||
require 'rabl-fast-json/helpers'
|
||||
require 'rabl-fast-json/template'
|
||||
require 'rabl-fast-json/compiler'
|
||||
require 'rabl-fast-json/library'
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
module RablFastJson
|
||||
class Compiler
|
||||
include Helpers
|
||||
|
||||
def initialize(context = nil)
|
||||
@context = context
|
||||
|
@ -28,10 +29,15 @@ module RablFastJson
|
|||
alias_method :attributes, :attribute
|
||||
|
||||
def child(name_or_data, options = {}, &block)
|
||||
return unless block_given?
|
||||
data, name = extract_data_and_name(name_or_data)
|
||||
name = options[:root] if root_given?(options)
|
||||
if partial_given?(options)
|
||||
template = Library.instance.get(options[:partial], @context)
|
||||
@template[name] = template.merge!(:_data => data)
|
||||
else
|
||||
_compile_sub_template(name, data, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def glue(data, &block)
|
||||
return unless block_given?
|
||||
|
@ -46,6 +52,7 @@ module RablFastJson
|
|||
alias_method :code, :node
|
||||
|
||||
def collection(data, options = {})
|
||||
@template.root_name = options[:root] if root_given?(options)
|
||||
object(data)
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
module RablFastJson
|
||||
module Helpers
|
||||
def root_given?(options)
|
||||
options[:root].present?
|
||||
end
|
||||
|
||||
def partial_given?(options)
|
||||
options[:partial].present?
|
||||
end
|
||||
end
|
||||
end
|
|
@ -4,6 +4,8 @@ module RablFastJson
|
|||
class Library
|
||||
include Singleton
|
||||
|
||||
attr_accessor :view_renderer
|
||||
|
||||
def initialize
|
||||
@cached_templates = {}
|
||||
end
|
||||
|
|
|
@ -43,11 +43,27 @@ class CompilerTest < ActiveSupport::TestCase
|
|||
assert_equal({ :bar => { :_data => :address, :foo => :foo } }, t.source)
|
||||
end
|
||||
|
||||
test "child with root name defined as option" do
|
||||
t = @compiler.compile_source(%{ child(:user, :root => :author) do attributes :foo end })
|
||||
assert_equal({ :author => { :_data => :user, :foo => :foo } }, t.source)
|
||||
end
|
||||
|
||||
test "child with arbitrary source store the data with the template" do
|
||||
t = @compiler.compile_source(%{ child :@user => :author do attribute :name end })
|
||||
assert_equal({ :author => { :_data => :@user, :name => :name } }, t.source)
|
||||
end
|
||||
|
||||
test "child with succint partial notation" do
|
||||
@view_renderer = mock()
|
||||
@view_renderer.stub_chain(:lookup_context, :find_template).with('users/base', [], false).and_return(
|
||||
mock(:source => %{ attribute :id }))
|
||||
RablFastJson::Library.reset_instance
|
||||
RablFastJson::Library.instance.view_renderer = @view_renderer
|
||||
|
||||
t = @compiler.compile_source(%{child(:user, :partial => 'users/base') })
|
||||
assert_equal( {:user => { :_data => :user, :id => :id } }, t.source)
|
||||
end
|
||||
|
||||
test "glue is compiled as a child but with anonymous name" do
|
||||
t = @compiler.compile_source(%{ glue(:@user) do attribute :name end })
|
||||
assert_equal({ :_glue0 => { :_data => :@user, :name => :name } }, t.source)
|
||||
|
@ -89,6 +105,7 @@ class CompilerTest < ActiveSupport::TestCase
|
|||
|
||||
test "extends use other template source as itself" do
|
||||
template = mock('template', :source => { :id => :id })
|
||||
RablFastJson::Library.reset_instance
|
||||
RablFastJson::Library.instance.stub(:get).with('users/base', @context).and_return(template)
|
||||
t = @compiler.compile_source(%{ extends 'users/base' })
|
||||
assert_equal({ :id => :id }, t.source)
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
# Configure Rails Environment
|
||||
ENV["RAILS_ENV"] = "test"
|
||||
$:.unshift File.expand_path('../../lib', __FILE__)
|
||||
|
||||
|
|
Loading…
Reference in New Issue