Class: Scrivito::Workspace
- Inherits:
-
Object
- Object
- Scrivito::Workspace
- Extended by:
- ActiveModel::Naming
- Defined in:
- app/cms/scrivito/workspace.rb,
app/cms/scrivito/workspace/publish_checker.rb
Overview
This class represents a CMS workspace, called “working copy” in the UI. A working copy lets editors create and modify content independently of the published content or other working copies. On creation, a working copy is based on the currently published content.
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
Returns the id of the workspace.
Class Method Summary collapse
-
.all ⇒ Array<Scrivito::Workspace>
Returns all workspaces.
-
.create(attributes) ⇒ Scrivito::Workspace
Create a new workspace.
-
.current ⇒ Scrivito::Workspace
Returns the currently used workspace.
-
.current=(workspace) ⇒ Object
Set the workspace to use for subsequent workspace operations.
-
.find(id) ⇒ Scrivito::Workspace
Find a workspace by its id.
-
.find_by_title(title) ⇒ Scrivito::Workspace
Find a workspace by its title.
-
.published ⇒ Scrivito::Workspace
Returns the published workspace.
-
.reload ⇒ Object
Reloads the current workspace to reflect the changes that were made to it concurrently since it was loaded.
-
.use(title_or_id) ⇒ Object
Find a workspace by its title or ID and set it as the currently used workspace.
Instance Method Summary collapse
-
#cache_key ⇒ String
This method provides a string that can be used as part of a cache key.
-
#destroy ⇒ Object
Destroy this workspace.
-
#memberships ⇒ Scrivito::MembershipCollection
Returns the memberships (users and their roles) of this workspace.
-
#objs ⇒ Scrivito::ObjCollection
Returns an ObjCollection of this working copy for accessing its CMS objects.
-
#publish ⇒ Object
Publish the changes that were made to this workspace.
-
#published? ⇒ Boolean
This method indicates whether the workspace is the published one.
-
#rebase ⇒ Object
Rebases the current workspace from the published content in order to integrate the changes that were published in the meantime.
-
#reload ⇒ Object
Reloads this workspace to reflect the changes that were made to it concurrently since it was loaded.
-
#title ⇒ String
Returns the title of the workspace if present.
-
#update(attributes) ⇒ Scrivito::Workspace
Updates the attributes of this workspace.
Instance Attribute Details
#id ⇒ String (readonly)
Returns the id of the workspace.
268 269 270 |
# File 'app/cms/scrivito/workspace.rb', line 268 def id @id end |
Class Method Details
.all ⇒ Array<Scrivito::Workspace>
Returns all workspaces.
44 45 46 47 48 49 50 |
# File 'app/cms/scrivito/workspace.rb', line 44 def self.all result_json = CmsRestApi.get('/workspaces') result_json['results'].map do |raw_data| Workspace.new(WorkspaceData.new(raw_data)) end end |
.create(attributes) ⇒ Scrivito::Workspace
Create a new workspace.
146 147 148 |
# File 'app/cms/scrivito/workspace.rb', line 146 def self.create(attributes) find(create_async(attributes).result['id']) end |
.current ⇒ Scrivito::Workspace
Returns the currently used workspace.
32 33 34 35 36 37 38 39 |
# File 'app/cms/scrivito/workspace.rb', line 32 def self.current workspace = Thread.current[:scrivito_current_workspace] if workspace.respond_to?(:call) Thread.current[:scrivito_current_workspace] = workspace.call else Thread.current[:scrivito_current_workspace] ||= published end end |
.current=(workspace) ⇒ Object
Set the workspace to use for subsequent workspace operations.
21 22 23 |
# File 'app/cms/scrivito/workspace.rb', line 21 def self.current=(workspace) Thread.current[:scrivito_current_workspace] = workspace end |
.find(id) ⇒ Scrivito::Workspace
Find a workspace by its id.
82 83 84 85 86 87 88 |
# File 'app/cms/scrivito/workspace.rb', line 82 def self.find(id) cache.fetch(id) do workspace_data = CmsBackend.find_workspace_data_by_id(id) from_workspace_data(id, workspace_data) end end |
.find_by_title(title) ⇒ Scrivito::Workspace
Find a workspace by its title. If multiple workspaces share the same title, one of them is returned. If no workspace with the given title can be found, nil
is returned.
96 97 98 |
# File 'app/cms/scrivito/workspace.rb', line 96 def self.find_by_title(title) all.detect { |workspace| workspace.title == title } end |
.published ⇒ Scrivito::Workspace
Returns the published workspace.
56 57 58 |
# File 'app/cms/scrivito/workspace.rb', line 56 def self.published find("published") end |
.reload ⇒ Object
Reloads the current workspace to reflect the changes that were made to it concurrently since it was loaded.
160 161 162 |
# File 'app/cms/scrivito/workspace.rb', line 160 def self.reload current.reload end |
.use(title_or_id) ⇒ Object
This method is intended to be used in the Rails console. Please avoid using it in application code.
Find a workspace by its title or ID and set it as the currently used workspace.
122 123 124 125 126 |
# File 'app/cms/scrivito/workspace.rb', line 122 def self.use(title_or_id) self.current = find_by_title(title_or_id) || find(title_or_id) rescue ResourceNotFound raise ResourceNotFound, %{Could not find #{self} with title or ID "#{title_or_id}"} end |
Instance Method Details
#cache_key ⇒ String
This method provides a string that can be used as part of a cache key. It changes whenever any content (Obj or Widget) changes. Due to this, caches using the cache_key
are invalidated whenever a CMS object in the working copy has been changed.
Scrivito provides the scrivito_cache method which integrates the cache_key
with Rails’ fragment caching. You might want to check whether scrivito_cache
satisfies your needs before implementing your own solution.
177 178 179 |
# File 'app/cms/scrivito/workspace.rb', line 177 def cache_key @cache_key ||= Digest::SHA1.hexdigest("#{id}|#{content_state_id}") end |
#destroy ⇒ Object
Destroy this workspace.
231 232 233 234 |
# File 'app/cms/scrivito/workspace.rb', line 231 def destroy reset_workspace_if_current CmsRestApi.delete(backend_url) end |
#memberships ⇒ Scrivito::MembershipCollection
Returns the memberships (users and their roles) of this workspace.
286 287 288 |
# File 'app/cms/scrivito/workspace.rb', line 286 def memberships @memberships ||= MembershipCollection.new(self) end |
#objs ⇒ Scrivito::ObjCollection
Returns an ObjCollection of this working copy for accessing its CMS objects.
350 351 352 |
# File 'app/cms/scrivito/workspace.rb', line 350 def objs @objs ||= ObjCollection.new(self) end |
#publish ⇒ Object
Publish the changes that were made to this workspace.
238 239 240 241 242 |
# File 'app/cms/scrivito/workspace.rb', line 238 def publish publish_async.result Workspace.published.reload reset_workspace_if_current end |
#published? ⇒ Boolean
This method indicates whether the workspace is the published one.
302 303 304 |
# File 'app/cms/scrivito/workspace.rb', line 302 def published? self.id == 'published' end |
#rebase ⇒ Object
This action is not required for auto-updated workspaces.
Rebases the current workspace from the published content in order to integrate the changes that were published in the meantime.
254 255 256 257 |
# File 'app/cms/scrivito/workspace.rb', line 254 def rebase rebase_async.result reload end |
#reload ⇒ Object
Reloads this workspace to reflect the changes that were made to it concurrently since it was loaded.
193 194 195 |
# File 'app/cms/scrivito/workspace.rb', line 193 def reload update_data(method(:fetch_workspace_data)) end |
#title ⇒ String
Returns the title of the workspace if present. Otherwise, and for the published content, an empty String
is returned.
278 279 280 281 |
# File 'app/cms/scrivito/workspace.rb', line 278 def title return '' if published? data.title || '' end |
#update(attributes) ⇒ Scrivito::Workspace
Updates the attributes of this workspace.
223 224 225 226 227 |
# File 'app/cms/scrivito/workspace.rb', line 223 def update(attributes) raise ScrivitoError, 'published workspace is not modifiable' if published? CmsRestApi.put(backend_url, workspace: attributes) reload end |