diff --git a/lib/rabl-fast-json.rb b/lib/rabl-fast-json.rb index 5214e6e..408a864 100644 --- a/lib/rabl-fast-json.rb +++ b/lib/rabl-fast-json.rb @@ -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' diff --git a/lib/rabl-fast-json/compiler.rb b/lib/rabl-fast-json/compiler.rb index 4653712..dd0be68 100644 --- a/lib/rabl-fast-json/compiler.rb +++ b/lib/rabl-fast-json/compiler.rb @@ -1,5 +1,6 @@ module RablFastJson class Compiler + include Helpers def initialize(context = nil) @context = context @@ -28,9 +29,14 @@ 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) - _compile_sub_template(name, data, &block) + 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) @@ -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 diff --git a/lib/rabl-fast-json/helpers.rb b/lib/rabl-fast-json/helpers.rb new file mode 100644 index 0000000..53b21b6 --- /dev/null +++ b/lib/rabl-fast-json/helpers.rb @@ -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 \ No newline at end of file diff --git a/lib/rabl-fast-json/library.rb b/lib/rabl-fast-json/library.rb index aa67ab9..cb68fcc 100644 --- a/lib/rabl-fast-json/library.rb +++ b/lib/rabl-fast-json/library.rb @@ -4,6 +4,8 @@ module RablFastJson class Library include Singleton + attr_accessor :view_renderer + def initialize @cached_templates = {} end diff --git a/test/compiler_test.rb b/test/compiler_test.rb index 7cb0926..0265775 100644 --- a/test/compiler_test.rb +++ b/test/compiler_test.rb @@ -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) diff --git a/test/test_helper.rb b/test/test_helper.rb index 4d1ad71..33e227d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,3 @@ -# Configure Rails Environment ENV["RAILS_ENV"] = "test" $:.unshift File.expand_path('../../lib', __FILE__)