lib/scrivito/workspace.rb in scrivito_sdk-0.17.0 vs lib/scrivito/workspace.rb in scrivito_sdk-0.18.0
- old
+ new
@@ -7,10 +7,12 @@
extend ActiveModel::Naming
include ModelIdentity
+ PublishPreventedDueToContentChange = Class.new(ScrivitoError)
+
# Set the currently used workspace
# @api public
# @param [Scrivito::Workspace] workspace
def self.current=(workspace)
@current = workspace
@@ -106,21 +108,30 @@
# Reloads this workspace to reflect any changes to it that may have happened concurrently since
# it was loaded
# @api public
def reload
@workspace_data = CmsBackend.instance.find_workspace_data_by_id(self.id)
- @revision = @base_revision = @memberships = nil
+
+ # Clear all cached instance variables.
+ @base_revision = nil
+ @memberships = nil
+ @revision = nil
end
+ def api_request(verb, path, payload = nil)
+ response = CmsRestApi.public_send(verb, "#{backend_url}#{path}", payload)
+ reload if [:post, :put, :delete].include?(verb)
+ response
+ end
+
# Updates this workspace's attributes
# @api public
# @param [Hash] attributes
# @return [Scrivito::Workspace]
def update(attributes)
CmsRestApi.put(backend_url, workspace: attributes)
-
- self.reload
+ reload
end
# Destroy this workspace
# @api public
def destroy
@@ -129,21 +140,28 @@
end
# Publish the changes of this workspace
# @api public
def publish
- CmsRestApi.put("#{backend_url}/publish", {})
+ internal_publish
+ end
- reset_workspace_if_current
+ def conditional_publish
+ internal_publish(if_content_state_id_equals: content_state.content_state_id)
+ rescue Scrivito::ClientError => e
+ if e.message.starts_with?('The specified condition for content_state_id')
+ raise PublishPreventedDueToContentChange.new(e.message)
+ else
+ raise e
+ end
end
# Rebases the current workspace on the published content
# @api public
def rebase
CmsRestApi.put("#{backend_url}/rebase", {})
-
- self.reload
+ reload
end
# Returns the id of the workspace
# @api public
# @return [String]
@@ -160,11 +178,11 @@
# @return [String]
def title
@workspace_data.title
end
- # @api beta
+ # @api public
# Returns the members of this workspace and their roles
#
# @return [MembershipsCollection]
def memberships
@memberships ||= MembershipsCollection.new(self)
@@ -214,14 +232,40 @@
# @return {ObjsCollection}
def objs
@objs ||= ObjsCollection.new(self)
end
+ # Returns all obj classes of this working copy.
+ #
+ # @api public
+ #
+ # @example Find the obj class named "Homepage" in the "rtc" {Workspace}.
+ # Workspace.find('rtc').obj_classes['Homepage']
+ #
+ # @return {ObjClassCollection}
+ def obj_classes
+ @obj_classes ||= ObjClassCollection.new(self)
+ end
+
def inspect
"<#{self.class} id=\"#{id}\" title=\"#{title}\">"
end
+ def as_json(options = {})
+ {
+ id: id,
+ title: title,
+ memberships: memberships,
+ }
+ end
+
private
+
+ def internal_publish(data={})
+ CmsRestApi.put("#{backend_url}/publish", data)
+
+ reset_workspace_if_current
+ end
def backend_url
"/workspaces/#{id}"
end