Fix some errors

This commit is contained in:
ccocchi 2012-03-27 17:07:30 +02:00
parent 35ba212214
commit d9e93b2c53
3 changed files with 17 additions and 8 deletions

View File

@ -71,10 +71,10 @@ module RablFastJson
@template.merge!(t.source) @template.merge!(t.source)
end end
def object(data) def object(data, options = {})
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 = data
@template.root_name = options[:root] || name
end end
protected protected

View File

@ -9,6 +9,13 @@ module RablFastJson
@source = {} @source = {}
end end
#
# def data=(symbol)
# raise "Data passed directly to template should be a symbol" if !symbol.is_a?(Symbol)
# @data = symbol
# end
#
def get_object_from_context def get_object_from_context
@object = @context.instance_variable_get(@data) if @data @object = @context.instance_variable_get(@data) if @data
end end
@ -29,8 +36,9 @@ module RablFastJson
def partial(template_path, options = {}) def partial(template_path, options = {})
raise "No object was given to partial" if options[:object].blank? raise "No object was given to partial" if options[:object].blank?
template = Library.instance.get(template_path, @context) object = options[:object]
template.render_resource(options[:object]) template = Library.instance.get(template_path, @context)
object.respond_to?(:each) ? template.render_collection(object) : template.render_resource(object)
end end
def render def render
@ -38,7 +46,7 @@ module RablFastJson
get_assigns_from_context get_assigns_from_context
@object.respond_to?(:each) ? render_collection : render_resource @object.respond_to?(:each) ? render_collection : render_resource
end end
def render_resource(data = nil, source = nil) def render_resource(data = nil, source = nil)
data ||= @object data ||= @object
source ||= @source source ||= @source
@ -52,7 +60,7 @@ module RablFastJson
when Proc when Proc
instance_exec data, &value # 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 !instance_exec data, &(value.first) #value.first.call(data)
instance_exec data, &(value.last) instance_exec data, &(value.last)
when Hash when Hash
current_value = value.dup current_value = value.dup
@ -77,5 +85,6 @@ module RablFastJson
collection ||= @object collection ||= @object
collection.inject([]) { |output, o| output << render_resource(o, source) } collection.inject([]) { |output, o| output << render_resource(o, source) }
end end
end end
end end

View File

@ -139,6 +139,6 @@ class CompilerTest < ActiveSupport::TestCase
}) })
assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source) assert_equal({ :user => { :_data => :@user, :id => :id } }, t.source)
assert_nil t.data assert_equal false, t.data
end end
end end