lib/scrivito/basic_obj.rb in scrivito_sdk-1.9.1 vs lib/scrivito/basic_obj.rb in scrivito_sdk-1.10.0.rc1
- old
+ new
@@ -76,10 +76,11 @@
# {Scrivito::AttributeContent::ClassMethods#default_for Obj.default_for} are taken into account.
#
# @param [Hash] attributes of the new obj
# @param [Hash] context in which the object is created
# @option context [Scrivito::User] :scrivito_user current visitor
+ # @option context [Scrivito::BasicObj] :current_page current page
# @return [Obj] the newly created {Scrivito::BasicObj Obj}
#
# @raise [ArgumentError] if called on +Obj+ without +_obj_class+ attribute.
#
# @see Scrivito::AttributeContent::ClassMethods#default_for
@@ -147,10 +148,34 @@
CmsRestApi::WidgetExtractor.notify_persisted_widgets(obj, widget_properties)
obj
end
end
+ #
+ # Restores a previously deleted +Obj+.
+ #
+ # @api public
+ #
+ # @raise [ScrivitoError] If the current workspace is +published+.
+ def self.restore(obj_id)
+ Workspace.current.assert_revertable
+ base_revision_path = "revisions/#{Workspace.current.base_revision_id}/objs/#{obj_id}"
+ obj_attributes = CmsRestApi.get(base_revision_path).merge('_id' => obj_id)
+ Workspace.current.api_request(:post, '/objs', obj: obj_attributes)
+ end
+
+ private_class_method def self.assert_not_basic_obj(method_name)
+ if self == Scrivito::BasicObj
+ raise ScrivitoError, "Can not call #{method_name} on Scrivito::BasicObj."\
+ " Only call it on Obj or subclasses of Obj"
+ end
+ end
+
+ private_class_method def self.default_attribute_names
+ super + %w[_path _permalink]
+ end
+
# Create a new {Scrivito::BasicObj Obj} instance with the given values and attributes.
# Normally, this method should not be used.
# Instead, CMS objects should be retrieved from the CMS database.
def initialize(attributes = {})
update_data(ObjDataFromHash.new(attributes))
@@ -959,10 +984,55 @@
return true if has_conflict?
cms_data_for_revision(base_revision) != cms_data_for_revision(published_revision)
end
+ def self.prepare_attributes_for_rest_api(attributes, obj = nil)
+ widget_pool_attributes, obj_attributes = CmsRestApi::WidgetExtractor.call(attributes, obj)
+ obj_id = obj ? obj.id : obj_attributes.fetch(:_id)
+ workspace = obj ? obj.revision.workspace : Workspace.current
+
+ api_attributes = serialize_attributes(
+ obj_attributes, widget_pool_attributes, workspace, obj_id
+ )
+
+ [api_attributes, widget_pool_attributes]
+ end
+
+ def self.find_attribute_definitions(obj_class, basic_class = self)
+ basic_class.type_computer.compute_type(obj_class).attribute_definitions if obj_class
+ end
+
+ def self.with_default_id_attribute(attributes)
+ attributes.reverse_merge(_id: SecureRandom.hex(8))
+ end
+
+ private_class_method def self.serialize_attributes(obj_attributes, widget_pool_attributes, workspace, obj_id)
+ serializer = AttributeSerializer.new(obj_attributes['_obj_class'] || name, obj_id)
+ serialized_attributes = serialize_obj_attributes(serializer, obj_attributes)
+ serialized_attributes['_widget_pool'] =
+ serialize_widget_pool_attributes(serializer, widget_pool_attributes)
+ serialized_attributes
+ end
+
+ private_class_method def self.serialize_obj_attributes(serializer, obj_attributes)
+ serializer.serialize(obj_attributes,
+ find_attribute_definitions(obj_attributes['_obj_class']) || attribute_definitions)
+ end
+
+ private_class_method def self.serialize_widget_pool_attributes(serializer, widget_pool_attributes)
+ {}.tap do |serialized_attributes|
+ widget_pool_attributes.each_pair do |widget, widget_attributes|
+ obj_class = widget_attributes['_obj_class']
+ widget_serializer = AttributeSerializer.new(obj_class, serializer.obj_id)
+
+ serialized_attributes[widget.id] = widget_serializer.serialize(widget_attributes,
+ find_attribute_definitions(obj_class, BasicWidget) || widget.attribute_definitions)
+ end
+ end
+ end
+
private
def children_including_deleted
workspace.objs.find_by_parent_path_including_deleted(path)
end
@@ -1091,75 +1161,8 @@
widget_pool[id] = Widget.reset_blank_attributes(widget_attributes)
end
end
self.class.reset_blank_attributes(attributes)
- end
-
- class << self
- def assert_not_basic_obj(method_name)
- if self == Scrivito::BasicObj
- raise ScrivitoError, "Can not call #{method_name} on Scrivito::BasicObj."\
- " Only call it on Obj or subclasses of Obj"
- end
- end
-
- #
- # Restores a previously deleted +Obj+.
- #
- # @api public
- #
- # @raise [ScrivitoError] If the current workspace is +published+.
- def restore(obj_id)
- Workspace.current.assert_revertable
- base_revision_path = "revisions/#{Workspace.current.base_revision_id}/objs/#{obj_id}"
- obj_attributes = CmsRestApi.get(base_revision_path).merge('_id' => obj_id)
- Workspace.current.api_request(:post, '/objs', obj: obj_attributes)
- end
-
- def prepare_attributes_for_rest_api(attributes, obj = nil)
- widget_pool_attributes, obj_attributes = CmsRestApi::WidgetExtractor.call(attributes, obj)
- obj_id = obj ? obj.id : obj_attributes.fetch(:_id)
- workspace = obj ? obj.revision.workspace : Workspace.current
-
- api_attributes = serialize_attributes(
- obj_attributes, widget_pool_attributes, workspace, obj_id
- )
-
- [api_attributes, widget_pool_attributes]
- end
-
- def serialize_attributes(obj_attributes, widget_pool_attributes, workspace, obj_id)
- serializer = AttributeSerializer.new(obj_attributes['_obj_class'] || name, obj_id)
- serialized_attributes = serialize_obj_attributes(serializer, obj_attributes)
- serialized_attributes['_widget_pool'] =
- serialize_widget_pool_attributes(serializer, widget_pool_attributes)
- serialized_attributes
- end
-
- def serialize_obj_attributes(serializer, obj_attributes)
- serializer.serialize(obj_attributes,
- find_attribute_definitions(obj_attributes['_obj_class']) || attribute_definitions)
- end
-
- def serialize_widget_pool_attributes(serializer, widget_pool_attributes)
- {}.tap do |serialized_attributes|
- widget_pool_attributes.each_pair do |widget, widget_attributes|
- obj_class = widget_attributes['_obj_class']
- widget_serializer = AttributeSerializer.new(obj_class, serializer.obj_id)
-
- serialized_attributes[widget.id] = widget_serializer.serialize(widget_attributes,
- find_attribute_definitions(obj_class, BasicWidget) || widget.attribute_definitions)
- end
- end
- end
-
- def find_attribute_definitions(obj_class, basic_class = self)
- basic_class.type_computer.compute_type(obj_class).attribute_definitions if obj_class
- end
-
- def with_default_id_attribute(attributes)
- attributes.reverse_merge(_id: SecureRandom.hex(8))
- end
end
end
end