Skip to content

Commit

Permalink
Deprecate PowerConverter based to_sipity_agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Johnson committed Oct 18, 2019
1 parent 8d6d346 commit f4c1ef3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions app/conversions/power_converters/sipity_agent.rb
@@ -1,4 +1,6 @@
PowerConverter.define_conversion_for(:sipity_agent) do |input|
Deprecation.warn('PowerConverter is deprecated. Use `Sipity::Agent(input)` instead')

case input
when Sipity::Agent
input
Expand Down
2 changes: 1 addition & 1 deletion app/forms/hyrax/forms/permission_template_form.rb
Expand Up @@ -149,7 +149,7 @@ def agents_from_attributes
else
Hyrax::Group.new(grant[:agent_id])
end
PowerConverter.convert_to_sipity_agent(agent)
Sipity::Agent(agent)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/hyrax/workflow_action_info.rb
Expand Up @@ -5,7 +5,7 @@ def initialize(work, user)
@work = work
@user = user
@entity = Sipity::Entity(work)
@agent = PowerConverter.convert(user, to: :sipity_agent)
@agent = Sipity::Agent(user)
end

attr_reader :entity, :agent, :user, :work
Expand Down
12 changes: 12 additions & 0 deletions app/models/sipity.rb
@@ -1,6 +1,18 @@
# frozen_string_literal: true

module Sipity
##
# Cast an object to an Entity
def Agent(input, &block) # rubocop:disable Naming/MethodName
result = case input
when Sipity::Agent
input
end

handle_conversion(input, result, :to_sipity_agent, &block)
end
module_function :Agent

##
# Cast an object to an Entity
def Entity(input, &block) # rubocop:disable Naming/MethodName
Expand Down
2 changes: 1 addition & 1 deletion app/services/hyrax/workflow/permission_generator.rb
Expand Up @@ -55,7 +55,7 @@ def initialize(roles:, workflow:, agents: [], **keywords)
attr_reader :entity, :agents, :action_names, :roles

def agents=(input)
@agents = Array.wrap(input).map { |agent| PowerConverter.convert_to_sipity_agent(agent) }
@agents = Array.wrap(input).map { |agent| Sipity::Agent(agent) }
end

def action_names=(input)
Expand Down
4 changes: 2 additions & 2 deletions app/services/hyrax/workflow/permission_query.rb
Expand Up @@ -163,9 +163,9 @@ def authorized_for_processing?(user:, entity:, action:)
def scope_processing_agents_for(user:)
return Sipity::Agent.none if user.blank?
return Sipity::Agent.none unless user.persisted?
user_agent = PowerConverter.convert_to_sipity_agent(user)
user_agent = Sipity::Agent(user)
group_agents = user.groups.map do |g|
PowerConverter.convert_to_sipity_agent(Hyrax::Group.new(g))
Sipity::Agent(Hyrax::Group.new(g))
end
Sipity::Agent.where(id: group_agents + [user_agent])
end
Expand Down
17 changes: 17 additions & 0 deletions spec/models/sipity_spec.rb
@@ -1,4 +1,21 @@
RSpec.describe Sipity do
describe '.Agent' do
it 'will convert a Sipity::Agent' do
object = Sipity::Agent.new
expect(described_class.Agent(object)).to eq(object)
end

it 'will convert an object that responds to #to_sipity_agent' do
object = double(to_sipity_agent: :a_sipity_agent)
expect(described_class.Agent(object)).to eq(:a_sipity_agent)
end

it 'will raise an exception if it cannot convert the given object' do
expect { described_class.Agent(double) }
.to raise_error(PowerConverter::ConversionError)
end
end

describe '.Entity' do
context "with a Sipity::Entity" do
let(:object) { Sipity::Entity.new }
Expand Down

0 comments on commit f4c1ef3

Please sign in to comment.