Method: Scrivito::AttributeContent::ClassMethods#default_for
- Defined in:
- app/cms/scrivito/attribute_content.rb
#default_for(attribute_name, &block) ⇒ Object
Sets the default value of an attribute defined by #attribute or for the built-in attributes _path and _permalink.
If Obj.create or Widget.new are called without providing a value for a specific custom attribute, the block is called, and its return value is used as the initial value of this attribute.
The block is called with two parameters.
The first parameter is an ActiveSupport::HashWithIndifferentAccess containing the attributes that were passed to Obj.create or Widget.new.
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 and the :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.
573 574 575 576 577 578 |
# File 'app/cms/scrivito/attribute_content.rb', line 573 def default_for(attribute_name, &block) attribute_name = attribute_name.to_s raise ScrivitoError, 'No block given' unless block_given? attribute_default_definitions[attribute_name] = block nil end |