Class: Hyrax::CollectionTypes::CreateService

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

Overview

Responsible for creating a CollectionType. If no params are given,the default user collection is assumed as defined by:

  • Hyrax::CollectionType::USER_COLLECTION_MACHINE_ID

  • Hyrax::CollectionType::USER_COLLECTION_DEFAULT_TITLE

  • DEFAULT_OPTIONS

See Also:

  • Hyrax:CollectionType

Defined Under Namespace

Classes: InvalidParticipantError

Constant Summary collapse

DEFAULT_OPTIONS =
{
  description: '',
  nestable: true,
  brandable: true,
  discoverable: true,
  sharable: true,
  share_applies_to_new_works: true,
  allow_multiple_membership: true,
  require_membership: false,
  assigns_workflow: false,
  assigns_visibility: false,
  badge_color: "#663333",
  participants: [{ agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE, agent_id: ::Ability.admin_group_name, access: Hyrax::CollectionTypeParticipant::MANAGE_ACCESS },
                 { agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE, agent_id: ::Ability.registered_group_name, access: Hyrax::CollectionTypeParticipant::CREATE_ACCESS }]
}.freeze
USER_COLLECTION_MACHINE_ID =
Hyrax::CollectionType::USER_COLLECTION_MACHINE_ID
USER_COLLECTION_TITLE =
Hyrax::CollectionType::USER_COLLECTION_DEFAULT_TITLE
USER_COLLECTION_OPTIONS =
{
  description: I18n.t('hyrax.collection_types.create_service.default_description'),
  nestable: true,
  brandable: true,
  discoverable: true,
  sharable: true,
  share_applies_to_new_works: false,
  allow_multiple_membership: true,
  require_membership: false,
  assigns_workflow: false,
  assigns_visibility: false,
  badge_color: "#705070",
  participants: [{ agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE, agent_id: ::Ability.admin_group_name, access: Hyrax::CollectionTypeParticipant::MANAGE_ACCESS },
                 { agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE, agent_id: ::Ability.registered_group_name, access: Hyrax::CollectionTypeParticipant::CREATE_ACCESS }]
}.freeze
ADMIN_SET_MACHINE_ID =
Hyrax::CollectionType::ADMIN_SET_MACHINE_ID
ADMIN_SET_TITLE =
Hyrax::CollectionType::ADMIN_SET_DEFAULT_TITLE
ADMIN_SET_OPTIONS =
{
  description: I18n.t('hyrax.collection_types.create_service.admin_set_description'),
  nestable: false,
  brandable: false,
  discoverable: false,
  sharable: true,
  share_applies_to_new_works: true,
  allow_multiple_membership: false,
  require_membership: true,
  assigns_workflow: true,
  assigns_visibility: true,
  badge_color: "#405060",
  participants: [{ agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE, agent_id: ::Ability.admin_group_name, access: Hyrax::CollectionTypeParticipant::MANAGE_ACCESS },
                 { agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE, agent_id: ::Ability.admin_group_name, access: Hyrax::CollectionTypeParticipant::CREATE_ACCESS }]
}.freeze

Class Method Summary collapse

Class Method Details

.add_default_participants(collection_type_id) ⇒ Object

Note:

Several checks get the user's groups from the user's ability. The same values can be retrieved directly from a passed in ability. If calling from Abilities, pass the ability. If you try to get the ability from the user, you end up in an infinit loop.

Add the default participants to a collection_type.

Parameters:

  • collection_type_id (Integer)

    the id of the collection type


119
120
121
122
123
124
125
126
127
128
# File 'app/services/hyrax/collection_types/create_service.rb', line 119

def self.add_default_participants(collection_type_id)
  return unless collection_type_id
  default_participants = [{  agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE,
                             agent_id: ::Ability.admin_group_name,
                             access: Hyrax::CollectionTypeParticipant::MANAGE_ACCESS },
                          { agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE,
                            agent_id: ::Ability.registered_group_name,
                            access: Hyrax::CollectionTypeParticipant::CREATE_ACCESS }]
  add_participants(collection_type_id, default_participants)
end

.add_participants(collection_type_id, participants) ⇒ Object

Add a participants to a collection_type.

Parameters:

  • collection_type_id (Integer)

    the id of the collection type

  • participants (Array<Hash>)

    each element holds agent_type, agent_id, and access for a participant to be added

Raises:


141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/services/hyrax/collection_types/create_service.rb', line 141

def self.add_participants(collection_type_id, participants)
  raise(InvalidParticipantError, participants) unless
    participants.all? { |p| p.key?(:agent_type) && p.key?(:agent_id) && p.key?(:access) }

  participants.each do |p|
    Hyrax::CollectionTypeParticipant.create!(hyrax_collection_type_id: collection_type_id, agent_type: p.fetch(:agent_type), agent_id: p.fetch(:agent_id), access: p.fetch(:access))
  end
rescue InvalidParticipantError => error
  Rails.logger.error "Participants not created for collection type " \
                     " #{collection_type_id}: #{error.message}"
  raise error
end

.create_admin_set_typeHyrax::CollectionType

Create admin set collection type.

Returns:


99
100
101
# File 'app/services/hyrax/collection_types/create_service.rb', line 99

def self.create_admin_set_type
  create_collection_type(machine_id: ADMIN_SET_MACHINE_ID, title: ADMIN_SET_TITLE, options: ADMIN_SET_OPTIONS)
end

.create_collection_type(machine_id:, title:, options: {}) ⇒ Hyrax::CollectionType

Create a new collection type.

Parameters:

  • machine_id (String)
  • title (String)

    short tag identifying the collection type

  • options (Hash) (defaults to: {})

    options to override DEFAULT_OPTIONS

Options Hash (options:):

  • :description (String)

    a description to show the user when selecting the collection type

  • :nestable (Boolean)

    if true, collections of this type can be nested

  • :brandable (Boolean)

    if true, collections of this type can be branded

  • :discoverable (Boolean)

    if true, collections of this type can be marked Public and found in search results

  • :sharable (Boolean)

    if true, collections of this type can have participants added for :manage, :deposit, or :view access

  • :share_applies_to_new_works (Boolean)

    if true, share participant permissions are applied to new works created in the collection

  • :allow_multiple_membership (Boolean)

    if true, works can be members of multiple collections of this type

  • :require_membership (Boolean)

    if true, all works must belong to at least one collection of this type. When combined with allow_multiple_membership=false, works can belong to one and only one collection of this type.

  • :assigns_workflow (Boolean)

    if true, collections of this type can be used to assign a workflow to a work

  • :assigns_visibility (Boolean)

    if true, collections of this type can be used to assign initial visibility to a work

  • :badge_color (String)

    a color for the badge to show the user when selecting the collection type

Returns:


86
87
88
89
90
91
92
# File 'app/services/hyrax/collection_types/create_service.rb', line 86

def self.create_collection_type(machine_id:, title:, options: {})
  opts = DEFAULT_OPTIONS.merge(options).except(:participants)
  ct = Hyrax::CollectionType.create!(opts.merge(machine_id: machine_id, title: title))
  participants = options[:participants].presence || DEFAULT_OPTIONS[:participants]
  add_participants(ct.id, participants)
  ct
end

.create_user_collection_typeHyrax::CollectionType

Create user collection type.

Returns:


108
109
110
# File 'app/services/hyrax/collection_types/create_service.rb', line 108

def self.create_user_collection_type
  create_collection_type(machine_id: USER_COLLECTION_MACHINE_ID, title: USER_COLLECTION_TITLE, options: USER_COLLECTION_OPTIONS)
end