Class: Hyrax::AdminSetCreateService

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

Overview

Responsible for creating a Hyrax::AdministrativeSet and its corresponding data.

  • An associated permission template

  • Available workflows

  • An active workflow

Constant Summary collapse

DEFAULT_ID =

rubocop:disable Metrics/ClassLength

'admin_set/default'
DEFAULT_TITLE =
['Default Admin Set'].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(admin_set:, creating_user:, workflow_importer: default_workflow_importer, default_admin_set: false) ⇒ AdminSetCreateService

Returns a new instance of AdminSetCreateService.

Parameters:

  • admin_set (Hyrax::AdministrativeSet | 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


169
170
171
172
173
174
# File 'app/services/hyrax/admin_set_create_service.rb', line 169

def initialize(admin_set:, creating_user:, workflow_importer: default_workflow_importer, default_admin_set: false)
  @admin_set = Hyrax::AdminSetCreateService.send(:admin_set_resource, admin_set)
  @creating_user = creating_user
  @workflow_importer = workflow_importer
  @default_admin_set = default_admin_set
end

Instance Attribute Details

#admin_setObject (readonly)

Returns the value of attribute admin_set.


176
177
178
# File 'app/services/hyrax/admin_set_create_service.rb', line 176

def admin_set
  @admin_set
end

#creating_userObject (readonly)

Returns the value of attribute creating_user.


176
177
178
# File 'app/services/hyrax/admin_set_create_service.rb', line 176

def creating_user
  @creating_user
end

#workflow_importerObject (readonly)

Returns the value of attribute workflow_importer.


176
177
178
# File 'app/services/hyrax/admin_set_create_service.rb', line 176

def workflow_importer
  @workflow_importer
end

Class Method Details

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

Deprecated.

Creates a non-default Hyrax::AdministrativeSet and corresponding data

Parameters:

Returns:

  • (TrueClass, FalseClass)

    true if it was successful

Raises:

  • (RuntimeError)

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

See Also:


63
64
65
66
67
68
69
70
71
# File 'app/services/hyrax/admin_set_create_service.rb', line 63

def call(admin_set:, creating_user:, **kwargs)
  Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0.  " \
                   "Warning: This method may hide runtime errors.  " \
                   "Instead, use 'Hyrax::AdminSetCreateService.call!'.  ")
  call!(admin_set: admin_set_resource(admin_set), creating_user: creating_user, **kwargs).present?
rescue RuntimeError => err
  raise err if default_admin_set?(id: admin_set.id)
  false
end

.call!(admin_set:, creating_user:, **kwargs) ⇒ Hyrax::AdministrativeSet

Creates a non-default Hyrax::AdministrativeSet and corresponding data

Parameters:

Returns:

Raises:

  • (RuntimeError)

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

  • (RuntimeError)

    if admin set cannot be persisted

See Also:


81
82
83
84
# File 'app/services/hyrax/admin_set_create_service.rb', line 81

def call!(admin_set:, creating_user:, **kwargs)
  raise "Use .find_or_create_default_admin_set to create a default admin set" if default_admin_set?(id: admin_set.id)
  new(admin_set: admin_set, creating_user: creating_user, **kwargs).create!
end

.create_default_admin_set(admin_set_id: DEFAULT_ID, title: DEFAULT_TITLE) ⇒ TrueClass

Deprecated.

Creates the default Hyrax::AdministrativeSet and corresponding data TODO: When this deprecated method is removed, update private method

.create_default_admin_set! to remove the parameters.

Parameters:

  • admin_set_id (String) (defaults to: DEFAULT_ID)

    The default admin set ID

  • title (Array<String>) (defaults to: DEFAULT_TITLE)

    The title of the default admin set

Returns:

  • (TrueClass)

See Also:


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

def create_default_admin_set(admin_set_id: DEFAULT_ID, title: DEFAULT_TITLE)
  Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0.  " \
                   "Instead, use 'Hyrax::AdminSetCreateService.find_or_create_default_admin_set'.")
  create_default_admin_set!(admin_set_id: admin_set_id, title: title).present?
rescue RuntimeError => _err
  false
end

.default_admin_set?(id:) ⇒ Boolean

Returns true if the id is for the default admin set; otherwise, false.

Parameters:

  • id (#to_s)

    id of the admin set to check

Returns:

  • (Boolean)

    true if the id is for the default admin set; otherwise, false


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

def default_admin_set?(id:)
  return false if id.blank?
  id.to_s == default_admin_set_id
end

.find_or_create_default_admin_setHyrax::AdministrativeSet

Finds the default AdministrativeSet if it exists; otherwise, creates it and corresponding data

Returns:

Raises:

  • (RuntimeError)

    if admin set cannot be persisted

See Also:


43
44
45
# File 'app/services/hyrax/admin_set_create_service.rb', line 43

def find_or_create_default_admin_set
  find_default_admin_set || create_default_admin_set!
end

Instance Method Details

#createTrueClass, FalseClass

Deprecated.

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

Returns:

  • (TrueClass, FalseClass)

    true if it was successful


181
182
183
184
185
186
187
188
# File 'app/services/hyrax/admin_set_create_service.rb', line 181

def create
  Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0.  " \
                     "Warning: This method may hide runtime errors.  " \
                     "Instead, use 'Hyrax::AdminSetCreateService.create!'.  ")
  create!.persisted?
rescue RuntimeError => _err
  false
end

#create!Hyrax::AdministrativeSet

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

Returns:

Raises:

  • (RuntimeError)

    if admin set cannot be persisted


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/services/hyrax/admin_set_create_service.rb', line 193

def create!
  admin_set.creator = [creating_user.user_key] if creating_user
  updated_admin_set = Hyrax.persister.save(resource: admin_set).tap do |result|
    if result
      ActiveRecord::Base.transaction do
        permission_template = PermissionTemplate.find_by(source_id: result.id.to_s) ||
                              permissions_create_service.create_default(collection: result,
                                                                        creating_user: creating_user)
        workflow = create_workflows_for(permission_template: permission_template)
        create_default_access_for(permission_template: permission_template, workflow: workflow) if default_admin_set?
      end
    end
  end
  Hyrax.publisher.publish('collection.metadata.updated', collection: updated_admin_set, user: creating_user)
  updated_admin_set
end