Class: Scrivito::BasicWidget
- Inherits:
-
Object
- Object
- Scrivito::BasicWidget
- Defined in:
- lib/scrivito/basic_widget.rb
Overview
The CMS widget class
Direct Known Subclasses
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
returns the entity (BasicObj or BasicWidget) that references this widget.
-
#container_field_name ⇒ Object
readonly
returns the name of the widget field that references this widget.
Instance Method Summary collapse
-
#copy ⇒ Object
Create a copy of a Widget.
-
#description_for_editor ⇒ Object
This method determines the description that is shown in the widget tooltips.
- #destroy ⇒ Object
-
#initialize(attributes = {}) ⇒ BasicWidget
constructor
Create a new Widget.
-
#revert ⇒ Object
Reverts all changes made to the
Widgetin the current workspace. -
#update(attributes) ⇒ Object
Update the attributes of this Widget.
Constructor Details
#initialize(attributes = {}) ⇒ BasicWidget
Create a new Widget. The new Widget must be stored inside a container (i.e. an Obj or another Widget) before it can be used.
See Obj.create for a detailed overview of how to set attributes.
48 49 50 |
# File 'lib/scrivito/basic_widget.rb', line 48 def initialize(attributes = {}) @attributes_to_be_saved = self.class.with_default_obj_class(attributes) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Scrivito::AttributeContent
Instance Attribute Details
#container ⇒ Object
returns the entity (Scrivito::BasicObj or Scrivito::BasicWidget) that references this widget
207 208 209 |
# File 'lib/scrivito/basic_widget.rb', line 207 def container @container end |
#container_field_name ⇒ Object
returns the name of the widget field that references this widget
213 214 215 |
# File 'lib/scrivito/basic_widget.rb', line 213 def container_field_name @container_field_name end |
Instance Method Details
#copy ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/scrivito/basic_widget.rb', line 85 def copy attrs = {} each_custom_attribute do |attr_name, attr_value, attr_type| attrs[attr_name] = attr_type == 'widget' ? attr_value.map(&:copy) : attr_value end self.class.new(attrs) end |
#description_for_editor ⇒ Object
This method determines the description that is shown in the widget tooltips. It can be overriden by a custom value.
260 261 262 |
# File 'lib/scrivito/basic_widget.rb', line 260 def description_for_editor obj_class_name end |
#destroy ⇒ Object
68 69 70 71 |
# File 'lib/scrivito/basic_widget.rb', line 68 def destroy = container[container_field_name] - [self] container.update(container_field_name => ) end |
#revert ⇒ Object
This method does not support Widgets, which are new. Please use Widget#destroy to destroy them.
This method does not support Widgets, which are deleted. Please use Obj#revert to restore them.
Reverts all changes made to the Widget in the current workspace.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/scrivito/basic_widget.rb', line 146 def revert workspace.assert_revertable case modification when Modification::UNMODIFIED # do nothing when Modification::EDITED previous_obj_content = CmsRestApi.get("revisions/#{workspace.base_revision_id}/objs/#{obj.id}") = previous_obj_content["_widget_pool"]["#{id}"] .delete_if do |attribute_name, _| type_of_attribute(attribute_name) == "widget" end CmsRestApi.put("workspaces/#{workspace.id}/objs/#{obj.id}", { obj: {_widget_pool: {id => }} }) else raise ScrivitoError, "cannot revert changes, since widget is #{modification}." end end |
#update(attributes) ⇒ Object
Update the attributes of this Widget
See Obj.create for a detailed overview of how to set attributes
61 62 63 64 |
# File 'lib/scrivito/basic_widget.rb', line 61 def update(attributes) obj.update(_widget_pool: { self => attributes }) reload end |