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)
|
||||
%{
|
||||
RablRails::Library.instance.
|
||||
get_rendered_template(#{template.source.inspect}, self)
|
||||
get_rendered_template(#{template.source.inspect}, self, local_assigns)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
@ -8,14 +8,14 @@ module RablRails
|
||||
@cached_templates = {}
|
||||
end
|
||||
|
||||
def get_rendered_template(source, context)
|
||||
def get_rendered_template(source, context, locals = nil)
|
||||
path = context.instance_variable_get(:@virtual_path)
|
||||
@lookup_context = context.lookup_context
|
||||
|
||||
compiled_template = compile_template_from_source(source, path)
|
||||
|
||||
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
|
||||
|
||||
def compile_template_from_source(source, path = nil)
|
||||
|
@ -33,7 +33,6 @@ module RablRails
|
||||
#
|
||||
class Context
|
||||
attr_reader :format
|
||||
attr_accessor :target_object
|
||||
|
||||
def initialize(path, options)
|
||||
@virtual_path = path
|
||||
@ -80,12 +79,11 @@ module RablRails
|
||||
object = options[:locals].delete(:object) if !object && options[:locals]
|
||||
|
||||
c = Context.new(template, options)
|
||||
c.target_object = object
|
||||
|
||||
t = c.lookup_context.find_template(template, [], false)
|
||||
|
||||
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
|
@ -5,9 +5,10 @@ module RablRails
|
||||
class Base
|
||||
attr_accessor :_options
|
||||
|
||||
def initialize(context) # :nodoc:
|
||||
def initialize(context, locals = nil) # :nodoc:
|
||||
@_context = context
|
||||
@_options = {}
|
||||
@_resource = locals[:resource] if locals
|
||||
setup_render_context
|
||||
end
|
||||
|
||||
@ -19,9 +20,13 @@ module RablRails
|
||||
#
|
||||
def render(template)
|
||||
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
|
||||
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) :
|
||||
render_resource(collection_or_resource, template.source)
|
||||
_options[:root_name] = template.root_name
|
||||
|
@ -6,7 +6,6 @@ class RenderTest < ActiveSupport::TestCase
|
||||
|
||||
setup do
|
||||
@user = User.new(1, 'Marty')
|
||||
@user.stub(:respond_to?).with(:each).and_return(false)
|
||||
@tmp_path = Pathname.new(Dir.mktmpdir)
|
||||
end
|
||||
|
||||
|
@ -89,6 +89,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
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(:context_method).and_return('marty')
|
||||
proc = lambda { |object| context_method }
|
||||
|
Loading…
Reference in New Issue
Block a user