lib/scrivito/attribute_content.rb in scrivito_sdk-1.5.5 vs lib/scrivito/attribute_content.rb in scrivito_sdk-1.6.0.rc1

- old
+ new

@@ -42,13 +42,13 @@ # DEFAULT_ATTRIBUTE_VALUES = { 'binary' => nil, 'date' => nil, 'enum' => nil, - 'float' => FloatConversion::DEFAULT_VALUE, + 'float' => nil, 'html' => '', - 'integer' => IntegerConversion::DEFAULT_VALUE, + 'integer' => nil, 'link' => nil, 'linklist' => [], 'multienum' => [], 'reference' => nil, 'referencelist' => [], @@ -94,10 +94,14 @@ def method_missing(method_name, *args) attribute_name = method_name.to_s has_custom_attribute?(attribute_name) ? read_attribute(attribute_name) : super end + def respond_to_missing?(method_name, include_private = false) + has_custom_attribute?(method_name.to_s) || super + end + # # Returns the value of an attribute specified by its name. # Passing an invalid key will not raise an error but return +nil+. # # @api public @@ -316,18 +320,22 @@ instance end def prepare_attributes_for_instantiation(attributes) - attributes.with_indifferent_access.tap do |attributes| - prepare_obj_class_attribute(attributes) unless special_class? + attributes.with_indifferent_access.tap do |indifferent_attributes| + prepare_obj_class_attribute(indifferent_attributes) unless special_class? end end def extract_obj_class_from_attributes(attributes) - if special_class? && (obj_class = attributes[:_obj_class] || attributes['_obj_class']) - type_computer.compute_type_without_fallback(obj_class) + if special_class? + if obj_class = attributes[:_obj_class] || attributes['_obj_class'] + type_computer.compute_type_without_fallback(obj_class) + else + raise ArgumentError, 'Please provide an obj class as the "_obj_class" property.' + end end end # # Defines an attribute. @@ -357,13 +365,13 @@ # resulting array of selectable values is empty. Empty string is not allowed as value. # @option options [Symbol, String] :default custom default value. # See {Scrivito::AttributeContent::DEFAULT_ATTRIBUTE_VALUES} for factory defaults. # See {Scrivito::AttributeContent::ClassMethods#default_for} for more advanced defaults. # @option options [Class, Array<Class>] :only specifies (for use in the UI) the valid classes - # _of_ _the_ _values_ in a +reference+ or a +referencelist+ attribute, e.g. +Image+ or +Video+. - # Raises an error if the attribute type is neither +reference+ nor +referencelist+. If not - # specified, the UI assumes that any class is valid. + # _of_ _the_ _values_ in a +reference+ or a +referencelist+ attribute, + # e.g. +Image+ or +Video+. Raises an error if the attribute type is neither +reference+ + # nor +referencelist+. If not specified, the UI assumes that any class is valid. # # @return nil # # @raise [Scrivito::ScrivitoError] if the +name+ is invalid # @raise [Scrivito::ScrivitoError] if the +type+ is unknown @@ -602,10 +610,10 @@ attributes_with_defaults = attributes_with_indifferent_access.dup attribute_definitions.each do |attribute_definition| attribute_name = attribute_definition.name attribute_default = attribute_defaults[attribute_name] - if !attributes_with_indifferent_access.has_key?(attribute_name) && attribute_default + if !attributes_with_indifferent_access.key?(attribute_name) && attribute_default attributes_with_defaults[attribute_name] = attribute_default.call(attributes_with_indifferent_access, context) end end