Class: Scrivito::Workspace

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idString (readonly)

Returns the id of the workspace.

Returns:

  • (String)


268
269
270
# File 'app/cms/scrivito/workspace.rb', line 268

def id
  @id
end

Class Method Details

.allArray<Scrivito::Workspace>

Returns all workspaces.

Returns:



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.

Examples:

Create a workspace, providing its title:

Scrivito::Workspace.create(title: "Jane")

Create a workspace, providing auto_update:

# Workspace will not be updated until it is published.
Scrivito::Workspace.create(auto_update: false)

Parameters:

  • attributes (Hash)

Options Hash (attributes):

  • :title (String)

    title of the workspace.

  • :auto_update (Boolean)

    Specifies whether changes published intermediately are applied automatically and instantly to the working copy. The default is true. Setting this option to false requires rebasing the working copy (manually) to keep it up to date.

Returns:



146
147
148
# File 'app/cms/scrivito/workspace.rb', line 146

def self.create(attributes)
  find(create_async(attributes).result['id'])
end

.currentScrivito::Workspace

Returns the currently used workspace.

Returns:



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.

Parameters:



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.

Parameters:

  • id (String)

Returns:

Raises:



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.

Parameters:

  • title (String)

Returns:



96
97
98
# File 'app/cms/scrivito/workspace.rb', line 96

def self.find_by_title(title)
  all.detect { |workspace| workspace.title == title }
end

.publishedScrivito::Workspace

Returns the published workspace.

Returns:



56
57
58
# File 'app/cms/scrivito/workspace.rb', line 56

def self.published
  find("published")
end

.reloadObject

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

Note:

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.

Examples:

Scrivito::Workspace.use("my working copy")
Scrivito::Workspace.current.title
# => "my working copy"

Scrivito::Workspace.use("6a75fe694eeeb093")
Scrivito::Workspace.current.id
# => "6a75fe694eeeb093"

# Raises Scrivito::ResourceNotFound:
Scrivito::Workspace.use("missing")

Parameters:

  • title_or_id (String)

    title or id of the workspace

Raises:



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_keyString

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.

Returns:

  • (String)

    A string that changes whenever the content of the working copy changes.



177
178
179
# File 'app/cms/scrivito/workspace.rb', line 177

def cache_key
  @cache_key ||= Digest::SHA1.hexdigest("#{id}|#{content_state_id}")
end

#destroyObject

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

#membershipsScrivito::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

#objsScrivito::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

#publishObject

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.

Returns:

  • (Boolean)

    true if the workspace is the published one



302
303
304
# File 'app/cms/scrivito/workspace.rb', line 302

def published?
  self.id == 'published'
end

#rebaseObject

Note:

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

#reloadObject

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

#titleString

Returns the title of the workspace if present. Otherwise, and for the published content, an empty String is returned.

Returns:

  • (String)


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.

Parameters:

  • attributes (Hash)

Returns:

Raises:



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