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.
409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/scrivito/attribute_content.rb', line 409 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.
547 548 549 |
# File 'lib/scrivito/attribute_content.rb', line 547 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.
519 520 521 522 523 524 |
# File 'lib/scrivito/attribute_content.rb', line 519 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.
536 537 538 |
# File 'lib/scrivito/attribute_content.rb', line 536 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.
593 594 595 |
# File 'lib/scrivito/attribute_content.rb', line 593 def hide_from_editor @hide_from_editor = true end |