Class: Sufia::AdminSetCreateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/sufia/admin_set_create_service.rb

Overview

Creates AdminSets

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(admin_set, creating_user, workflow_name) ⇒ AdminSetCreateService

Returns a new instance of AdminSetCreateService.

Parameters:

  • admin_set (AdminSet)

    the admin set to operate on

  • creating_user (User)

    the user who created the admin set.



19
20
21
22
23
# File 'app/services/sufia/admin_set_create_service.rb', line 19

def initialize(admin_set, creating_user, workflow_name)
  @admin_set = admin_set
  @creating_user = creating_user
  @workflow_name = workflow_name || AdminSet::DEFAULT_WORKFLOW_NAME
end

Instance Attribute Details

#admin_setObject (readonly)

Returns the value of attribute admin_set.



25
26
27
# File 'app/services/sufia/admin_set_create_service.rb', line 25

def admin_set
  @admin_set
end

#creating_userObject (readonly)

Returns the value of attribute creating_user.



25
26
27
# File 'app/services/sufia/admin_set_create_service.rb', line 25

def creating_user
  @creating_user
end

#workflow_nameObject (readonly)

Returns the value of attribute workflow_name.



25
26
27
# File 'app/services/sufia/admin_set_create_service.rb', line 25

def workflow_name
  @workflow_name
end

Class Method Details

.create_default!Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/services/sufia/admin_set_create_service.rb', line 4

def self.create_default!
  return if AdminSet.exists?(AdminSet::DEFAULT_ID)
  admin_set = AdminSet.new(id: AdminSet::DEFAULT_ID, title: ['Default Admin Set'])
  begin
    new(admin_set, nil, AdminSet::DEFAULT_WORKFLOW_NAME).create
  rescue ActiveFedora::IllegalOperation
    # It is possible that another thread created the AdminSet just before this method
    # was called, so ActiveFedora will raise IllegalOperation. In this case we can safely
    # ignore the error.
    Rails.logger.error("AdminSet ID=#{AdminSet::DEFAULT_ID} may or may not have been created due to threading issues.")
  end
end

Instance Method Details

#access_grants_attributesObject



38
39
40
41
# File 'app/services/sufia/admin_set_create_service.rb', line 38

def access_grants_attributes
  return [] unless creating_user
  [{ agent_type: 'user', agent_id: creating_user.user_key, access: 'manage' }]
end

#createTrueClass, FalseClass

Creates an admin set, setting the creator and the default access controls.

Returns:

  • (TrueClass, FalseClass)

    true if it was successful



29
30
31
32
33
34
35
36
# File 'app/services/sufia/admin_set_create_service.rb', line 29

def create
  admin_set.read_groups = ['public']
  admin_set.edit_groups = ['admin']
  admin_set.creator = [creating_user.user_key] if creating_user
  admin_set.save.tap do |result|
    create_permission_template if result
  end
end

#create_permission_templateObject



43
44
45
46
47
# File 'app/services/sufia/admin_set_create_service.rb', line 43

def create_permission_template
  PermissionTemplate.create!(admin_set_id: admin_set.id,
                             access_grants_attributes: access_grants_attributes,
                             workflow_name: workflow_name)
end