Enable non restful template

This commit is contained in:
ccocchi 2012-03-15 15:12:52 +01:00
parent 0c0ec9f23f
commit dfbe769a91
6 changed files with 63 additions and 6 deletions

View File

@ -72,6 +72,7 @@ module RablFastJson
end end
def object(data) def object(data)
return if data === false
data, name = extract_data_and_name(data) data, name = extract_data_and_name(data)
@template.data, @template.root_name = data, name @template.data, @template.root_name = data, name
end end

View File

@ -9,12 +9,19 @@ module RablFastJson
@source = {} @source = {}
end end
def get_object_from_assigns def get_object_from_context
@object = @context.instance_variable_get(@data) @object = @context.instance_variable_get(@data) if @data
end
def get_assigns_from_context
@context.instance_variable_get(:@_assigns).each_pair { |k, v|
instance_variable_set("@#{k}", v) unless k.start_with?('_') || k == @data
}
end end
def render def render
get_object_from_assigns get_object_from_context
get_assigns_from_context
@object.respond_to?(:each) ? render_collection : render_resource @object.respond_to?(:each) ? render_collection : render_resource
end end
@ -29,10 +36,10 @@ module RablFastJson
when Symbol when Symbol
data.send(value) # attributes data.send(value) # attributes
when Proc when Proc
value.call(data) # node instance_exec data, &value # node
when Array # node with condition when Array # node with condition
next output if !value.first.call(data) next output if !value.first.call(data)
value.last.call(data) instance_exec data, &(value.last)
when Hash when Hash
current_value = value.dup current_value = value.dup
data_symbol = current_value.delete(:_data) data_symbol = current_value.delete(:_data)

View File

@ -7,6 +7,7 @@ class TestCompiledTemplate < ActiveSupport::TestCase
@data = User.new(1, 'foobar', 'male') @data = User.new(1, 'foobar', 'male')
@data.stub(:respond_to?).with(:each).and_return(false) @data.stub(:respond_to?).with(:each).and_return(false)
@context.stub(:instance_variable_get).with(:@data).and_return(@data) @context.stub(:instance_variable_get).with(:@data).and_return(@data)
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
@template = RablFastJson::CompiledTemplate.new @template = RablFastJson::CompiledTemplate.new
@template.context = @context @template.context = @context
@template.data = :@data @template.data = :@data

View File

@ -129,4 +129,16 @@ class CompilerTest < ActiveSupport::TestCase
assert_instance_of Array, t.source[:foo] assert_instance_of Array, t.source[:foo]
assert_equal 2, t.source[:foo].size assert_equal 2, t.source[:foo].size
end end
test "compile with no object" do
t = @compiler.compile_source(%{
object false
child(:@user => :user) do
attribute :id
end
})
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
assert_nil t.data
end
end end

View File

@ -29,6 +29,7 @@ class DeepNestingTest < ActiveSupport::TestCase
@context.stub(:instance_variable_get).with(:@user).and_return(@user) @context.stub(:instance_variable_get).with(:@user).and_return(@user)
@context.stub(:instance_variable_get).with(:@view_renderer).and_return(@view_renderer) @context.stub(:instance_variable_get).with(:@view_renderer).and_return(@view_renderer)
@context.stub(:instance_variable_get).with(:@virtual_path).and_return('users/show') @context.stub(:instance_variable_get).with(:@virtual_path).and_return('users/show')
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
end end
test "compile and render deep nesting template" do test "compile and render deep nesting template" do

View File

@ -0,0 +1,35 @@
require 'test_helper'
class NonRestfulResponseTest < ActiveSupport::TestCase
setup do
RablFastJson::Library.reset_instance
@user = User.new(1, 'foo', 'male')
@user.stub_chain(:posts, :count).and_return(10)
@user.stub(:respond_to?).with(:each).and_return(false)
@context = Context.new
@context.stub(:instance_variable_get).with(:@user).and_return(@user)
@context.stub(:instance_variable_get).with(:@view_renderer).and_return(mock())
@context.stub(:instance_variable_get).with(:@virtual_path).and_return('user/show')
@context.stub(:instance_variable_get).with(:@_assigns).and_return({'user' => @user})
end
test "compile and render non restful resource" do
source = %{
object false
node(:post_count) { @user.posts.count }
child(:@user => :user) do
attributes :id, :name
end
}
assert_equal(ActiveSupport::JSON.encode({
:post_count => 10,
:user => {
:id => 1,
:name => 'foo'
}
}), RablFastJson::Library.instance.get_rendered_template(source, @context))
end
end