lib/scrivito/basic_obj.rb in scrivito_sdk-0.15.0 vs lib/scrivito/basic_obj.rb in scrivito_sdk-0.16.0
- old
+ new
@@ -68,23 +68,14 @@
# @api public
# @param [Hash] attributes
# @return [Obj] the newly created {BasicObj Obj}
def self.create(attributes)
attributes = with_default_obj_class(attributes)
-
- widget_hash = CmsRestApi::WidgetExtractor.call(attributes)
- converted_attributes = CmsRestApi::AttributeSerializer.convert(attributes)
-
- converted_attributes['_widget_pool'] =
- CmsRestApi::AttributeSerializer.generate_widget_pool_changes(widget_hash)
-
- json = CmsRestApi.post(cms_rest_api_path, obj: converted_attributes)
-
+ api_attributes, widget_properties = prepare_attributes_for_rest_api(attributes, nil)
+ json = CmsRestApi.post(cms_rest_api_path, obj: api_attributes)
obj = find(json['id'] || json['_id'])
-
- CmsRestApi::WidgetExtractor.notify_persisted_widgets(obj, widget_hash)
-
+ CmsRestApi::WidgetExtractor.notify_persisted_widgets(obj, widget_properties)
obj
end
# Create a new {BasicObj Obj} instance with the given values and attributes.
# Normally this method should not be used.
@@ -108,30 +99,30 @@
end
### FINDERS ####################
# Find a {BasicObj Obj} by its id.
- # If the paremeter is an Array containing ids, return a list of corresponding Objs.
+ # If the parameter is an Array containing ids, return a list of corresponding Objs.
# @param [String, Integer, Array<String, Integer>]id_or_list
# @return [Obj, Array<Obj>]
# @api public
def self.find(id_or_list)
- find_filtering_deleted(id_or_list, false)
+ Workspace.current.find_obj(id_or_list)
end
def self.find_by_id(id)
- find_objs_by(:id, [id]).first.first
+ Workspace.current.find_obj_by_id(id)
end
# Find a {BasicObj Obj} by its id.
- # If the paremeter is an Array containing ids, return a list of corresponding Objs.
+ # If the parameter is an Array containing ids, return a list of corresponding Objs.
# The results include deleted objects as well.
# @param [String, Integer, Array<String, Integer>]id_or_list
# @return [Obj, Array<Obj>]
# @api public
def self.find_including_deleted(id_or_list)
- find_filtering_deleted(id_or_list, true)
+ Workspace.current.find_obj_including_deleted(id_or_list)
end
# Returns a {ObjSearchEnumerator} with the given initial subquery consisting of the four arguments.
#
# Note that +field+ and +value+ can also be arrays for searching several fields or searching for several values.
@@ -145,11 +136,11 @@
# @param [String, Array<String>] value See {ObjSearchEnumerator#and} for details
# @param [Hash] boost See {ObjSearchEnumerator#and} for details
# @return [ObjSearchEnumerator]
# @api public
def self.where(field, operator, value, boost = nil)
- ObjSearchEnumerator.new(nil).and(field, operator, value, boost)
+ ObjSearchEnumerator.new.and(field, operator, value, boost)
end
# Returns a {ObjSearchEnumerator} of all {BasicObj Obj}s.
# If invoked on a subclass of Obj, the result will be restricted to instances of that subclass.
# @return [ObjSearchEnumerator]
@@ -174,17 +165,13 @@
# Returns +nil+ if no matching Obj exists.
# @param [String] path Path of the {BasicObj Obj}.
# @return [Obj]
# @api public
def self.find_by_path(path)
- find_objs_by(:path, [path]).first.first
+ Workspace.current.find_obj_by_path(path)
end
- def self.find_many_by_paths(pathes)
- find_objs_by(:path, pathes).map(&:first)
- end
-
# Find an {BasicObj Obj} with the given name.
# If several Objs with the given name exist, an arbitrary one of these Objs is chosen and returned.
# If no Obj with the name exits, +nil+ is returned.
# @param [String] name Name of the {BasicObj Obj}.
# @return [Obj]
@@ -204,11 +191,11 @@
# Returns the {BasicObj Obj} with the given permalink, or +nil+ if no matching Obj exists.
# @param [String] permalink The permalink of the {BasicObj Obj}.
# @return [Obj]
# @api public
def self.find_by_permalink(permalink)
- find_objs_by(:permalink, [permalink]).first.first
+ Workspace.current.find_obj_by_permalink(permalink)
end
# Returns the {BasicObj Obj} with the given permalink, or raise ResourceNotFound if no matching Obj exists.
# @param [String] permalink The permalink of the {BasicObj Obj}.
# @return [Obj]
@@ -216,33 +203,10 @@
def self.find_by_permalink!(permalink)
find_by_permalink(permalink) or
raise ResourceNotFound, "Could not find Obj with permalink '#{permalink}'"
end
- # accepts the name of an "obj_by" - view, a list of keys
- # and an "include_deleted" flag
- # returns a list of lists of Objs: a list of Objs for each given keys.
- def self.find_objs_by(view, keys, include_deleted = false)
- if include_deleted
- finder_method_name = :find_obj_data_including_deleted_by
- else
- finder_method_name = :find_obj_data_by
- end
-
- revision = Workspace.current.revision
- result = CmsBackend.instance.public_send(finder_method_name, revision, view, keys)
-
- result.map do |list|
- list.map do |obj_data|
- obj = BasicObj.instantiate(obj_data)
- obj.revision = revision
-
- obj
- end
- end
- end
-
# Hook method to control which page classes should be available for a page with given path.
# Override it to allow only certain classes or none.
# Must return either +NilClass+, or +Array+.
#
# Be aware that the given argument is a parent path.
@@ -264,31 +228,40 @@
# Update the {BasicObj Obj} with the attributes provided.
#
# For an overview of which values you can set via this method see the
# documentation of {BasicObj.create Obj.create}.
#
+ # Additionally, +update+ accepts a +_widget_pool+ hash in +attributes+ to modify widgets.
+ # The keys of +_widget_pool+ are widget instances, the values are the modified attributes of
+ # these particular widgets.
+ #
# @api public
# @param [Hash] attributes
+ # @example Move the +widget_to_move+ widget from the +left+ widget field of the +two_column_widget1+ widget to +left+ of +two_column_widget2+:
+ # obj.update(
+ # _widget_pool: {
+ # two_column_widget1 => {left: two_column_widget1.left - [widget_to_move]},
+ # two_column_widget2 => {left: two_column_widget2.left + [widget_to_move]}
+ # },
+ # headline: "Some widgets were moved!"
+ # )
+ #
+ # @example Move the +widget_to_move+ widget from the +right+ widget field of the +two_column_widget1+ widget to the top-level widget field +main_content+:
+ # obj.update(
+ # main_content: @obj.main_content + [widget_to_move],
+ # _widget_pool: {
+ # two_column_widget1 => {
+ # right: two_column_widget1.right - [widget_to_move]
+ # }
+ # }
+ # )
def update(attributes)
- widget_hash = CmsRestApi::WidgetExtractor.call(attributes, self)
- converted_attributes = CmsRestApi::AttributeSerializer.convert(attributes)
-
- converted_attributes['_widget_pool'] =
- CmsRestApi::AttributeSerializer.generate_widget_pool_changes(widget_hash)
-
- widget_pool = converted_attributes['_widget_pool']
- widget_gc = WidgetGarbageCollection.new(self, {self => attributes}.merge(widget_hash))
- widget_gc.widgets_to_delete.each { |widget| widget_pool[widget.id] = nil }
-
- CmsRestApi.put(cms_rest_api_path, obj: converted_attributes)
-
+ api_attributes, widget_properties = prepare_attributes_for_rest_api(attributes)
+ CmsRestApi.put(cms_rest_api_path, obj: api_attributes)
Workspace.reload
-
reload
-
- CmsRestApi::WidgetExtractor.notify_persisted_widgets(self, widget_hash)
-
+ CmsRestApi::WidgetExtractor.notify_persisted_widgets(self, widget_properties)
self
end
# Destroys the {BasicObj Obj} in the current {Workspace}
# @api public
@@ -323,20 +296,20 @@
ancestor_paths = parent_path.scan(/\/[^\/]+/).inject([""]) do |list, component|
list << list.last + component
end
ancestor_paths[0] = "/"
- BasicObj.find_many_by_paths(ancestor_paths)
+ Workspace.current.find_objs_by_paths(ancestor_paths)
end
# return a list of all child {BasicObj Obj}s.
# @return [Array<Obj>]
# @api public
def children
return [] unless path
- self.class.find_objs_by(:ppath, [path]).first
+ Workspace.current.find_objs_by_parent_path(path)
end
### ATTRIBUTES #################
# returns the {BasicObj Obj}'s path as a String.
@@ -788,10 +761,14 @@
def child_path?
!path.nil? && !root?
end
+ def prepare_attributes_for_rest_api(attributes)
+ self.class.prepare_attributes_for_rest_api(attributes, self)
+ end
+
class << self
def restore(obj_id)
Workspace.current.assert_revertable
base_revision_path = "revisions/#{Workspace.current.base_revision_id}/objs/#{obj_id}"
@@ -801,23 +778,28 @@
def cms_rest_api_path
"workspaces/#{Workspace.current.id}/objs"
end
- private
+ def prepare_attributes_for_rest_api(attributes, obj)
+ widget_properties = CmsRestApi::WidgetExtractor.call(attributes, obj)
+ api_attributes = CmsRestApi::AttributeSerializer.convert(attributes)
+ api_attributes['_widget_pool'] =
+ CmsRestApi::AttributeSerializer.generate_widget_pool_changes(widget_properties)
- def find_filtering_deleted(id_or_list, include_deleted)
- case id_or_list
- when Array
- find_objs_by(:id, id_or_list, include_deleted).map(&:first).compact
- else
- obj = find_objs_by(:id, [id_or_list.to_s], include_deleted).first.first
- obj or raise ResourceNotFound, "Could not find Obj with id #{id_or_list}"
+ if obj
+ widget_pool = api_attributes['_widget_pool']
+ widget_gc = WidgetGarbageCollection.new(obj, {obj => attributes}.merge(widget_properties))
+ widget_gc.widgets_to_delete.each { |widget| widget_pool[widget.id] = nil }
end
+
+ [api_attributes, widget_properties]
end
+ private
+
def search_for_all
- ObjSearchEnumerator.new(nil).batch_size(1000)
+ ObjSearchEnumerator.new.batch_size(1000)
end
end
end
end