Class: Scrivito::BasicWidget

Inherits:
Object
  • Object
show all
Defined in:
lib/scrivito/basic_widget.rb

Overview

The CMS widget class

Direct Known Subclasses

Widget

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Examples:

Create a widget using a subclass

# you can create widgets by explicitly providing the attribute `_obj_class`
new_widget = Widget.new(_obj_class: "MyWidget")
# but it is better to simply use the constructor of a subclass
new_widget = MyWidget.new

Create a widget and store it inside an Obj

# create the widget
new_widget = Widget.new(_obj_class: "MyWidget")
# store it inside an obj
my_obj(my_widget_field: [new_widget])

Parameters:

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


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

#containerObject

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_nameObject

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

#copyObject

Create a copy of a Widget.

The copy will have all attributes of the original widget including its widgets. Its attributes can be accessed only after it has been stored in a widget field of an Obj, since initially the copy is not stored in any widget field.

Examples:

Duplicate the first widget in field my_widget

obj.update(my_widgets: obj.my_widgets.push(obj.my_widgets.first.copy))


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_editorObject

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

#destroyObject

Destroys the Widget in the current Workspace



68
69
70
71
# File 'lib/scrivito/basic_widget.rb', line 68

def destroy
  new_widget_list = container[container_field_name] - [self]
  container.update(container_field_name => new_widget_list)
end

#revertObject

Note:

This method does not support Widgets, which are new. Please use Widget#destroy to destroy them.

Note:

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.

Raises:



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_widget_content = previous_obj_content["_widget_pool"]["#{id}"]
    previous_widget_content.delete_if do |attribute_name, _|
      type_of_attribute(attribute_name) == "widget"
    end
    CmsRestApi.put("workspaces/#{workspace.id}/objs/#{obj.id}",
        { obj: {_widget_pool: {id => previous_widget_content}} })
  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

Parameters:

  • attributes (Hash)


61
62
63
64
# File 'lib/scrivito/basic_widget.rb', line 61

def update(attributes)
  obj.update(_widget_pool: { self => attributes })
  reload
end