lib/scrivito/attribute_content.rb in scrivito_sdk-1.9.1 vs lib/scrivito/attribute_content.rb in scrivito_sdk-1.10.0.rc1

- old
+ new

@@ -467,11 +467,12 @@ nil end # # Sets the default value of an attribute defined by - # {Scrivito::AttributeContent::ClassMethods#attribute}. + # {Scrivito::AttributeContent::ClassMethods#attribute} or for the built-in attributes +_path+ + # and +_permalink+. # # @api public # # If {Scrivito::BasicObj.create Obj.create} or {Scrivito::BasicWidget.new Widget.new} are called # without providing a value for a specific custom attribute, the +block+ is called, and its @@ -481,14 +482,16 @@ # # The first parameter is an +ActiveSupport::HashWithIndifferentAccess+ containing the attributes # that were passed to {Scrivito::BasicObj.create Obj.create} or # {Scrivito::BasicWidget.new Widget.new}. # - # The second parameter is a +Hash+ containing the context that was handed over to - # {Scrivito::BasicObj.create Obj.create} or {Scrivito::BasicWidget.new Widget.new}. If the - # current visitor is a {Scrivito::User}, this user can be accessed by means of the - # +:scrivito_user+ key contained in the provided context. + # The second parameter, +context+, is a hash. If the +default_for+ callback is triggered by a UI + # user creating a CMS object or a Widget, Scrivito places the {Scrivito::User +:current_user+} + # and the {Scrivito::BasicObj +:current_page+} (originating from the UI calling the creation + # method) into this hash. +:current_page+ won't be present in the +context+ if the user creates + # the object or widget while viewing a page which isn't a CMS object. The +context+ hash is + # empty if the object or widget is not created using the UI but, for example, via the console. # # @param [Symbol, String] attribute_name the name of the attribute. # @param [Proc] block that returns the default value. # @raise [Scrivito::ScrivitoError] if no block is present # @return nil @@ -566,11 +569,11 @@ # @see https://scrivito.com/default-attribute-values Specifying default attribute values # def default_for(attribute_name, &block) attribute_name = attribute_name.to_s raise ScrivitoError, 'No block given' unless block_given? - attribute_defaults[attribute_name] = block + attribute_default_definitions[attribute_name] = block nil end # # Short description of a CMS object or widget type for the UI. @@ -601,23 +604,22 @@ AttributeDefinitionCollection.new(all_attribute_definitions) end # For testing purposes only. def reset_attribute_defaults! - @attribute_defaults = nil + @attribute_default_definitions = nil end def build_attributes_with_defaults(attributes = {}, context = {}) attributes_with_indifferent_access = attributes.with_indifferent_access + context_with_indifferent_access = context.with_indifferent_access 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.key?(attribute_name) && attribute_default - attributes_with_defaults[attribute_name] = - attribute_default.call(attributes_with_indifferent_access, context) + attribute_defaults.each do |name, default_block| + unless attributes_with_indifferent_access.key?(name) + attributes_with_defaults[name] = + default_block.call(attributes_with_indifferent_access, context_with_indifferent_access) end end if superclass.respond_to?(:build_attributes_with_defaults) attributes_with_defaults.merge!( @@ -723,10 +725,18 @@ def own_attribute_definitions @own_attribute_definitions ||= {} end + def attribute_defaults + attribute_default_definitions.slice(*default_attribute_names) + end + + def default_attribute_names + attribute_definitions.map(&:name) + end + private def prepare_obj_class_attribute(attributes) if obj_class = attributes['_obj_class'] assert_valid_obj_class(obj_class) @@ -739,11 +749,11 @@ unless obj_class == to_s raise ScrivitoError, "Cannot set _obj_class to #{obj_class.inspect} when creating #{self}" end end - def attribute_defaults - @attribute_defaults ||= {} + def attribute_default_definitions + @attribute_default_definitions ||= {} end end end end