lib/scrivito/workspace.rb in scrivito_sdk-0.65.2 vs lib/scrivito/workspace.rb in scrivito_sdk-0.66.0.rc1
- old
+ new
@@ -3,12 +3,10 @@
module Scrivito
# This class represents a CMS workspace
# @api public
class Workspace
- PUBLISHED_ID = 'published'
-
extend ActiveModel::Naming
include ModelIdentity
PublishPreventedDueToContentChange = Class.new(ScrivitoError)
@@ -29,45 +27,43 @@
# @return [Scrivito::Workspace]
def self.current
if @current.respond_to? :call
@current = @current.call
else
- @current ||= default
+ @current ||= published
end
end
# Returns all the workspaces
# @api public
# @return [Array<Scrivito::Workspace>]
def self.all
result_json = CmsRestApi.get('/workspaces')
- result_json['results'].map do |workspace_json|
- Workspace.find(workspace_json['id'])
+ result_json['results'].map do |raw_data|
+ Workspace.new(WorkspaceData.new(raw_data))
end
end
- def self.default
- published
- end
-
def self.published
- cache.fetch(PUBLISHED_ID) do
- cache[PUBLISHED_ID] = find(PUBLISHED_ID)
- end
+ find("published")
end
# Find a workspace by its id
# @api public
# @param [String] id
# @return [Scrivito::Workspace]
# @raise [Scrivito::ResourceNotFound]
def self.find(id)
- if workspace_data = CmsBackend.instance.find_workspace_data_by_id(id)
- Workspace.new workspace_data
- else
- raise ResourceNotFound, "Could not find #{self} with id #{id}"
+ cache.fetch(id) do
+ workspace_data = CmsBackend.instance.find_workspace_data_by_id(id)
+
+ unless workspace_data
+ raise ResourceNotFound, "Could not find #{self} with id #{id}"
+ end
+
+ cache[id] = Workspace.new(workspace_data)
end
end
# Find a workspace by its title.
# If multiple workspaces share the same title, one of them is returned.
@@ -77,12 +73,42 @@
# @return [Scrivito::Workspace]
def self.find_by_title(title)
all.detect { |workspace| workspace.title == title }
end
+ # Find a workspace by its id or title and set it as the currently used workspace.
+ # @example
+ # Scrivito::Workspace.use('6a75fe694eeeb093')
+ #
+ # Scrivito::Workspace.current.id
+ # # => '6a75fe694eeeb093'
+ #
+ # Scrivito::Workspace.use('my working copy')
+ #
+ # Scrivito::Workspace.current.title
+ # # => 'my working copy'
+ #
+ # # raises Scrivito::ResourceNotFound:
+ # Scrivito::Workspace.use('missing')
+ # @api public
+ # @param [String] id_or_title
+ # @return [void]
+ # @raise [Scrivito::ResourceNotFound]
+ def self.use(id_or_title)
+ workspace = begin
+ find(id_or_title)
+ rescue ResourceNotFound
+ find_by_title(id_or_title)
+ end
+ if workspace.blank?
+ raise(ResourceNotFound, "Could not find #{self} with id or title #{id_or_title}")
+ end
+ self.current = workspace
+ end
+
delegate :content_state_id, :base_content_state_id, :content_state,
- :base_revision_id, :base_content_state, :uses_obj_classes, to: :data
+ :base_revision_id, :base_content_state, to: :data
# Create a new workspace
# @api public
# @param [Hash] attributes
# @return [Scrivito::Workspace]
@@ -94,12 +120,11 @@
# reloads the current workspace to reflect any changes to it that may have happened concurrently
# since it was loaded
# @api public
def self.reload
- id = current.id
- self.current_using_proc = proc { find(id) }
+ current.reload
end
def self.cache
@cache ||= {}
end
@@ -133,10 +158,11 @@
# Updates this workspace's attributes
# @api public
# @param [Hash] attributes
# @return [Scrivito::Workspace]
def update(attributes)
+ raise ScrivitoError, 'published workspace is not modifiable' if published?
CmsRestApi.put(backend_url, workspace: attributes)
reload
end
# Destroy this workspace
@@ -149,10 +175,12 @@
# Publish the changes of this workspace
# @api public
def publish
CmsRestApi.put("#{backend_url}/publish", {})
+ Workspace.published.reload
+
reset_workspace_if_current
end
# Rebases the current workspace on the published content
# @api public
@@ -170,15 +198,16 @@
def revision_id
@workspace_data.revision_id
end
- # Returns the title of the workspace
+ # Returns the title of the workspace or an empty +String+
# @api public
# @return [String]
def title
- @workspace_data.title
+ return '' if published?
+ @workspace_data.title || ''
end
# @api public
# Returns the members of this workspace and their roles
#
@@ -193,19 +222,22 @@
def published?
self.id == 'published'
end
- def rtc?
- self.id == 'rtc'
- end
-
def outdated?
!published? && Workspace.published.revision.id != base_revision_id
end
def revision
+ unless data.content_state_id?
+ # reload data from changes feed in order to obtain content_state_id
+ reload
+
+ raise InternalError unless data.content_state_id?
+ end
+
@revision ||= Revision.new(id: revision_id, workspace: self)
end
def base_revision
if base_revision_id
@@ -229,33 +261,19 @@
end
end
def assert_revertable
raise ScrivitoError, 'published workspace is not modifiable' if published?
- raise ScrivitoError, 'rtc workspace may contain attribute and class changes' if rtc?
end
# {ObjCollection} for this working copy
# @api public
# @return {ObjCollection}
def objs
@objs ||= ObjCollection.new(self)
end
- # Returns all obj classes of this working copy.
- #
- # @api public
- # @deprecated
- #
- # @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 has_modification_for?(obj_id)
@@ -279,13 +297,14 @@
def backend_url
"/workspaces/#{id}"
end
def reset_workspace_if_current
+ Workspace.cache.delete(id)
+
if Workspace.current == self
- Workspace.current = Workspace.default
+ Workspace.current = Workspace.published
end
end
-
end
end