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
def object(data)
return if data === false
data, name = extract_data_and_name(data)
@template.data, @template.root_name = data, name
end

View File

@ -9,12 +9,19 @@ module RablFastJson
@source = {}
end
def get_object_from_assigns
@object = @context.instance_variable_get(@data)
def get_object_from_context
@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
def render
get_object_from_assigns
get_object_from_context
get_assigns_from_context
@object.respond_to?(:each) ? render_collection : render_resource
end
@ -29,10 +36,10 @@ module RablFastJson
when Symbol
data.send(value) # attributes
when Proc
value.call(data) # node
instance_exec data, &value # node
when Array # node with condition
next output if !value.first.call(data)
value.last.call(data)
instance_exec data, &(value.last)
when Hash
current_value = value.dup
data_symbol = current_value.delete(:_data)

View File

@ -7,6 +7,7 @@ class TestCompiledTemplate < ActiveSupport::TestCase
@data = User.new(1, 'foobar', 'male')
@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(:@_assigns).and_return({})
@template = RablFastJson::CompiledTemplate.new
@template.context = @context
@template.data = :@data

View File

@ -129,4 +129,16 @@ class CompilerTest < ActiveSupport::TestCase
assert_instance_of Array, t.source[:foo]
assert_equal 2, t.source[:foo].size
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

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(:@view_renderer).and_return(@view_renderer)
@context.stub(:instance_variable_get).with(:@virtual_path).and_return('users/show')
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
end
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