Add test for RablRails#render with instance
variables
This commit is contained in:
parent
b42f63788f
commit
8f5ebfac03
@ -28,7 +28,7 @@ module RablRails
|
|||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Context class to emulate normal rendering view
|
# Context class to emulate normal Rails view
|
||||||
# context
|
# context
|
||||||
#
|
#
|
||||||
class Context
|
class Context
|
||||||
@ -66,6 +66,16 @@ module RablRails
|
|||||||
# Default render format is JSON, but can be changed via
|
# Default render format is JSON, but can be changed via
|
||||||
# an option: { format: 'xml' }
|
# an option: { format: 'xml' }
|
||||||
#
|
#
|
||||||
|
# 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:
|
||||||
|
# object :@user
|
||||||
|
# node(:read) { |u| u.has_read?(@post) }
|
||||||
|
#
|
||||||
|
# Your method call should look like this:
|
||||||
|
# RablRails.render(user, 'users/show', locals: { post: Post.new })
|
||||||
|
#
|
||||||
def render(object, template, options = {})
|
def render(object, template, options = {})
|
||||||
object = options[:locals].delete(:object) if !object && options[:locals]
|
object = options[:locals].delete(:object) if !object && options[:locals]
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ class RenderTest < ActiveSupport::TestCase
|
|||||||
attributes :name
|
attributes :name
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal %q({"user":{"name":"Marty"}}), RablRails.render(nil, 'nil', locals: { object: @user }, view_path: @tmp_path)
|
assert_equal %q({"user":{"name":"Marty"}}), RablRails.render(nil, 'nil', locals: { object: @user }, view_path: @tmp_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -28,12 +27,21 @@ class RenderTest < ActiveSupport::TestCase
|
|||||||
attributes :id, :name
|
attributes :id, :name
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path)
|
assert_equal %q({"user":{"id":1,"name":"Marty"}}), RablRails.render(@user, 'show', view_path: @tmp_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "raise error if template is not found" do
|
test "raise error if template is not found" do
|
||||||
assert_raises(RablRails::Renderer::TemplateNotFound) { RablRails.render(@user, 'show') }
|
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 }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
assert_equal %q({"username":"Marty"}), RablRails.render(nil, 'instance', view_path: @tmp_path, locals: { user: @user })
|
||||||
end
|
end
|
||||||
|
|
||||||
test "handle path for extends" do
|
test "handle path for extends" do
|
||||||
|
Loading…
Reference in New Issue
Block a user