lib/rabl/builder.rb in rabl-0.7.9 vs lib/rabl/builder.rb in rabl-0.7.10

- old
+ new

@@ -63,11 +63,11 @@ # Indicates an attribute or method should be included in the json output # attribute :foo, :as => "bar" # attribute :foo, :as => "bar", :if => lambda { |m| m.foo } def attribute(name, options={}) - if @_object && @_object.respond_to?(name) && resolve_condition(options) + if @_object && attribute_present?(name) && resolve_condition(options) @_result[options[:as] || name] = data_object_attribute(name) end end alias_method :attributes, :attribute @@ -124,9 +124,21 @@ result = options[:unless] == false || (options[:unless].respond_to?(:call) && !options[:unless].call(@_object)) if options.has_key?(:unless) result end private + + # Checks if an attribute is present. If not, check if the configuration specifies that this is an error + # attribute_present?(created_at) => true + def attribute_present?(name) + if @_object.respond_to?(name) + return true + elsif Rabl.configuration.raise_on_missing_attribute + raise "Failed to render missing attribute #{name}" + else + return false + end + end # Returns a guess at the format in this scope # request_format => "xml" def request_format format = @options[:format]