Locals are now passed to the renderer object.
Allow to skip object or collection definition when using `respond_to` block
This commit is contained in:
parent
7e6da1a619
commit
4b61edad64
@ -7,7 +7,7 @@ module RablRails
|
|||||||
def self.call(template)
|
def self.call(template)
|
||||||
%{
|
%{
|
||||||
RablRails::Library.instance.
|
RablRails::Library.instance.
|
||||||
get_rendered_template(#{template.source.inspect}, self)
|
get_rendered_template(#{template.source.inspect}, self, local_assigns)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,14 +8,14 @@ module RablRails
|
|||||||
@cached_templates = {}
|
@cached_templates = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_rendered_template(source, context)
|
def get_rendered_template(source, context, locals = nil)
|
||||||
path = context.instance_variable_get(:@virtual_path)
|
path = context.instance_variable_get(:@virtual_path)
|
||||||
@lookup_context = context.lookup_context
|
@lookup_context = context.lookup_context
|
||||||
|
|
||||||
compiled_template = compile_template_from_source(source, path)
|
compiled_template = compile_template_from_source(source, path)
|
||||||
|
|
||||||
format = context.params[:format] || 'json'
|
format = context.params[:format] || 'json'
|
||||||
Renderers.const_get(format.upcase!).new(context).render(compiled_template)
|
Renderers.const_get(format.upcase!).new(context, locals).render(compiled_template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile_template_from_source(source, path = nil)
|
def compile_template_from_source(source, path = nil)
|
||||||
|
@ -33,7 +33,6 @@ module RablRails
|
|||||||
#
|
#
|
||||||
class Context
|
class Context
|
||||||
attr_reader :format
|
attr_reader :format
|
||||||
attr_accessor :target_object
|
|
||||||
|
|
||||||
def initialize(path, options)
|
def initialize(path, options)
|
||||||
@virtual_path = path
|
@virtual_path = path
|
||||||
@ -80,12 +79,11 @@ module RablRails
|
|||||||
object = options[:locals].delete(:object) if !object && options[:locals]
|
object = options[:locals].delete(:object) if !object && options[:locals]
|
||||||
|
|
||||||
c = Context.new(template, options)
|
c = Context.new(template, options)
|
||||||
c.target_object = object
|
|
||||||
|
|
||||||
t = c.lookup_context.find_template(template, [], false)
|
t = c.lookup_context.find_template(template, [], false)
|
||||||
|
|
||||||
raise TemplateNotFound unless t
|
raise TemplateNotFound unless t
|
||||||
|
|
||||||
Library.instance.get_rendered_template(t.source, c)
|
Library.instance.get_rendered_template(t.source, c, resource: object)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -5,9 +5,10 @@ module RablRails
|
|||||||
class Base
|
class Base
|
||||||
attr_accessor :_options
|
attr_accessor :_options
|
||||||
|
|
||||||
def initialize(context) # :nodoc:
|
def initialize(context, locals = nil) # :nodoc:
|
||||||
@_context = context
|
@_context = context
|
||||||
@_options = {}
|
@_options = {}
|
||||||
|
@_resource = locals[:resource] if locals
|
||||||
setup_render_context
|
setup_render_context
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -19,9 +20,13 @@ module RablRails
|
|||||||
#
|
#
|
||||||
def render(template)
|
def render(template)
|
||||||
collection_or_resource = if template.data
|
collection_or_resource = if template.data
|
||||||
template.data.to_s.start_with?('@') ? instance_variable_get(template.data) : @_context.send(template.data)
|
if @_context.respond_to?(template.data)
|
||||||
|
@_context.send(template.data)
|
||||||
|
else
|
||||||
|
instance_variable_get(template.data)
|
||||||
end
|
end
|
||||||
collection_or_resource = @_context.target_object unless collection_or_resource || template.data == false || !@_context.respond_to?(:target_object)
|
end
|
||||||
|
collection_or_resource ||= @_resource
|
||||||
output_hash = collection_or_resource.respond_to?(:each) ? render_collection(collection_or_resource, template.source) :
|
output_hash = collection_or_resource.respond_to?(:each) ? render_collection(collection_or_resource, template.source) :
|
||||||
render_resource(collection_or_resource, template.source)
|
render_resource(collection_or_resource, template.source)
|
||||||
_options[:root_name] = template.root_name
|
_options[:root_name] = template.root_name
|
||||||
|
@ -6,7 +6,6 @@ class RenderTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
setup do
|
setup do
|
||||||
@user = User.new(1, 'Marty')
|
@user = User.new(1, 'Marty')
|
||||||
@user.stub(:respond_to?).with(:each).and_return(false)
|
|
||||||
@tmp_path = Pathname.new(Dir.mktmpdir)
|
@tmp_path = Pathname.new(Dir.mktmpdir)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -89,6 +89,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "node with context method call" do
|
test "node with context method call" do
|
||||||
|
@context.stub(:respond_to?).with(:@data).and_return(false)
|
||||||
@context.stub(:respond_to?).with(:context_method).and_return(true)
|
@context.stub(:respond_to?).with(:context_method).and_return(true)
|
||||||
@context.stub(:context_method).and_return('marty')
|
@context.stub(:context_method).and_return('marty')
|
||||||
proc = lambda { |object| context_method }
|
proc = lambda { |object| context_method }
|
||||||
|
Loading…
Reference in New Issue
Block a user