Skip to content

Commit

Permalink
Deprecate PowerConverter-based to_sipity_action_name
Browse files Browse the repository at this point in the history
Allow `WorkflowAction` to handle casting to an action name, using `.name_for(object_to_cast)`.
  • Loading branch information
Tom Johnson committed Oct 18, 2019
1 parent 2258dc2 commit e5d1dfb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/conversions/power_converters/sipity_action_name.rb
@@ -1,4 +1,5 @@
PowerConverter.define_conversion_for(:sipity_action_name) do |input|
Deprecation.warn('PowerConverter is deprecated. Use `Sipity::WorkflowAction.name_for(input)` instead')
case input
when String, Symbol
input.to_s.sub(/[\?\!]\Z/, '')
Expand Down
17 changes: 17 additions & 0 deletions app/models/sipity/workflow_action.rb
Expand Up @@ -23,5 +23,22 @@ class WorkflowAction < ActiveRecord::Base
dependent: :destroy,
as: :scope_for_notification,
class_name: 'Sipity::NotifiableContext'

##
# @param [Object] input
# @return [String]
def self.name_for(input)
result = case input
when String, Symbol
input.to_s.sub(/[\?\!]\Z/, '')
when Sipity::WorkflowAction
input.name
end
result ||= input.try(:to_sipity_action_name)
return result unless result.nil?
return yield if block_given?

raise ConversionError.new(input) # rubocop:disable Style/RaiseArgs
end
end
end
2 changes: 1 addition & 1 deletion app/services/hyrax/workflow/permission_query.rb
Expand Up @@ -147,7 +147,7 @@ def scope_roles_associated_with_the_given_entity(entity:)
# @param action an object that can be converted into a Sipity::WorkflowAction#name
# @return [Boolean]
def authorized_for_processing?(user:, entity:, action:)
action_name = PowerConverter.convert_to_sipity_action_name(action)
action_name = Sipity::WorkflowAction.name_for(action)
scope_permitted_workflow_actions_available_for_current_state(user: user, entity: entity)
.find_by(Sipity::WorkflowAction.arel_table[:name].eq(action_name)).present?
end
Expand Down
33 changes: 33 additions & 0 deletions spec/models/sipity/workflow_action_spec.rb
@@ -1,3 +1,7 @@
# frozen_string_literal: true

require "spec_helper"

module Sipity
RSpec.describe WorkflowAction, type: :model do
context 'database configuration' do
Expand All @@ -7,5 +11,34 @@ module Sipity
its(:column_names) { is_expected.to include('resulting_workflow_state_id') }
its(:column_names) { is_expected.to include('name') }
end

describe '.name' do
[
[:show, 'show'],
[:show?, 'show'],
[:new?, 'new'],
[:new, 'new'],
[:edit?, 'edit'],
[:edit, 'edit'],
[:submit, 'submit'],
[:submit?, 'submit'],
[:attach, 'attach'],
[Sipity::WorkflowAction.new(name: 'hello'), 'hello']
].each_with_index do |(original, expected), index|
it "will convert #{original.inspect} to #{expected.inspect} (scenario ##{index})" do
expect(described_class.name_for(original)).to eq(expected)
end
end
end

it 'will raise an exception if it is not processible' do
object = double('Bad Wolf')
expect { described_class.name_for(object) }.to raise_error(Sipity::ConversionError)
end

it 'will leverage a short-circuit #to_processing_action_name' do
object = double(to_sipity_action_name: 'bob')
expect(described_class.name_for(object)).to eq('bob')
end
end
end

0 comments on commit e5d1dfb

Please sign in to comment.