Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] update AdminSetCreateService to create valkyrie resources #5083

Closed
wants to merge 8 commits into from
36 changes: 24 additions & 12 deletions app/controllers/hyrax/admin/admin_sets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class Admin::AdminSetsController < ApplicationController
include Hyrax::CollectionsControllerBehavior

before_action :authenticate_user!
before_action :ensure_manager!, except: [:show]
load_and_authorize_resource
before_action :ensure_viewer!, only: [:show]

Expand Down Expand Up @@ -75,7 +74,7 @@ def update

def create
if create_admin_set
redirect_to hyrax.edit_admin_admin_set_path(@admin_set), notice: I18n.t('new_admin_set', scope: 'hyrax.admin.admin_sets.form.permission_update_notices', name: @admin_set.title.first)
redirect_to hyrax.edit_admin_admin_set_path(admin_set_id), notice: I18n.t('new_admin_set', scope: 'hyrax.admin.admin_sets.form.permission_update_notices', name: @admin_set.title.first)
else
setup_form
render :new
Expand All @@ -86,7 +85,7 @@ def destroy
if @admin_set.destroy
after_delete_success
else
redirect_to hyrax.admin_admin_set_path(@admin_set), alert: @admin_set.errors.full_messages.to_sentence
redirect_to hyrax.admin_admin_set_path(admin_set_id), alert: @admin_set.errors.full_messages.to_sentence
end
end

Expand All @@ -103,14 +102,7 @@ def self.local_prefixes
private

def update_referer
hyrax.edit_admin_admin_set_path(@admin_set) + (params[:referer_anchor] || '')
end

def ensure_manager!
# TODO: Review for possible removal. Doesn't appear to apply anymore.
# Even though the user can view this admin set, they may not be able to view
# it on the admin page.
authorize! :manage_any, AdminSet
hyrax.edit_admin_admin_set_path(admin_set_id) + (params[:referer_anchor] || '')
end

def ensure_viewer!
Expand All @@ -120,7 +112,11 @@ def ensure_viewer!
end

def create_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
false
end

def setup_form
Expand Down Expand Up @@ -162,5 +158,21 @@ def after_delete_success
redirect_to hyrax.my_collections_path, notice: t(:'hyrax.admin.admin_sets.delete.notification')
end
end

def admin_set_id
@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
12 changes: 8 additions & 4 deletions app/models/admin_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class AdminSet < ActiveFedora::Base
include Hyrax::HumanReadableType
include Hyrax::HasRepresentative

DEFAULT_ID = 'admin_set/default'
DEFAULT_TITLE = ['Default Admin Set'].freeze
DEFAULT_ID = Hyrax::AdminSetCreateService::DEFAULT_ID
elrayle marked this conversation as resolved.
Show resolved Hide resolved
DEFAULT_TITLE = Hyrax::AdminSetCreateService::DEFAULT_TITLE
DEFAULT_WORKFLOW_NAME = Hyrax.config.default_active_workflow_name

validates_with Hyrax::HasOneTitleValidator
Expand All @@ -42,16 +42,20 @@ class AdminSet < ActiveFedora::Base
after_destroy :destroy_permission_template

def self.default_set?(id)
id == DEFAULT_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)
elrayle marked this conversation as resolved.
Show resolved Hide resolved
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

# Creates the default AdminSet and an associated PermissionTemplate with workflow
def self.find_or_create_default_admin_set_id
Hyrax::AdminSetCreateService.create_default_admin_set(admin_set_id: DEFAULT_ID, title: DEFAULT_TITLE) unless exists?(DEFAULT_ID)
Hyrax::AdminSetCreateService.find_or_create_default_admin_set
DEFAULT_ID
end

Expand Down
20 changes: 10 additions & 10 deletions app/models/concerns/hyrax/ability/admin_set_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ module Ability
module AdminSetAbility
def admin_set_abilities # rubocop:disable Metrics/MethodLength
if admin?
can :manage, AdminSet
can :manage_any, AdminSet
can :create_any, AdminSet
can :view_admin_show_any, AdminSet
can :manage, [AdminSet, Hyrax::AdministrativeSet]
can :manage_any, [AdminSet, Hyrax::AdministrativeSet]
can :create_any, [AdminSet, Hyrax::AdministrativeSet]
can :view_admin_show_any, [AdminSet, Hyrax::AdministrativeSet]
else
can :manage_any, AdminSet if Hyrax::Collections::PermissionsService.can_manage_any_admin_set?(ability: self)
can [:create_any, :create], AdminSet if Hyrax::CollectionTypes::PermissionsService.can_create_admin_set_collection_type?(ability: self)
can :view_admin_show_any, AdminSet if Hyrax::Collections::PermissionsService.can_view_admin_show_for_any_admin_set?(ability: self)
can :manage_any, [AdminSet, Hyrax::AdministrativeSet] if Hyrax::Collections::PermissionsService.can_manage_any_admin_set?(ability: self)
can [:create_any, :create], [AdminSet, Hyrax::AdministrativeSet] if Hyrax::CollectionTypes::PermissionsService.can_create_admin_set_collection_type?(ability: self)
can :view_admin_show_any, [AdminSet, Hyrax::AdministrativeSet] if Hyrax::Collections::PermissionsService.can_view_admin_show_for_any_admin_set?(ability: self)

can [:edit, :update, :destroy], AdminSet do |admin_set| # for test by solr_doc, see solr_document_ability.rb
can [:edit, :update, :destroy], [AdminSet, Hyrax::AdministrativeSet] do |admin_set| # for test by solr_doc, see solr_document_ability.rb
test_edit(admin_set.id)
end

can :deposit, AdminSet do |admin_set| # for test by solr_doc, see collection_ability.rb
can :deposit, [AdminSet, Hyrax::AdministrativeSet] do |admin_set| # for test by solr_doc, see collection_ability.rb
Hyrax::Collections::PermissionsService.can_deposit_in_collection?(ability: self, collection_id: admin_set.id)
end

can :view_admin_show, AdminSet do |admin_set| # admin show page # for test by solr_doc, see collection_ability.rb
can :view_admin_show, [AdminSet, Hyrax::AdministrativeSet] do |admin_set| # admin show page # for test by solr_doc, see collection_ability.rb
Hyrax::Collections::PermissionsService.can_view_admin_show_for_collection?(ability: self, collection_id: admin_set.id)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/hyrax/permission_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Hyrax
# @todo write up what "default embargo behavior", when it is applied, and how
# it interacts with embargoes specified by user input.
#
# @example cerating a permission template and manager for an admin set
# @example creating a permission template and manager for an admin set
# admin_set = Hyrax::AdministrativeSet.new(title: 'My Admin Set')
# admin_set = Hyrax.persister.save(resource: admin_set)
#
Expand Down
10 changes: 8 additions & 2 deletions app/presenters/hyrax/admin_set_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def total_viewable_items

# AdminSet cannot be deleted if default set or non-empty
def disable_delete?
AdminSet.default_set?(id) || any_items?
default_set? || any_items?
end

# Message to display if deletion is disabled
def disabled_message
return I18n.t('hyrax.admin.admin_sets.delete.error_default_set') if AdminSet.default_set?(id)
return I18n.t('hyrax.admin.admin_sets.delete.error_default_set') if default_set?

I18n.t('hyrax.admin.admin_sets.delete.error_not_empty') if any_items?
end
Expand Down Expand Up @@ -63,5 +63,11 @@ def allow_batch?
return false unless current_ability.can?(:edit, solr_document)
!disable_delete?
end

private

def default_set?
Hyrax::AdminSetCreateService.default_admin_set?(id: id)
end
end
end
Loading