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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allArray<Scrivito::Workspace>

Returns all the workspaces

Returns:



41
42
43
44
45
46
47
# File 'lib/scrivito/workspace.rb', line 41

def self.all
  result_json = CmsRestApi.get('/workspaces')

  result_json['results'].map do |workspace_json|
    Workspace.find(workspace_json['id'])
  end
end

.create(attributes) ⇒ Scrivito::Workspace

Create a new workspace

Parameters:

  • attributes (Hash)

Returns:



88
89
90
91
92
# File 'lib/scrivito/workspace.rb', line 88

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:



30
31
32
33
34
35
36
# File 'lib/scrivito/workspace.rb', line 30

def self.current
  if @current.respond_to? :call
    @current = @current.call
  else
    @current ||= default
  end
end

.current=(workspace) ⇒ Object

Set the currently used workspace

Parameters:



19
20
21
# File 'lib/scrivito/workspace.rb', line 19

def self.current=(workspace)
  @current = workspace
end

.find(id) ⇒ Scrivito::Workspace

Find a workspace by its id

Parameters:

  • id (String)

Returns:

Raises:



64
65
66
67
68
69
70
# File 'lib/scrivito/workspace.rb', line 64

def self.find(id)
  if workspace_data = CmsBackend.instance.find_workspace_data_by_id(id)
    Workspace.new workspace_data
  else
    raise ResourceNotFound, "Could not find #{self} with id #{id}"
  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:



78
79
80
# File 'lib/scrivito/workspace.rb', line 78

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

.reloadObject

reloads the current workspace to reflect any changes to it that may have happened concurrently since it was loaded



97
98
99
100
# File 'lib/scrivito/workspace.rb', line 97

def self.reload
  id = current.id
  self.current_using_proc = proc { find(id) }
end

Instance Method Details

#destroyObject

Destroy this workspace



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

def destroy
  reset_workspace_if_current
  CmsRestApi.delete(backend_url)
end

#idString

Returns the id of the workspace

Returns:

  • (String)


166
167
168
# File 'lib/scrivito/workspace.rb', line 166

def id
  @workspace_data.id
end

#membershipsMembershipCollection

Returns the members of this workspace and their roles



185
186
187
# File 'lib/scrivito/workspace.rb', line 185

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

#obj_classesObjClassCollection

Deprecated.

Returns all obj classes of this working copy.

Examples:

Find the obj class named “Homepage” in the “rtc” Scrivito::Workspace.

Workspace.find('rtc').obj_classes['Homepage']

Returns:



253
254
255
# File 'lib/scrivito/workspace.rb', line 253

def obj_classes
  @obj_classes ||= ObjClassCollection.new(self)
end

#objsObjCollection

ObjCollection for this working copy

Returns:



240
241
242
# File 'lib/scrivito/workspace.rb', line 240

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

#publishObject

Publish the changes of this workspace



150
151
152
153
154
# File 'lib/scrivito/workspace.rb', line 150

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

  reset_workspace_if_current
end

#rebaseObject

Rebases the current workspace on the published content



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

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

#reloadObject

Reloads this workspace to reflect any changes to it that may have happened concurrently since it was loaded



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

def reload
  @workspace_data = CmsBackend.instance.find_workspace_data_by_id(self.id)

  # Clear all cached instance variables.
  @base_revision = nil
  @memberships   = nil
  @revision      = nil
end

#titleString

Returns the title of the workspace

Returns:

  • (String)


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

def title
  @workspace_data.title
end

#update(attributes) ⇒ Scrivito::Workspace

Updates this workspace’s attributes

Parameters:

  • attributes (Hash)

Returns:



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

def update(attributes)
  CmsRestApi.put(backend_url, workspace: attributes)
  reload
end