app/cms/scrivito/basic_widget.rb in scrivito_sdk-1.16.0 vs app/cms/scrivito/basic_widget.rb in scrivito_sdk-1.17.0.rc1

- old
+ new

@@ -93,12 +93,10 @@ ) end attr_accessor :container, :container_attribute_name - attr_writer :obj, :id - attr_reader :attributes_to_be_saved delegate :widget_from_pool, to: :obj # Create a new Widget. @@ -219,18 +217,35 @@ def persisted? @attributes_to_be_saved.nil? end + def attach_to_obj(an_obj, widget_id = compute_widget_id(an_obj)) + @obj = an_obj + @id = widget_id + + attributes_to_be_saved + end + def ==(other) - other.respond_to?(:obj) && obj == other.obj && other.respond_to?(:id) && id == other.id + eql?(other) end def eql?(other) - self == other + other.respond_to?(:equality_identifier, true) && + equality_identifier.eql?(other.equality_identifier) end + def hash + unless persisted? + # An unbound widget will likely be attached soon, which mutates its #hash value. Evil. + raise ScrivitoError, 'a new widget is not allowed to be used as a hash key' + end + + equality_identifier.hash + end + # # Reverts all changes made to the +Widget+ in the current workspace. # # @api public # @@ -260,11 +275,11 @@ end CmsRestApi.put("workspaces/#{workspace.id}/objs/#{obj.id}", { obj: {_widget_pool: {id => previous_widget_content}} }) reload else - raise ScrivitoError, "cannot revert changes, since widget is #{modification}." + raise ScrivitoError, "cannot revert changes, since widget is #{modification}" end end def in_revision(revision) obj_in_revision = obj.in_revision(revision) @@ -295,18 +310,10 @@ Modification::EDITED end end end - def hash - if @obj && @id - (id + obj.id).hash - else - super - end - end - # Returns the entity ({Scrivito::BasicObj} or {Scrivito::BasicWidget}) # that references this widget. # @api public # # @return [Scrivito::AttributeContent] the entity that references this widget. @@ -358,11 +365,12 @@ else raise_not_persisted_error end end - def forget_unsaved_attributes + def notify_persisted(obj) + @obj = obj @attributes_to_be_saved = nil reload_data end def reload @@ -396,10 +404,16 @@ def contained_widgets referenced = referenced_widgets referenced + referenced.map { |widget| widget.contained_widgets }.flatten end + protected + + def equality_identifier + [id, obj.id] + end + private def workspace if revision.workspace revision.workspace @@ -430,9 +444,17 @@ 'string' if attribute_name == 'obj_class' end def value_of_system_attribute(attribute_name) data_from_cms.value_of(attribute_name) + end + + def compute_widget_id(obj) + @attributes_to_be_saved.delete('_id') || generate_widget_id(obj) + end + + def generate_widget_id(obj) + obj.present? ? obj.generate_widget_pool_id : BasicObj.generate_widget_pool_id end end end