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



156
157
158
159
160
161
# File 'app/services/hyrax/admin_set_create_service.rb', line 156

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.



163
164
165
# File 'app/services/hyrax/admin_set_create_service.rb', line 163

def admin_set
  @admin_set
end

#creating_userObject (readonly)

Returns the value of attribute creating_user.



163
164
165
# File 'app/services/hyrax/admin_set_create_service.rb', line 163

def creating_user
  @creating_user
end

#workflow_importerObject (readonly)

Returns the value of attribute workflow_importer.



163
164
165
# File 'app/services/hyrax/admin_set_create_service.rb', line 163

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:



46
47
48
49
50
51
52
53
54
# File 'app/services/hyrax/admin_set_create_service.rb', line 46

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:



64
65
66
67
# File 'app/services/hyrax/admin_set_create_service.rb', line 64

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

.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



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

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:



26
27
28
# File 'app/services/hyrax/admin_set_create_service.rb', line 26

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



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

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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/services/hyrax/admin_set_create_service.rb', line 180

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