Module: Scrivito::AttributeContent::ClassMethods
- Included in:
- BasicObj, BasicWidget
- Defined in:
- lib/scrivito/attribute_content.rb
Instance Method Summary collapse
-
#attribute(name, type, options = {}) ⇒ Object
Defines an attribute.
-
#attribute_definitions ⇒ Scrivito::AttributeDefinitionCollection
Returns the attribute definitions.
-
#default_for(attribute_name, &block) ⇒ Object
Sets the default value of an attribute.
-
#description_for_editor ⇒ String
Short description of a CMS object or widget type for the UI.
-
#hide_from_editor ⇒ Object
This method prevents UI users from creating
ObjsorWidgetsof the given type.
Instance Method Details
#attribute(name, type, options = {}) ⇒ Object
Defines an attribute.
For the purpose of persisting model data in the CMS, the attributes of the model need to be defined. When defining an attribute, you specify the name under which Scrivito should persist its value as well as the type of content it is meant to contain, and, for the enum and multienum types, the selectable values.
Attributes are inherited. If, for example, the Page model defines a title attribute of the string type, the SpecialPage model, which inherits from Page, is also equipped with title. Inherited attributes can be overridden, i.e. you may redefine title in SpecialPage if you want its type to be html, for example.
383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/scrivito/attribute_content.rb', line 383 def attribute(name, type, = {}) name, type, = name.to_s, type.to_s, assert_valid_attribute_name(name) assert_valid_attribute_type(type) default = .delete(:default) || .delete('default') if default assert_valid_attribute_default(name, type, default) default_for(name) { default } end own_attribute_definitions[name] = AttributeDefinition.new(name, type, ) nil end |
#attribute_definitions ⇒ Scrivito::AttributeDefinitionCollection
Returns the attribute definitions.
521 522 523 |
# File 'lib/scrivito/attribute_content.rb', line 521 def attribute_definitions AttributeDefinitionCollection.new(all_attribute_definitions) end |
#default_for(attribute_name, &block) ⇒ Object
Sets the default value of an attribute.
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 is a Hash containing the context that was handed over to Obj.create or Widget.new. If the current visitor is a User, this user can be accessed by means of the :scrivito_user key contained in the provided context.
493 494 495 496 497 498 |
# File 'lib/scrivito/attribute_content.rb', line 493 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 nil end |
#description_for_editor ⇒ String
Short description of a CMS object or widget type for the UI. The description is displayed when adding new pages or widgets, for example. As a general rule, it is used whenever no widget or object instance is available. If there is, the BasicObj#description_for_editor and, respectively, BasicWidget#description_for_editor instance methods are used instead.
This method can be overridden to customize the description displayed to editors.
510 511 512 |
# File 'lib/scrivito/attribute_content.rb', line 510 def description_for_editor name end |
#hide_from_editor ⇒ Object
This method prevents UI users from creating Objs or Widgets of the given type. It does not prevent adding such objects programatically.
By default, hide_from_editor is false.
567 568 569 |
# File 'lib/scrivito/attribute_content.rb', line 567 def hide_from_editor @hide_from_editor = true end |