Module: Scrivito::AttributeContent::ClassMethods
- Included in:
- BasicObj, BasicWidget
- Defined in:
- app/cms/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 or for the built-in attributes
_path
and_permalink
. -
#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.
459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'app/cms/scrivito/attribute_content.rb', line 459 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.
605 606 607 |
# File 'app/cms/scrivito/attribute_content.rb', line 605 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 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 |
#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.
594 595 596 |
# File 'app/cms/scrivito/attribute_content.rb', line 594 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
.
650 651 652 |
# File 'app/cms/scrivito/attribute_content.rb', line 650 def hide_from_editor @hide_from_editor = true end |