Module: Scrivito::AttributeContent

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

Overview

This class is for handling attributes: setting and accessing their values, providing default values, restricting the types of widgets that may be added to a widgetlist attribute, plus a couple of convenience methods.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_ATTRIBUTE_VALUES =

Default attribute values.

{
  'binary'        => nil,
  'date'          => nil,
  'enum'          => nil,
  'float'         => nil,
  'html'          => '',
  'integer'       => nil,
  'link'          => nil,
  'linklist'      => [],
  'multienum'     => [],
  'reference'     => nil,
  'referencelist' => [],
  'string'        => '',
  'stringlist'    => [],
  'widgetlist'    => [],
}

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



94
95
96
97
# File 'app/cms/scrivito/attribute_content.rb', line 94

def method_missing(method_name, *args)
  attribute_name = method_name.to_s
  has_custom_attribute?(attribute_name) ? read_attribute(attribute_name) : super
end

Instance Method Details

#[](attribute_name) ⇒ Object

Returns the value of an attribute specified by its name. Passing an invalid key will not raise an error but return nil.

Parameters:

  • attribute_name (Symbol, String)

    the name of the attribute.

Returns:

  • the value of the attribute if it’s defined or nil otherwise.



111
112
113
114
# File 'app/cms/scrivito/attribute_content.rb', line 111

def [](attribute_name)
  attribute_name = attribute_name.to_s
  read_attribute(attribute_name) if has_attribute?(attribute_name)
end

#as_json(options = nil) ⇒ Hash

Note:

Override it in subclasses to fit your needs.

Returns a hash to be used for the JSON serialization.

Parameters:

  • options (Hash) (defaults to: nil)

Returns:

  • (Hash)

See Also:



257
258
259
# File 'app/cms/scrivito/attribute_content.rb', line 257

def as_json(options = nil)
  {id: id}
end

#obj_classString

Returns the object class name of this CMS object.

Examples:

BlogPost.all.first.obj_class
#=> "BlogPost"
FrenchBlog::BlogPost.all.first.obj_class
#=> "FrenchBlog::BlogPost"

GermanBlog::BlogPost.all.first.obj_class
#=> "GermanBlog::BlogPost"

Returns:

  • (String)


199
200
201
# File 'app/cms/scrivito/attribute_content.rb', line 199

def obj_class
  data_from_cms.value_of('_obj_class')
end

#obj_class_nameString

Deprecated.

Use #obj_class instead.

Returns the object class name of this CMS object.

Returns:

  • (String)


176
177
178
179
180
181
182
# File 'app/cms/scrivito/attribute_content.rb', line 176

def obj_class_name
  Scrivito::Deprecation.warn(
    'Scrivito::BasicObj#obj_class_name and Scrivito::BasicWidget#obj_class_name are deprecated'\
    ' and will be removed in a future version.'\
    ' Please use Scrivito::BasicObj#obj_class and Scrivito::BasicWidget#obj_class instead.')
  obj_class
end

#valid_widget_classes_for(field_name) ⇒ nil, Array<Class>

Hook method that lets you control the widget classes that are made available for adding instances of them to this page or widget. Override it to allow only specific classes or none at all. Must return either NilClass, or Array.

If nil is returned (default), all widget classes will be available for this page or widget.

If an Array is returned, it is expected to include the permitted classes. Their order is preserved as they are offered to the user via the widget browser.

Parameters:

  • field_name (String)

    Name of the widget attribute.

Returns:

  • (nil, Array<Class>)

See Also:



134
135
# File 'app/cms/scrivito/attribute_content.rb', line 134

def valid_widget_classes_for(field_name)
end