Class: Scrivito::Workspace
- Inherits:
-
Object
- Object
- Scrivito::Workspace
- Extended by:
- ActiveModel::Naming
- Defined in:
- lib/scrivito/workspace.rb,
lib/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.
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(id_or_title)
Find a workspace by its id or title 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.
-
#id ⇒ String
Returns the id of the 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.
Class Method Details
.all ⇒ Array<Scrivito::Workspace>
Returns all workspaces.
43 44 45 46 47 48 49 |
# File 'lib/scrivito/workspace.rb', line 43 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.
138 139 140 |
# File 'lib/scrivito/workspace.rb', line 138 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 |
# File 'lib/scrivito/workspace.rb', line 32 def self.current if @current.respond_to? :call @current = @current.call else @current ||= published end end |
.current=(workspace) ⇒ Object
Set the workspace to use for subsequent workspace operations.
21 22 23 |
# File 'lib/scrivito/workspace.rb', line 21 def self.current=(workspace) @current = workspace end |
.find(id) ⇒ Scrivito::Workspace
Find a workspace by its id.
81 82 83 84 85 86 87 |
# File 'lib/scrivito/workspace.rb', line 81 def self.find(id) cache.fetch(id) do workspace_data = CmsBackend.instance.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.
95 96 97 |
# File 'lib/scrivito/workspace.rb', line 95 def self.find_by_title(title) all.detect { |workspace| workspace.title == title } end |
.published ⇒ Scrivito::Workspace
Returns the published workspace.
55 56 57 |
# File 'lib/scrivito/workspace.rb', line 55 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.
151 152 153 |
# File 'lib/scrivito/workspace.rb', line 151 def self.reload current.reload end |
.use(id_or_title)
This method returns an undefined value.
Find a workspace by its id or title and set it as the currently used workspace.
120 121 122 123 124 125 126 127 |
# File 'lib/scrivito/workspace.rb', line 120 def self.use(id_or_title) self.current = if id_or_title =~ /^[a-z0-9]{16}$/ find(id_or_title) else find_by_title(id_or_title) or raise ResourceNotFound, "Could not find #{self} with title #{id_or_title}" end 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.
168 169 170 |
# File 'lib/scrivito/workspace.rb', line 168 def cache_key @cache_key ||= Digest::SHA1.hexdigest("#{id}|#{content_state_id}") end |
#destroy ⇒ Object
Destroy this workspace.
222 223 224 225 |
# File 'lib/scrivito/workspace.rb', line 222 def destroy reset_workspace_if_current CmsRestApi.delete(backend_url) end |
#id ⇒ String
Returns the id of the workspace.
258 259 260 |
# File 'lib/scrivito/workspace.rb', line 258 def id @id end |
#memberships ⇒ Scrivito::MembershipCollection
Returns the memberships (users and their roles) of this workspace.
278 279 280 |
# File 'lib/scrivito/workspace.rb', line 278 def memberships @memberships ||= MembershipCollection.new(self) end |
#objs ⇒ Scrivito::ObjCollection
Returns an ObjCollection of this working copy for accessing its CMS objects.
342 343 344 |
# File 'lib/scrivito/workspace.rb', line 342 def objs @objs ||= ObjCollection.new(self) end |
#publish ⇒ Object
Publish the changes that were made to this workspace.
229 230 231 232 233 |
# File 'lib/scrivito/workspace.rb', line 229 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.
294 295 296 |
# File 'lib/scrivito/workspace.rb', line 294 def published? self.id == 'published' end |
#rebase ⇒ Object
Rebases the current workspace from the published content in order to integrate the changes that were published in the meantime.
244 245 246 247 |
# File 'lib/scrivito/workspace.rb', line 244 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.
184 185 186 |
# File 'lib/scrivito/workspace.rb', line 184 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.
270 271 272 273 |
# File 'lib/scrivito/workspace.rb', line 270 def title return '' if published? data.title || '' end |
#update(attributes) ⇒ Scrivito::Workspace
Updates the attributes of this workspace.
214 215 216 217 218 |
# File 'lib/scrivito/workspace.rb', line 214 def update(attributes) raise ScrivitoError, 'published workspace is not modifiable' if published? CmsRestApi.put(backend_url, workspace: attributes) reload end |