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:



134
135
136
137
138
# File 'lib/scrivito/workspace.rb', line 134

def self.create(attributes)
  workspace_json = CmsRestApi.post("/workspaces", workspace: attributes)

  self.find(workspace_json["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:



77
78
79
80
81
82
83
# File 'lib/scrivito/workspace.rb', line 77

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:



91
92
93
# File 'lib/scrivito/workspace.rb', line 91

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

.reloadObject

Reloads the current workspace to reflect the changes that were made to it concurrently since it was loaded.



143
144
145
# File 'lib/scrivito/workspace.rb', line 143

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)

Raises:



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/scrivito/workspace.rb', line 113

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

Instance Method Details

#destroyObject

Destroy this workspace.



189
190
191
192
# File 'lib/scrivito/workspace.rb', line 189

def destroy
  reset_workspace_if_current
  CmsRestApi.delete(backend_url)
end

#idString

Returns the id of the workspace.

Returns:

  • (String)


215
216
217
# File 'lib/scrivito/workspace.rb', line 215

def id
  @id
end

#membershipsMembershipCollection

Returns the memberships (users and their roles) of this workspace.



235
236
237
# File 'lib/scrivito/workspace.rb', line 235

def memberships
  @memberships ||= MembershipCollection.new(self)
end

#objsObjCollection

Returns an ObjCollection of this working copy for accessing its CMS objects.

Returns:



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

def objs
  @objs ||= ObjCollection.new(self)
end

#publishObject

Publish the changes that were made to this workspace.



196
197
198
199
200
201
202
# File 'lib/scrivito/workspace.rb', line 196

def publish
  CmsRestApi.put("#{backend_url}/publish", {})

  Workspace.published.reload

  reset_workspace_if_current
end

#rebaseObject

Rebases the current workspace from the published content in order to integrate the changes that were published in the meantime.



207
208
209
210
# File 'lib/scrivito/workspace.rb', line 207

def rebase
  CmsRestApi.put("#{backend_url}/rebase", {})
  reload
end

#reloadObject

Reloads this workspace to reflect the changes that were made to it concurrently since it was loaded.



159
160
161
# File 'lib/scrivito/workspace.rb', line 159

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)


227
228
229
230
# File 'lib/scrivito/workspace.rb', line 227

def title
  return '' if published?
  data.title || ''
end

#update(attributes) ⇒ Scrivito::Workspace

Updates the attributes of this workspace.

Parameters:

  • attributes (Hash)

Returns:

Raises:



181
182
183
184
185
# File 'lib/scrivito/workspace.rb', line 181

def update(attributes)
  raise ScrivitoError, 'published workspace is not modifiable' if published?
  CmsRestApi.put(backend_url, workspace: attributes)
  reload
end