Class: Hyrax::AdminSetCreateService

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

Overview

Responsible for creating an AdminSet and its corresponding data:

  • An associated permission template

  • Available workflows

  • An active workflow

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(admin_set:, creating_user:, workflow_importer: default_workflow_importer) ⇒ 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 (if any).

  • workflow_importer (#call) (defaults to: default_workflow_importer)

    imports the workflow



45
46
47
48
49
# File 'app/services/hyrax/admin_set_create_service.rb', line 45

def initialize(admin_set:, creating_user:, workflow_importer: default_workflow_importer)
  @admin_set = admin_set
  @creating_user = creating_user
  @workflow_importer = workflow_importer
end

Instance Attribute Details

#admin_setObject (readonly)

Returns the value of attribute admin_set.



51
52
53
# File 'app/services/hyrax/admin_set_create_service.rb', line 51

def admin_set
  @admin_set
end

#creating_userObject (readonly)

Returns the value of attribute creating_user.



51
52
53
# File 'app/services/hyrax/admin_set_create_service.rb', line 51

def creating_user
  @creating_user
end

#workflow_importerObject (readonly)

Returns the value of attribute workflow_importer.



51
52
53
# File 'app/services/hyrax/admin_set_create_service.rb', line 51

def workflow_importer
  @workflow_importer
end

Class Method Details

.call(admin_set:, creating_user:, **kwargs) ⇒ TrueClass, FalseClass

Creates a non-default AdminSet and corresponding data

Parameters:

  • admin_set (AdminSet)

    the admin set to operate on

  • creating_user (User)

    the user who created the admin set

Returns:

  • (TrueClass, FalseClass)

    true if it was successful

Raises:

  • (RuntimeError)

    if you attempt to create a default admin set via this mechanism

See Also:



37
38
39
40
# File 'app/services/hyrax/admin_set_create_service.rb', line 37

def self.call(admin_set:, creating_user:, **kwargs)
  raise "Use .create_default_admin_set to create a default admin set" if admin_set.default_set?
  new(admin_set: admin_set, creating_user: creating_user, **kwargs).create
end

.create_default_admin_set(admin_set_id:, title:) ⇒ TrueClass

Creates the default AdminSet and corresponding data

Parameters:

  • admin_set_id (String)

    The default admin set ID

  • title (Array<String>)

    The title of the default admin set

Returns:

  • (TrueClass)

See Also:



18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/hyrax/admin_set_create_service.rb', line 18

def self.create_default_admin_set(admin_set_id:, title:)
  admin_set = AdminSet.new(id: admin_set_id, title: Array.wrap(title))
  begin
    new(admin_set: admin_set, creating_user: nil).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

#createTrueClass, FalseClass

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

Returns:

  • (TrueClass, FalseClass)

    true if it was successful



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/hyrax/admin_set_create_service.rb', line 55

def create
  admin_set.edit_groups = [admin_group_name]
  admin_set.creator = [creating_user.user_key] if creating_user
  admin_set.save.tap do |result|
    if result
      ActiveRecord::Base.transaction do
        permission_template = create_permission_template
        workflow = create_workflows_for(permission_template: permission_template)
        create_default_access_for(permission_template: permission_template, workflow: workflow) if admin_set.default_set?
      end
    end
  end
end