Skip to content

Commit

Permalink
🧹 Clean up services
Browse files Browse the repository at this point in the history
This commit will reconcile the services that are overrides for Hyrax
with the Hyrax 5.0.0rc2 version.  There were a number of overrides that
were changed to the decorator pattern.
  • Loading branch information
kirkkwang committed Dec 21, 2023
1 parent 342605d commit e718e51
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 296 deletions.
8 changes: 5 additions & 3 deletions app/services/hyrax/admin_set_create_service_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Hyrax
# This decorator is used to override logic found in Hyrax v3.4.2
# This decorator is used to override logic found in Hyrax v5.0.0rc2
#
# Because Hyku has converted the Hyrax::Group model from a PORO to a db-backed active record object,
# we have to query for existing Hyrax groups instead of initializing empty ones.
Expand All @@ -27,13 +27,15 @@ def create!
updated_admin_set
end

private

def workflow_agents
[
# OVERRIDE: replace #new with #find_by(:name)
Hyrax::Group.find_by(name: admin_group_name)
Sipity::Agent(Hyrax::Group.find_by(name: admin_group_name))
].tap do |agent_list|
# The default admin set does not have a creating user
agent_list << creating_user if creating_user
agent_list << Sipity::Agent(creating_user) if creating_user
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# OVERRIDE Hyrax v3.4.2
# OVERRIDE Hyrax v5.0.0rc2
# - Give the :collection_manager role MANAGE_ACCESS to all non-AdminSet CollectionTypes by default
# - Give the :collection_editor role CREATE_ACCESS to all non-AdminSet CollectionTypes by default
# - Exclude CREATE_ACCESS from ::Ability.registered_group_name (all registered users) if we are restricting permissions
Expand All @@ -17,7 +17,7 @@ module CollectionTypes
#
# @see Hyrax:CollectionType
#
class CreateService # rubocop:disable Metrics/ClassLength
module CreateServiceDecorator # rubocop:disable Metrics/ModuleLength
DEFAULT_OPTIONS = {
description: '',
nestable: true,
Expand Down Expand Up @@ -63,8 +63,6 @@ class CreateService # rubocop:disable Metrics/ClassLength
end
}.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,
Expand Down Expand Up @@ -110,95 +108,6 @@ class CreateService # rubocop:disable Metrics/ClassLength
end
}.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

# @api public
#
# Create a new collection type.
#
# @param machine_id [String]
# @param title [String] short tag identifying the collection type
# @param options [Hash] options to override DEFAULT_OPTIONS
# @option options [String] :description a description to show the user when selecting the collection type
# @option options [Boolean] :nestable if true, collections of this type can be nested
# @option options [Boolean] :brandable if true, collections of this type can be branded
# @option options [Boolean] :discoverable if true, collections of this type can be marked Public
# and found in search results
# @option options [Boolean] :sharable if true, collections of this type can have participants added for
# :manage, :deposit, or :view access
# @option options [Boolean] :share_applies_to_new_works if true, share participant permissions are applied
# to new works created in the collection
# @option options [Boolean] :allow_multiple_membership if true, works can be members of multiple collections
# of this type
# @option options [Boolean] :require_membership 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.
# @option options [Boolean] :assigns_workflow if true, collections of this type can be used to assign
# a workflow to a work
# @option options [Boolean] :assigns_visibility if true, collections of this type can be used to assign
# initial visibility to a work
# @option options [String] :badge_color a color for the badge to show the user when selecting the collection type
# @return [Hyrax::CollectionType] the newly created collection type instance
def self.create_collection_type(machine_id:, title:, options: {})
opts = DEFAULT_OPTIONS.merge(options).except(:participants)
ct = Hyrax::CollectionType.create!(opts.merge(machine_id:, title:))
participants = options[:participants].presence || DEFAULT_OPTIONS[:participants]
add_participants(ct.id, participants)
ct
end

# @api public
#
# Create admin set collection type.
#
# @return [Hyrax::CollectionType] the newly created admin set collection type instance
def self.create_admin_set_type
create_collection_type(
machine_id: ADMIN_SET_MACHINE_ID,
title: ADMIN_SET_TITLE,
options: ADMIN_SET_OPTIONS
)
end

# @api public
#
# Create user collection type.
#
# @return [Hyrax::CollectionType] the newly created user collection type instance
def self.create_user_collection_type
create_collection_type(
machine_id: USER_COLLECTION_MACHINE_ID,
title: USER_COLLECTION_TITLE,
options: USER_COLLECTION_OPTIONS
)
end

# @api public
#
# Add the default participants to a collection_type.
Expand All @@ -209,7 +118,7 @@ def self.create_user_collection_type
# If calling from Abilities, pass the ability.
# If you try to get the ability from the user, you end up in an infinite loop.
# rubocop:disable Metrics/MethodLength
def self.add_default_participants(collection_type_id)
def 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,
Expand All @@ -234,55 +143,10 @@ def self.add_default_participants(collection_type_id)
end
add_participants(collection_type_id, default_participants)
end
# rubocop:enable Metrics/MethodLength

##
# @api public
#
# Add a participants to a collection_type.
#
# @param collection_type_id [Integer] the id of the collection type
# @param participants [Array<Hash>] each element holds agent_type, agent_id,
# and access for a participant to be added
#
# @raise [InvalidParticipantError] if a participant is missing an
# `agent_type`, `agent_id`, or `access`.
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

##
# An error class for the case that invalid/incomplete participants are
# added to a collection type.
class InvalidParticipantError < RuntimeError
attr_reader :participants

def initialize(participants)
@participants = participants
end

##
# @return [String]
def message
@participants.map do |participant|
"#{participant[:agent_type]}, #{participant[:agent_id]}, #{participant[:access]}"
end.join("--\n")
end
end
end
end # rubocop:enable Metrics/ModuleLength
end
end

Hyrax::CollectionTypes::CreateService.singleton_class.send(:prepend, Hyrax::CollectionTypes::CreateServiceDecorator)
Hyrax::CollectionTypes::CreateService::DEFAULT_OPTIONS = Hyrax::CollectionTypes::CreateServiceDecorator::DEFAULT_OPTIONS
Hyrax::CollectionTypes::CreateService::USER_COLLECTION_OPTIONS = Hyrax::CollectionTypes::CreateServiceDecorator::USER_COLLECTION_OPTIONS
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# OVERRIDE Hyrax v3.4.2 Grants certain roles access to either all AdminSets or
# all Collections (depending on the role) at create time.
# OVERRIDE Hyrax v5.0.0rc2 Grants certain roles access to either all AdminSets or
# all Collections (depending on the role) at create time.
module Hyrax
module Collections
module PermissionsCreateServiceDecorator
Expand Down
37 changes: 0 additions & 37 deletions app/services/hyrax/indexes_thumbnails.rb

This file was deleted.

18 changes: 18 additions & 0 deletions app/services/hyrax/indexes_thumbnails_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# OVERRIDE Hyrax v5.0.0rc2 to make collection thumbnails uploadable

module Hyrax
module IndexesThumbnailsDecorator
# Returns the value for the thumbnail path to put into the solr document
def thumbnail_path
if object.class == Collection && UploadedCollectionThumbnailPathService.uploaded_thumbnail?(object)
UploadedCollectionThumbnailPathService.call(object)
else
super
end
end
end
end

Hyrax::IndexesThumbnails.prepend(Hyrax::IndexesThumbnailsDecorator)
2 changes: 2 additions & 0 deletions app/services/hyrax/manifest_builder_service_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

# OVERRIDE Hyrax v5.0.0rc2 to change `&amp;` to `&` in the Universal Viewer

module Hyrax
module ManifestBuilderServiceDecorator
private
Expand Down
4 changes: 3 additions & 1 deletion app/services/hyrax/permission_manager_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Hyrax
# This decorator is used to override logic found in Hyrax v3.4.1
# This decorator is used to override logic found in Hyrax v5.0.0rc2
#
# Because Hyku has converted the Hyrax::Group model from a PORO to a db-backed active record object,
# we have to query for existing Hyrax groups instead of initializing empty ones.
Expand All @@ -16,6 +16,8 @@ module Hyrax
# Because of this, we also add queries for Role permissions in addition to Group permissions
# as part of these overrides.
module PermissionManagerDecorator
private

def update_groups_for(mode:, groups:)
groups = groups.map(&:to_s)

Expand Down
26 changes: 0 additions & 26 deletions app/services/hyrax/quick_classification_query.rb

This file was deleted.

29 changes: 29 additions & 0 deletions app/services/hyrax/quick_classification_query_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# OVERRIDE Hryax v5.0.0rc2

module Hyrax
module QuickClassificationQueryDecorator
# OVERRIDE: only use work types that are enabled in the current tenant
# @param [::User] user the current user
# @param [#call] concern_name_normalizer (String#constantize) a proc that translates names to classes
# @param [Array<String>] models the options to display, defaults to everything.
def initialize(user,
models: Site.instance.available_works,
concern_name_normalizer: ->(str) { str.constantize })
@user = user
@concern_name_normalizer = concern_name_normalizer
@models = models
end

# OVERRIDE: only use work types that are enabled in the current tenant
#
# @return true if the requested concerns is same as all avaliable concerns
def all?
# OVERRIDE: use Site.instance.available_works instead of Hyrax.config.registered_curation_concern_types
models == Site.instance.available_works
end
end
end

Hyrax::QuickClassificationQuery.prepend(Hyrax::QuickClassificationQueryDecorator)
Loading

0 comments on commit e718e51

Please sign in to comment.