Method: Scrivito::BasicWidget#copy

Defined in:
lib/scrivito/basic_widget.rb

#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))


127
128
129
130
131
132
133
134
135
136
# File 'lib/scrivito/basic_widget.rb', line 127

def copy
  attributes = {}
  attribute_definitions.each do |attribute_definition|
    attribute_name = attribute_definition.name
    attribute_value = read_attribute(attribute_name)
    attribute_value = attribute_value.map(&:copy) if attribute_definition.widgetlist?
    attributes[attribute_name] = attribute_value
  end
  self.class.new(attributes)
end