Merge pull request #8 from ccocchi/condition-blocks
Allow conditionnal blocks within template
This commit is contained in:
commit
140a80ecf9
@ -5,6 +5,7 @@ require 'active_support/core_ext/class/attribute_accessors'
|
|||||||
|
|
||||||
require 'rabl-rails/version'
|
require 'rabl-rails/version'
|
||||||
require 'rabl-rails/template'
|
require 'rabl-rails/template'
|
||||||
|
require 'rabl-rails/condition'
|
||||||
require 'rabl-rails/compiler'
|
require 'rabl-rails/compiler'
|
||||||
|
|
||||||
require 'rabl-rails/renderer'
|
require 'rabl-rails/renderer'
|
||||||
|
@ -5,7 +5,7 @@ module RablRails
|
|||||||
#
|
#
|
||||||
class Compiler
|
class Compiler
|
||||||
def initialize
|
def initialize
|
||||||
@glue_count = 0
|
@i = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -80,8 +80,8 @@ module RablRails
|
|||||||
#
|
#
|
||||||
def glue(data)
|
def glue(data)
|
||||||
return unless block_given?
|
return unless block_given?
|
||||||
name = :"_glue#{@glue_count}"
|
name = :"_glue#{@i}"
|
||||||
@glue_count += 1
|
@i += 1
|
||||||
@template[name] = sub_compile(data) { yield }
|
@template[name] = sub_compile(data) { yield }
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -118,6 +118,20 @@ module RablRails
|
|||||||
@template.merge!(t.source)
|
@template.merge!(t.source)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Provide a conditionnal block
|
||||||
|
#
|
||||||
|
# condition(->(u) { u.is_a?(Admin) }) do
|
||||||
|
# attributes :secret
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
def condition(proc)
|
||||||
|
return unless block_given?
|
||||||
|
name = :"_if#{@i}"
|
||||||
|
@i += 1
|
||||||
|
@template[name] = Condition.new(proc, sub_compile(nil) { yield })
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -142,7 +156,7 @@ module RablRails
|
|||||||
return {} unless block_given?
|
return {} unless block_given?
|
||||||
old_template, @template = @template, {}
|
old_template, @template = @template, {}
|
||||||
yield
|
yield
|
||||||
@template.merge!(:_data => data)
|
data ? @template.merge!(:_data => data) : @template
|
||||||
ensure
|
ensure
|
||||||
@template = old_template
|
@template = old_template
|
||||||
end
|
end
|
||||||
|
10
lib/rabl-rails/condition.rb
Normal file
10
lib/rabl-rails/condition.rb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
module RablRails
|
||||||
|
class Condition
|
||||||
|
attr_reader :proc, :source
|
||||||
|
|
||||||
|
def initialize(proc, source)
|
||||||
|
@proc = proc
|
||||||
|
@source = source
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -78,6 +78,11 @@ module RablRails
|
|||||||
else # child
|
else # child
|
||||||
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
|
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
|
||||||
end
|
end
|
||||||
|
when Condition
|
||||||
|
if instance_exec data, &(value.proc)
|
||||||
|
output.merge!(render_resource(data, value.source))
|
||||||
|
end
|
||||||
|
next output
|
||||||
end
|
end
|
||||||
output[key] = out
|
output[key] = out
|
||||||
output
|
output
|
||||||
|
@ -153,6 +153,12 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
assert_equal 2, t.source[:foo].size
|
assert_equal 2, t.source[:foo].size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "conditionnal block compile nicely" do
|
||||||
|
t = @compiler.compile_source(%{ condition(->(u) {}) do attributes :secret end })
|
||||||
|
assert_instance_of RablRails::Condition, t.source[:_if0]
|
||||||
|
assert_equal({ :secret => :secret }, t.source[:_if0].source)
|
||||||
|
end
|
||||||
|
|
||||||
test "compile with no object" do
|
test "compile with no object" do
|
||||||
t = @compiler.compile_source(%{
|
t = @compiler.compile_source(%{
|
||||||
object false
|
object false
|
||||||
|
@ -127,6 +127,18 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|||||||
assert_equal %q({"users":[]}), render_json_output
|
assert_equal %q({"users":[]}), render_json_output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "condition blocks are transparent if the condition passed" do
|
||||||
|
c = RablRails::Condition.new(->(u) { true }, { :name => :name })
|
||||||
|
@template.source = { :_if0 => c }
|
||||||
|
assert_equal %q({"name":"foobar"}), render_json_output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "condition blocks are ignored if the condition is not met" do
|
||||||
|
c = RablRails::Condition.new(->(u) { false }, { :name => :name })
|
||||||
|
@template.source = { :_if0 => c }
|
||||||
|
assert_equal %q({}), render_json_output
|
||||||
|
end
|
||||||
|
|
||||||
test "render object with root node" do
|
test "render object with root node" do
|
||||||
@template.root_name = :author
|
@template.root_name = :author
|
||||||
@template.source = { :id => :id, :name => :name }
|
@template.source = { :id => :id, :name => :name }
|
||||||
|
Loading…
Reference in New Issue
Block a user