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 defined by #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
Objs
orWidgets
of 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.
448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/scrivito/attribute_content.rb', line 448 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.
591 592 593 |
# File 'lib/scrivito/attribute_content.rb', line 591 def attribute_definitions AttributeDefinitionCollection.new(all_attribute_definitions) end |
#default_for(attribute_name, &block) ⇒ Object
Sets the default value of an attribute defined by #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.
559 560 561 562 563 564 |
# File 'lib/scrivito/attribute_content.rb', line 559 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.
580 581 582 |
# File 'lib/scrivito/attribute_content.rb', line 580 def description_for_editor name.titleize 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
.
637 638 639 |
# File 'lib/scrivito/attribute_content.rb', line 637 def hide_from_editor @hide_from_editor = true end |