Skip to content

Commit

Permalink
make test stub AdminSetCreateService as the service
Browse files Browse the repository at this point in the history
makes a few additional minor adjustments based on PR feedback
  • Loading branch information
elrayle committed Sep 1, 2021
1 parent 68a6411 commit 099ee13
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
23 changes: 19 additions & 4 deletions app/controllers/hyrax/admin/admin_sets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ def ensure_viewer!
end

def create_admin_set
# TODO: Should be able to remove the valkryie_resource check when create/edit form is updated
admin_set = @admin_set.respond_to?(:valkyrie_resource) ? @admin_set.valkyrie_resource : @admin_set
admin_set_create_service.call(admin_set: admin_set, creating_user: current_user)
updated_admin_set = admin_set_create_service.call!(admin_set: admin_set_resource, creating_user: current_user)
update_admin_set(updated_admin_set)
true
# rescue RuntimeError => e
rescue RuntimeError => e
false
end

def setup_form
Expand Down Expand Up @@ -158,7 +161,19 @@ def after_delete_success
end

def admin_set_id
@admin_set&.id.to_s
@admin_set&.id&.to_s
end

def admin_set_resource
@admin_set.respond_to?(:valkyrie_resource) ? @admin_set.valkyrie_resource : @admin_set
end

def update_admin_set(updated_admin_set)
@admin_set = if @admin_set.respond_to?(:valkyrie_resource)
Wings::ActiveFedoraConverter.convert(resource: updated_admin_set)
else
updated_admin_set
end
end
end
end
4 changes: 4 additions & 0 deletions app/models/admin_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ class AdminSet < ActiveFedora::Base
after_destroy :destroy_permission_template

def self.default_set?(id)
Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0. " \
"Instead, use the same method in 'Hyrax::AdminSetCreateService.default_admin_set?(id:)'.")
Hyrax::AdminSetCreateService.default_admin_set?(id: id)
end

def default_set?
Deprecation.warn("'##{__method__}' will be removed in Hyrax 4.0. " \
"Instead, use the same method in 'Hyrax::AdminSetCreateService.default_admin_set?(id:)'.")
self.class.default_set?(id)
end

Expand Down
25 changes: 16 additions & 9 deletions spec/controllers/hyrax/admin/admin_sets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,33 @@
end

context "when it's successful" do
let(:service) do
lambda do |admin_set:, **_kargs|
admin_set.id = 123
# https://github.com/samvera/active_fedora/issues/1251
allow(admin_set).to receive(:persisted?).and_return(true)
true
end
let(:service) { double Hyrax::AdminSetCreateService }
let(:updated_admin_set) do
FactoryBot.valkyrie_create(:hyrax_admin_set, id: '123',
title: ['Test title'])
end

before do
allow(service).to receive(:call!).with(admin_set: anything, creating_user: user)
.and_return(updated_admin_set)
allow(updated_admin_set).to receive(:persisted?).and_return(true)
end

it 'creates admin set' do
post :create, params: { admin_set: { title: 'Test title',
description: 'test description',
workflow_name: 'default' } }
admin_set = assigns(:admin_set)
expect(response).to redirect_to(edit_admin_admin_set_path(admin_set.id.to_s))
expect(response).to redirect_to(edit_admin_admin_set_path(admin_set))
end
end

context "when it fails" do
let(:service) { ->(**_kargs) { false } }
let(:service) { double Hyrax::AdminSetCreateService }
before do
allow(service).to receive(:call!).with(admin_set: anything, creating_user: user)
.and_raise(RuntimeError)
end

it 'shows the new form' do
post :create, params: { admin_set: { title: 'Test title',
Expand Down

0 comments on commit 099ee13

Please sign in to comment.