module RailsConnector # The CMS widget class # @api public class BasicWidget include AttributeContent def self.type_computer @_type_computer ||= TypeComputer.new(RailsConnector::BasicWidget, ::Widget) end def self.reset_type_computer! @_type_computer = nil end attr_accessor :id, :obj, :container, :container_field_name alias_method :initialize, :update_data delegate :widget_from_pool, to: :obj def obj_class data_from_cms.value_of('_obj_class') end def ==(other) other.respond_to?(:obj) && obj == other.obj && other.respond_to?(:id) && id == other.id end def eql?(other) self == other end # Reverts content changes of this widget. Widget attributes of this widget are not effected. # After calling this method it's as if this widget has been never modified in the current working copy. # This method does not work with +new+ or +deleted+ widgets. # This method also does also not work for the +published+ workspace or the +rtc+ working copy. def revert Workspace.current.raise_if_reverting_objs_impossible previous_obj_content = CmsRestApi.get("revisions/#{Workspace.current.base_revision_id}/objs/#{obj.id}") if 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.current.id}/objs/#{obj.id}", { obj: {_widget_pool: {id => previous_widget_content}} }) else raise "cannot revert changes, since widget is new." end end def hash (id + obj.id).hash end # returns the entity ({BasicObj} or {BasicWidget}) that references this widget # @api public def container @container || cache_container_and_field_name_for_widget.first end # returns the name of the widget field that references this widget # @api public def container_field_name @container_field_name || cache_container_and_field_name_for_widget.second end private def cache_container_and_field_name_for_widget @cache_container_and_field_name_for_widget ||= obj.container_and_field_name_for_widget(id) end end end