-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate
PowerConverter
based to_sipity_action
`PowerConverter` is a community-maintained gem, but is not in the Samvera namespace. Depending on it under these circumstances is not in keeping with our policy. Rather than simply inline the `PowerConverter` class, this proposes pushing in the direction a more generic ruby pattern that inspired it. http://devblog.avdi.org/2012/05/07/a-ruby-conversion-idiom/
- Loading branch information
Tom Johnson
committed
Oct 18, 2019
1 parent
220a9be
commit 2258dc2
Showing
7 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Style/RaiseArgs | ||
module Sipity | ||
## | ||
# Cast an object to a WorkflowAction in a given workflow | ||
def WorkflowAction(input, workflow) # rubocop:disable Naming/MethodName | ||
workflow_id = PowerConverter.convert_to_sipity_workflow_id(workflow) | ||
|
||
result = case input | ||
when WorkflowAction | ||
input if input.workflow_id == workflow_id | ||
when String, Symbol | ||
WorkflowAction.find_by(workflow_id: workflow_id, name: input.to_s) | ||
end | ||
|
||
result ||= input.try(:to_sipity_action) | ||
return result unless result.nil? | ||
return yield if block_given? | ||
|
||
raise ConversionError.new(input) | ||
end | ||
module_function :WorkflowAction | ||
|
||
# rubocop:enable Style/RaiseArgs | ||
class ConversionError < PowerConverter::ConversionError | ||
def initialize(value, **options) | ||
options[:scope] ||= nil | ||
options[:to] ||= nil | ||
super(value, options) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
RSpec.describe Sipity do | ||
describe '.WorkflowAction' do | ||
let(:workflow_id) { 1 } | ||
let(:action) { Sipity::WorkflowAction.new(id: 4, name: 'show', workflow_id: workflow_id) } | ||
|
||
context "with workflow_id and action's workflow_id matching" do | ||
it 'will return the object if it is a Sipity::WorkflowAction' do | ||
expect(described_class.WorkflowAction(action, workflow_id)).to eq(action) | ||
end | ||
|
||
it 'will return the object if it responds to #to_sipity_action' do | ||
object = double(to_sipity_action: action) | ||
expect(described_class.WorkflowAction(object, workflow_id)).to eq(action) | ||
end | ||
|
||
it 'will raise an error if it cannot convert the object' do | ||
object = double | ||
expect { described_class.WorkflowAction(object, workflow_id) } | ||
.to raise_error(Sipity::ConversionError) | ||
end | ||
|
||
it 'will use a found action based on the given string and workflow_id' do | ||
expect(Sipity::WorkflowAction).to receive(:find_by).and_return(action) | ||
expect(described_class.WorkflowAction(action.name, workflow_id)).to eq(action) | ||
end | ||
|
||
context "when the WorkflowAction can not be found" do | ||
it 'will raise an error' do | ||
expect(Sipity::WorkflowAction).to receive(:find_by).and_return(nil) | ||
expect { described_class.WorkflowAction(action.name, workflow_id) } | ||
.to raise_error(Sipity::ConversionError) | ||
end | ||
end | ||
end | ||
|
||
context "with mismatching workflow_id and action's workflow_id" do | ||
it "will fail an error if the scope's workflow_id is different than the actions" do | ||
expect { described_class.WorkflowAction(action, workflow_id + 1) } | ||
.to raise_error(Sipity::ConversionError) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters