Skip to content

Commit

Permalink
Merge pull request #2855 from samvera/rework-add-participants
Browse files Browse the repository at this point in the history
Fail eagerly when adding invalid participants to collection types
  • Loading branch information
jcoyne authored Mar 28, 2018
2 parents 87db600 + 06ae61a commit 63f1f29
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
43 changes: 33 additions & 10 deletions app/services/hyrax/collection_types/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,46 @@ def self.add_default_participants(collection_type_id)
add_participants(collection_type_id, default_participants)
end

##
# @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
# @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)
return unless collection_type_id && participants.count > 0
raise(InvalidParticipantError, participants) unless
participants.all? { |p| p.key?(:agent_type) && p.key?(:agent_id) && p.key?(:access) }

participants.each do |p|
begin
agent_type = p.fetch(:agent_type)
agent_id = p.fetch(:agent_id)
access = p.fetch(:access)
Hyrax::CollectionTypeParticipant.create!(hyrax_collection_type_id: collection_type_id, agent_type: agent_type, agent_id: agent_id, access: access)
rescue => e
Rails.logger.error "Participant not created for collection type #{collection_type_id}: #{agent_type}, #{agent_id}, #{access} -- reason: #{e.class.name} - #{e.message}\n"
end
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
Expand Down
34 changes: 32 additions & 2 deletions spec/services/hyrax/collection_types/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,38 @@
let(:coltype) { create(:collection_type) }

it 'adds the participants to a collection type' do
expect(Hyrax::CollectionTypeParticipant).to receive(:create!)
described_class.add_participants(coltype.id, participants)
expect { described_class.add_participants(coltype.id, participants) }
.to change { Hyrax::CollectionType.find(coltype.id).collection_type_participants.to_a }
.to contain_exactly an_object_having_attributes(**participants.first)
end

context 'when participants are incomplete' do
let(:participants) do
[{ agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE,
agent_id: 'test_group',
access: Hyrax::CollectionTypeParticipant::MANAGE_ACCESS },
{ agent_type: Hyrax::CollectionTypeParticipant::GROUP_TYPE,
agent_id: 'test_group' }]
end

it 'logs and raises an error' do
expect(Rails.logger)
.to receive(:error)
.with a_string_starting_with('Participants not created')

expect { described_class.add_participants(coltype.id, participants) }
.to raise_error(described_class::InvalidParticipantError)
end

it 'does not add participants' do
expect do
begin
described_class.add_participants(coltype.id, participants)
rescue described_class::InvalidParticipantError
nil
end
end.not_to change { Hyrax::CollectionType.find(coltype.id).collection_type_participants.to_a }
end
end
end
end

0 comments on commit 63f1f29

Please sign in to comment.