lib/scrivito/basic_obj.rb in scrivito_sdk-0.16.0 vs lib/scrivito/basic_obj.rb in scrivito_sdk-0.17.0
- old
+ new
@@ -40,10 +40,16 @@
# Link.new(:url => "http://www.example.com", :title => "Example"),
# # internal link
# Link.new(:obj => other_obj, :title => "Other Obj")
# ])
#
+ # @example Passing a {Link Link} allows you to set a link.
+ # Obj.create(
+ # external_link: Link.new(url: 'http://www.example.com', title: 'Example')
+ # internal_link: Link.new(obj: other_obj, title: 'Other Obj')
+ # )
+ #
# @example Dates attributes accept Time, Date and their subclasses (DateTime for example)
# Obj.create(:date => Time.new)
# Obj.create(:date => Date.now)
#
# @example String, text, html and enum can be set by passing a {String} value
@@ -72,10 +78,11 @@
attributes = with_default_obj_class(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_properties)
+ Workspace.current.reload
obj
end
# Create a new {BasicObj Obj} instance with the given values and attributes.
# Normally this method should not be used.
@@ -104,25 +111,25 @@
# 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)
- Workspace.current.find_obj(id_or_list)
+ Workspace.current.objs.find(id_or_list)
end
def self.find_by_id(id)
- Workspace.current.find_obj_by_id(id)
+ Workspace.current.objs.find_by_id(id)
end
# Find a {BasicObj Obj} by its id.
# 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)
- Workspace.current.find_obj_including_deleted(id_or_list)
+ Workspace.current.objs.find_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.
@@ -136,40 +143,40 @@
# @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.and(field, operator, value, boost)
+ Workspace.current.objs.where(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]
# @api public
def self.all
if superclass == Scrivito::BasicObj
- search_for_all
+ Workspace.current.objs.all
else
find_all_by_obj_class(name)
end
end
# Returns a {ObjSearchEnumerator} of all Objs with the given +obj_class+.
# @param [String] obj_class Name of the ObjClass.
# @return [ObjSearchEnumerator]
# @api public
def self.find_all_by_obj_class(obj_class)
- search_for_all.and(:_obj_class, :equals, obj_class)
+ Workspace.current.objs.find_all_by_obj_class(obj_class)
end
# Find the {BasicObj Obj} with the given path.
# 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)
- Workspace.current.find_obj_by_path(path)
+ Workspace.current.objs.find_by_path(path)
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.
@@ -191,11 +198,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)
- Workspace.current.find_obj_by_permalink(permalink)
+ Workspace.current.objs.find_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]
@@ -255,11 +262,11 @@
# }
# )
def update(attributes)
api_attributes, widget_properties = prepare_attributes_for_rest_api(attributes)
CmsRestApi.put(cms_rest_api_path, obj: api_attributes)
- Workspace.reload
+ workspace.reload
reload
CmsRestApi::WidgetExtractor.notify_persisted_widgets(self, widget_properties)
self
end
@@ -270,11 +277,11 @@
raise ClientError.new(I18n.t('scrivito.errors.models.basic_obj.has_children'), 412)
end
CmsRestApi.delete(cms_rest_api_path)
- Workspace.reload
+ workspace.reload
end
def to_param
id
end
@@ -296,20 +303,20 @@
ancestor_paths = parent_path.scan(/\/[^\/]+/).inject([""]) do |list, component|
list << list.last + component
end
ancestor_paths[0] = "/"
- Workspace.current.find_objs_by_paths(ancestor_paths)
+ Workspace.current.objs.find_by_paths(ancestor_paths)
end
# return a list of all child {BasicObj Obj}s.
# @return [Array<Obj>]
# @api public
def children
return [] unless path
- Workspace.current.find_objs_by_parent_path(path)
+ workspace.objs.find_by_parent_path(path)
end
### ATTRIBUTES #################
# returns the {BasicObj Obj}'s path as a String.
@@ -462,19 +469,29 @@
else
super
end
end
+ def has_attribute?(key)
+ key = key.to_s
+
+ if INTERNAL_KEYS.include?(key)
+ true
+ else
+ super
+ end
+ end
+
# Reloads the attributes of this object from the database.
# Notice that the ruby class of this Obj instance will NOT change,
# even if the obj_class in the database has changed.
# @api public
def reload
id = self.id.to_s
reload_data = Proc.new do
- CmsBackend.instance.find_obj_data_by(Workspace.current.revision, :id, [id]).first.first
+ CmsBackend.instance.find_obj_data_by(workspace.revision, :id, [id]).first.first
end
update_data(reload_data)
end
@@ -482,13 +499,15 @@
# @api public
def obj_class_name
read_attribute('_obj_class')
end
+ # Returns the {Scrivito::ObjClass} of this object.
+ # @return [Scrivito::ObjClass]
+ # @api public
def obj_class
- raise ScrivitoError, "BasicObj#obj_class is no longer available"+
- ", please use BasicObj#obj_class_name instead."
+ ObjClass.find(obj_class_name)
end
# @api public
def last_changed
read_attribute('_last_changed')
@@ -510,11 +529,11 @@
if read_attribute('_modification') == 'deleted'
cms_data_for_revision(revision).present?
end
end
- def modification(revision=Workspace.current.base_revision)
+ def modification(revision=workspace.base_revision)
return Modification::UNMODIFIED unless revision
obj_data_from_revision = cms_data_for_revision(revision)
if deleted?(revision)
@@ -644,21 +663,21 @@
# Reverts changes of this object.
# After calling this method it's as if this object has been never modified in the current working copy.
# This method does not work with +new+ or +deleted+ objects.
# This method also does also not work for the +published+ workspace or the +rtc+ working copy.
def revert
- Workspace.current.assert_revertable
+ workspace.assert_revertable
if binary?
raise "revert not supported for binary objs"
else
case modification
when Modification::UNMODIFIED
# don't do anything
when Modification::EDITED
previous_content = CmsRestApi.get(
- "revisions/#{Workspace.current.base_revision_id}/objs/#{id}")
+ "revisions/#{workspace.base_revision_id}/objs/#{id}")
updated_content = previous_content.except('id', '_id')
added_widget_ids = read_widget_pool.keys - previous_content['_widget_pool'].keys
added_widget_ids.each do |added_widget_id|
updated_content['_widget_pool'][added_widget_id] = nil
@@ -754,13 +773,21 @@
blob_spec = read_attribute('blob')
Blob.find(blob_spec["id"]) if blob_spec
end
def cms_rest_api_path(obj_id = id)
- "#{self.class.cms_rest_api_path}/#{obj_id}"
+ "#{self.class.cms_rest_api_path(workspace)}/#{obj_id}"
end
+ def workspace
+ if revision.workspace
+ revision.workspace
+ else
+ raise ScrivitoError, "No workspace set for this obj"
+ end
+ end
+
def child_path?
!path.nil? && !root?
end
def prepare_attributes_for_rest_api(attributes)
@@ -774,12 +801,12 @@
base_revision_path = "revisions/#{Workspace.current.base_revision_id}/objs/#{obj_id}"
obj_attributes = CmsRestApi.get(base_revision_path).except('id').merge('_id' => obj_id)
CmsRestApi.post(cms_rest_api_path, obj: obj_attributes)
end
- def cms_rest_api_path
- "workspaces/#{Workspace.current.id}/objs"
+ def cms_rest_api_path(workspace = Workspace.current)
+ "workspaces/#{workspace.id}/objs"
end
def prepare_attributes_for_rest_api(attributes, obj)
widget_properties = CmsRestApi::WidgetExtractor.call(attributes, obj)
api_attributes = CmsRestApi::AttributeSerializer.convert(attributes)
@@ -791,15 +818,9 @@
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.batch_size(1000)
end
end
end
end