Skip to content
Browse files

Fixes support for code if blocks

  • Loading branch information...
1 parent fd1b41f commit a4e0351391861120686e3a10b6de170a5dde2997 @nesquena committed
Showing with 6 additions and 5 deletions.
  1. +3 −3 lib/rabl/builder.rb
  2. +3 −2 lib/rabl/engine.rb
View
6 lib/rabl/builder.rb
@@ -15,8 +15,8 @@ def to_hash(options={})
attribute(attribute, :as => name)
end if @options.has_key?(:attributes)
# Code
- @options[:code].each_pair do |name, block|
- code(name, &block)
+ @options[:code].each_pair do |name, settings|
+ code(name, settings[:options], &settings[:block])
end if @options.has_key?(:code)
# Children
@options[:child].each do |settings|
@@ -55,7 +55,7 @@ def attribute(*args)
# code(:foo) { "bar" }
# code(:foo, :if => lambda { |m| m.foo.present? }) { "bar" }
def code(name, options={}, &block)
- append_node = options[:if].nil? || (options[:if].respond_to?(:call) && options[:if].call(@_object))
+ append_node = !options[:if] || options[:if] == true || (options[:if].respond_to?(:call) && options[:if].call(@_object))
@_result[name] = block.call(@_object) if append_node
end
alias_method :node, :code
View
5 lib/rabl/engine.rb
@@ -42,9 +42,10 @@ def attribute(*args)
# Creates an arbitrary code node that is included in the json output
# code(:foo) { "bar" }
- def code(name, &block)
+ # code(:foo, :if => lambda { ... }) { "bar" }
+ def code(name, options={}, &block)
@_options[:code] ||= {}
- @_options[:code][name] = block
+ @_options[:code][name] = { :options => options, :block => block }
end
# Creates a child node that is included in json output

0 comments on commit a4e0351

Please sign in to comment.
Something went wrong with that request. Please try again.