Class: Scrivito::Workspace

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

Instance Method Summary collapse

Class Method Details

.allArray<Scrivito::Workspace>

Returns all workspaces.

Returns:



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.

Examples:

Create a workspace, providing its title:

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

Parameters:

  • attributes (Hash)

Returns:



138
139
140
# File 'lib/scrivito/workspace.rb', line 138

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
# 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.

Parameters:



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.

Parameters:

  • id (String)

Returns:

Raises:



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.

Parameters:

  • title (String)

Returns:



95
96
97
# File 'lib/scrivito/workspace.rb', line 95

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

.publishedScrivito::Workspace

Returns the published workspace.

Returns:



55
56
57
# File 'lib/scrivito/workspace.rb', line 55

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.



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.

Examples:

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")

Parameters:

  • id_or_title (String)

    id or title of the workspace

Raises:



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_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.



168
169
170
# File 'lib/scrivito/workspace.rb', line 168

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

#destroyObject

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

#idString

Returns the id of the workspace.

Returns:

  • (String)


258
259
260
# File 'lib/scrivito/workspace.rb', line 258

def id
  @id
end

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

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

#publishObject

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.

Returns:

  • (Boolean)

    true if the workspace is the published one



294
295
296
# File 'lib/scrivito/workspace.rb', line 294

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

#rebaseObject

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

#reloadObject

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

#titleString

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

Returns:

  • (String)


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.

Parameters:

  • attributes (Hash)

Returns:

Raises:



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