Module: Scrivito::AttributeContent::ClassMethods

Included in:
BasicObj, BasicWidget
Defined in:
lib/scrivito/attribute_content.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, options = {}) ⇒ Object

Defines an attribute.

In order to be able to persist model data in CMS you have to define its attributes. By defining an attribute you tell Scrivito under which name its value should be persisted, which type of content it will contain etc, which values are allowed etc.

Attributes are inherited, e.g. if a model Page defines an attribute title of type string and a model SpecialPage inherits from Page, then the model SpecialPage will also have the attribute title. Inherited attributes can be overridden, e.g. SpecialPage can override the inherited attribute title by defining its own title with a different type for example.

Examples:

Defining attributes

class Page < ::Obj
  attribute :my_string, :string
  attribute 'my_html', 'my_html'
  attribute :my_enum, :enum, values: %w[a b c]
  attribute :my_multienum, :multienum
end

Page.attribute_definitions
#=> #<Scrivito::AttributeDefinitionCollection>

Page.attribute_definitions[:my_string]
#=> #<Scrivito::AttributeDefinition>

Page.attribute_definitions[:my_string].type
#=> "string"

Page.attribute_definitions[:my_html].type
#=> "html"

Page.attribute_definitions[:my_enum].type
#=> "enum"
Page.attribute_definitions[:my_enum].values
#=> ["a", "b", "c"]

Page.attribute_definitions[:my_multienum].type
#=> "multienum"
Page.attribute_definitions[:my_multienum].values
#=> []

Inheriting attributes

class Page < ::Obj
  attribute :title, :string
end

class SpecialPage < Page
end

SpecialPage.attribute_definitions[:title].type
#=> "string"

Overriding inherited attributes

class Page < ::Obj
  attribute :title, :string
end

class SpecialPage < Page
  attribute :title, :html
end

Page.attribute_definitions[:title].type
#=> "string"

SpecialPage.attribute_definitions[:title].type
#=> "html"

Parameters:

  • name (Symbol, String)

    name of the attribute.

  • type (Symbol, String)

    type of the attribute. Scrivito supports following types: string, html, enum, multienum, widgetlist, reference, referencelist and binary.

  • options (Hash) (defaults to: {})

    definition options.

Options Hash (options):

  • :values (Symbol, String)

    allowed values for types enum and multienum. If no values are given for that types, then an empty array will be assumed.

Returns:

  • nil

Raises:



382
383
384
385
386
387
388
# File 'lib/scrivito/attribute_content.rb', line 382

def attribute(name, type, options = {})
  name, type, options = name, type, options
  assert_valid_attribute_name(name.to_s)
  assert_valid_attribute_type(type.to_s)
  own_attribute_definitions[name.to_s] = AttributeDefinition.new(name, type, options)
  nil
end

#attribute_definitionsScrivito::AttributeDefinitionCollection

Returns the attribute definitions.

Returns:

See Also:

  • Scrivito::AttributeContent.attribute


407
408
409
# File 'lib/scrivito/attribute_content.rb', line 407

def attribute_definitions
  AttributeDefinitionCollection.new(all_attribute_definitions)
end

#description_for_editorObject

This method determines the description that is shown in the UI and defaults to class name. It can be overriden by a custom value.



396
397
398
# File 'lib/scrivito/attribute_content.rb', line 396

def description_for_editor
  name
end

#hide_from_editorObject

This method disables the creation of Objs or Widgets of the given type using the UI. It does not prevent adding these objects programatically.

By default hide_from_editor is false.

Examples:

Hiding error pages

class ErrorPage < Obj
  hide_from_editor
end

Hiding admin widgets

class AdminWidget < Widget
  hide_from_editor
end


427
428
429
# File 'lib/scrivito/attribute_content.rb', line 427

def hide_from_editor
  @hide_from_editor = true
end