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:
ccocchi 2012-09-13 15:57:05 +02:00
parent 7e6da1a619
commit 4b61edad64
6 changed files with 22 additions and 19 deletions

View File

@ -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

View File

@ -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)

View File

@ -4,7 +4,7 @@ require 'rabl-rails/renderers/json'
module RablRails
module Renderer
class TemplateNotFound < StandardError; end
mattr_reader :view_path
@@view_path = 'app/views'
@ -33,7 +33,6 @@ module RablRails
#
class Context
attr_reader :format
attr_accessor :target_object
def initialize(path, options)
@virtual_path = path
@ -56,17 +55,17 @@ module RablRails
@lookup_context ||= LookupContext.new(@options[:view_path], format)
end
end
#
# Renders object with the given rabl template.
#
#
# Object can also be passed as an option :
# { locals: { object: obj_to_render } }
#
# Default render format is JSON, but can be changed via
# an option: { format: 'xml' }
#
# If template includes uses of instance variables (usually
# If template includes uses of instance variables (usually
# defined in the controller), you can passed them as locals
# options.
# For example, if you have this template:
@ -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

View File

@ -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
end
collection_or_resource = @_context.target_object unless collection_or_resource || template.data == false || !@_context.respond_to?(:target_object)
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

View File

@ -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
@ -29,16 +28,16 @@ class RenderTest < ActiveSupport::TestCase
end
assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path)
end
test "raise error if template is not found" do
assert_raises(RablRails::Renderer::TemplateNotFound) { RablRails.render(@user, 'not_found') }
end
test "instance variables can be passed via options[:locals]" do
File.open(@tmp_path + "instance.json.rabl", "w") do |f|
f.puts %q{
object false
node(:username) { |_| @user.name }
node(:username) { |_| @user.name }
}
end
assert_equal %q({"username":"Marty"}), RablRails.render(nil, 'instance', view_path: @tmp_path, locals: { user: @user })
@ -51,7 +50,7 @@ class RenderTest < ActiveSupport::TestCase
extends 'base'
}
end
File.open(@tmp_path + "base.json.rabl", "w") do |f|
f.puts %q{
attribute :name, as: :extended_name

View File

@ -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 }