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

Deprecate PowerConverter #4097

Merged
merged 7 commits into from Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/conversions/power_converters/polymorphic_type.rb
@@ -1,4 +1,5 @@
PowerConverter.define_conversion_for(:polymorphic_type) do |input|
Deprecation.warn('PowerConverter is deprecated. Use `.base_class` instead')
if input.respond_to?(:base_class)
input.base_class
elsif input.is_a?(ActiveRecord::Base)
Expand Down
1 change: 1 addition & 0 deletions app/conversions/power_converters/sipity_action.rb
@@ -1,4 +1,5 @@
PowerConverter.define_conversion_for(:sipity_action) do |input, scope|
Deprecation.warn('PowerConverter is deprecated. Use `Sipity::WorkflowAction(input, workflow)` instead')
workflow_id = PowerConverter.convert_to_sipity_workflow_id(scope)
case input
when Sipity::WorkflowAction
Expand Down
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
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
1 change: 1 addition & 0 deletions app/conversions/power_converters/sipity_entity.rb
@@ -1,4 +1,5 @@
PowerConverter.define_conversion_for(:sipity_entity) do |input|
Deprecation.warn('PowerConverter is deprecated. Use `Sipity::Entity(input)` instead')
case input
when URI::GID
Sipity::Entity.find_by(proxy_for_global_id: input.to_s)
Expand Down
1 change: 1 addition & 0 deletions app/conversions/power_converters/sipity_role.rb
@@ -1,4 +1,5 @@
PowerConverter.define_conversion_for(:sipity_role) do |input|
Deprecation.warn('PowerConverter is deprecated. Use `Sipity::WorkflowAction.name_for(input)` instead')
case input
when Sipity::Role
input
Expand Down
2 changes: 2 additions & 0 deletions app/conversions/power_converters/sipity_workflow_state.rb
@@ -1,6 +1,8 @@
require 'power_converter'

PowerConverter.define_conversion_for(:sipity_workflow_state) do |input, workflow|
Deprecation.warn('PowerConverter is deprecated. Use `Sipity::WorkflowState(input, workflow)` instead')

case input
when Sipity::WorkflowState
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
8 changes: 7 additions & 1 deletion app/forms/hyrax/forms/workflow_action_form.rb
Expand Up @@ -50,9 +50,15 @@ def authorized_for_processing

private

class << self
def workflow_action_for(name, scope:)
Sipity::WorkflowAction(name, scope) { nil }
end
end

def convert_to_sipity_objects!
@subject = WorkflowActionInfo.new(work, current_user)
@sipity_workflow_action = PowerConverter.convert_to_sipity_action(name, scope: subject.entity.workflow) { nil }
@sipity_workflow_action = self.class.workflow_action_for(name, scope: subject.entity.workflow)
end

attr_reader :subject, :sipity_workflow_action
Expand Down
6 changes: 3 additions & 3 deletions app/indexers/hyrax/indexes_workflow.rb
Expand Up @@ -21,11 +21,11 @@ def index_suppressed(solr_document)
# Write the workflow roles and state so one can see where the document moves to next
# @param [Hash] solr_document the solr document to add the field to
def index_workflow_fields(solr_document)
return unless object.persisted?
entity = PowerConverter.convert_to_sipity_entity(object)
return if entity.nil?
entity = Sipity::Entity(object)
solr_document[workflow_role_field] = workflow_roles(entity).map { |role| "#{entity.workflow.permission_template.source_id}-#{entity.workflow.name}-#{role}" }
solr_document[workflow_state_name_field] = entity.workflow_state.name if entity.workflow_state
rescue Sipity::ConversionError
nil
end

def workflow_state_name_field
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/hyrax/suppressible.rb
Expand Up @@ -22,7 +22,7 @@ def suppressed?
##
# @deprecated Use `PowerConverter.convert_to_sipity_entity(obj)` instead.
def to_sipity_entity
Deprecation.warn "Use `PowerConverter.convert_to_sipity_entity(obj)` instead."
Deprecation.warn "Use `Sipity::Entity(entity)` instead."
raise "Can't create an entity until the model has been persisted" unless persisted?
@sipity_entity ||= Sipity::Entity.find_by(proxy_for_global_id: to_global_id.to_s)
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/hyrax/workflow_action_info.rb
Expand Up @@ -4,8 +4,8 @@ class WorkflowActionInfo
def initialize(work, user)
@work = work
@user = user
@entity = PowerConverter.convert(work, to: :sipity_entity)
@agent = PowerConverter.convert(user, to: :sipity_agent)
@entity = Sipity::Entity(work)
@agent = Sipity::Agent(user)
end

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

module Sipity
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
# rubocop:disable Naming/MethodName, Metrics/CyclomaticComplexity, Metrics/MethodLength
def Entity(input, &block)
result = case input
when Sipity::Entity
input
when URI::GID, GlobalID
Entity.find_by(proxy_for_global_id: input.to_s)
when SolrDocument
Entity(input.to_model)
when Sipity::Comment
Entity(input.entity)
else
Entity(input.to_global_id) if input.respond_to?(:to_global_id)
end

handle_conversion(input, result, :to_sipity_entity, &block)
rescue URI::GID::MissingModelIdError
Entity(nil)
end
module_function :Entity
# rubocop:enable Naming/MethodName, Metrics/CyclomaticComplexity, Metrics/MethodLength

##
# Cast an object to an Role
def Role(input, &block) # rubocop:disable Naming/MethodName
result = case input
when Sipity::Role
input
when String, Symbol
Sipity::Role.find_or_create_by(name: input)
end

handle_conversion(input, result, :to_sipity_role, &block)
end
module_function :Role

##
# Cast an object to a WorkflowAction in a given workflow
def WorkflowAction(input, workflow, &block) # 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

handle_conversion(input, result, :to_sipity_action, &block)
end
module_function :WorkflowAction

##
# Cast an object to a WorkflowState in a given workflow
def WorkflowState(input, workflow, &block) # rubocop:disable Naming/MethodName
result = case input
when Sipity::WorkflowState
input
when Symbol, String
WorkflowState.find_by(workflow_id: workflow.id, name: input)
end

handle_conversion(input, result, :to_sipity_workflow_state, &block)
end
module_function :WorkflowState

class ConversionError < PowerConverter::ConversionError
def initialize(value, **options)
options[:scope] ||= nil
options[:to] ||= nil
super(value, options)
end
end

##
# Provides compatibility with the old `PowerConverter` conventions
def handle_conversion(input, result, method_name)
result ||= input.try(method_name)
return result unless result.nil?
return yield if block_given?

raise ConversionError.new(input) # rubocop:disable Style/RaiseArgs
end
module_function :handle_conversion
end
13 changes: 13 additions & 0 deletions app/models/sipity/workflow_action.rb
Expand Up @@ -23,5 +23,18 @@ class WorkflowAction < ActiveRecord::Base
dependent: :destroy,
as: :scope_for_notification,
class_name: 'Sipity::NotifiableContext'

##
# @param [Object] input
# @return [String]
def self.name_for(input, &block)
result = case input
when String, Symbol
input.to_s.sub(/[\?\!]\Z/, '')
when Sipity::WorkflowAction
input.name
end
Sipity.handle_conversion(input, result, :to_sipity_action_name, &block)
end
end
end
2 changes: 1 addition & 1 deletion app/presenters/hyrax/inspect_work_presenter.rb
Expand Up @@ -27,7 +27,7 @@ def solr
private

def sipity_entity
@sipity_entity ||= PowerConverter.convert(solr_document, to: :sipity_entity)
@sipity_entity ||= Sipity::Entity(solr_document)
end

def sipity_entity_roles
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/hyrax/workflow_presenter.rb
Expand Up @@ -42,7 +42,7 @@ def action_label(action)
end

def sipity_entity
PowerConverter.convert(solr_document, to: :sipity_entity)
Sipity::Entity(solr_document)
rescue PowerConverter::ConversionError
nil
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/hyrax/workflow/notification_generator.rb
Expand Up @@ -39,7 +39,7 @@ def assign_recipients_to(notification:)
notification_configuration.recipients.slice(:to, :cc, :bcc).each do |(recipient_strategy, recipient_roles)|
Array.wrap(recipient_roles).each do |role|
notification.recipients.find_or_create_by!(
role: PowerConverter.convert_to_sipity_role(role), recipient_strategy: recipient_strategy.to_s
role: Sipity::Role(role), recipient_strategy: recipient_strategy.to_s
)
end
end
Expand All @@ -57,7 +57,7 @@ def assign_scope_and_reason_to(notification:)
attr_reader :scope

def assign_scope!
@scope = PowerConverter.convert_to_sipity_action(notification_configuration.scope, scope: workflow)
@scope = Sipity::WorkflowAction(notification_configuration.scope, workflow)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/services/hyrax/workflow/permission_generator.rb
Expand Up @@ -55,19 +55,19 @@ 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)
@action_names = Array.wrap(input)
end

def roles=(input)
@roles = Array.wrap(input).map { |role| PowerConverter.convert_to_sipity_role(role) }
@roles = Array.wrap(input).map { |role| Sipity::Role(role) }
end

def entity=(entity)
@entity = PowerConverter.convert_to_sipity_entity(entity)
@entity = Sipity::Entity(entity)
end

public
Expand Down
28 changes: 14 additions & 14 deletions app/services/hyrax/workflow/permission_query.rb
Expand Up @@ -43,7 +43,7 @@ def workflow_roles
# * Actions to which the user Only actions permitted to the user
#
# @param user [User]
# @param entity [#to_sipity_entity] an object that can be converted into a Sipity::Entity
# @param entity [Object] an object that can be converted into a Sipity::Entity
# @return [ActiveRecord::Relation<Sipity::WorkflowAction>]
def scope_permitted_workflow_actions_available_for_current_state(user:, entity:)
workflow_actions_scope = scope_workflow_actions_available_for_current_state(entity: entity)
Expand All @@ -66,8 +66,8 @@ def scope_permitted_workflow_actions_available_for_current_state(user:, entity:)
# @param role [Object] that can be converted into a Sipity::Role
# @return [ActiveRecord::Relation<Sipity::Agent>] augmented with
def scope_agents_associated_with_entity_and_role(entity:, role:)
entity = PowerConverter.convert_to_sipity_entity(entity)
role = PowerConverter.convert_to_sipity_role(role)
entity = Sipity::Entity(entity)
role = Sipity::Role(role)

agents = Sipity::Agent.arel_table

Expand Down Expand Up @@ -126,7 +126,7 @@ def scope_agents_associated_with_entity_and_role(entity:, role:)
# @param entity [Object] that can be converted into a Sipity::Entity
# @return [ActiveRecord::Relation<Sipity::Role>]
def scope_roles_associated_with_the_given_entity(entity:)
entity = PowerConverter.convert_to_sipity_entity(entity)
entity = Sipity::Entity(entity)
return Sipity::Role.none unless entity
Sipity::Role.where(
Sipity::Role.arel_table[:id].in(
Expand All @@ -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 All @@ -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 Expand Up @@ -240,9 +240,9 @@ def scope_entities_for_the_user(user:)
# @param entity an object that can be converted into a Sipity::Entity
# @return [ActiveRecord::Relation<User>]
def scope_users_for_entity_and_roles(entity:, roles:)
entity = PowerConverter.convert_to_sipity_entity(entity)
role_ids = Array.wrap(roles).map { |role| PowerConverter.convert_to_sipity_role(role).id }
user_polymorphic_type = PowerConverter.convert_to_polymorphic_type(::User)
entity = Sipity::Entity(entity)
role_ids = Array.wrap(roles).map { |role| Sipity::Role(role).id }
user_polymorphic_type = ::User.base_class

user_table = ::User.arel_table
agent_table = Sipity::Agent.arel_table
Expand Down Expand Up @@ -290,7 +290,7 @@ def user_emails_for_entity_and_roles(entity:, roles:)
# @param entity an object that can be converted into a Sipity::Entity
# @return [ActiveRecord::Relation<Sipity::WorkflowRole>]
def scope_processing_workflow_roles_for_user_and_entity(user:, entity:)
entity = PowerConverter.convert_to_sipity_entity(entity)
entity = Sipity::Entity(entity)
workflow_scope = scope_processing_workflow_roles_for_user_and_workflow(user: user, workflow: entity.workflow)

entity_specific_scope = scope_processing_workflow_roles_for_user_and_entity_specific(user: user, entity: entity)
Expand Down Expand Up @@ -335,7 +335,7 @@ def scope_processing_workflow_roles_for_user_and_workflow(user:, workflow:)
# @param entity an object that can be converted into a Sipity::Entity
# @return [ActiveRecord::Relation<Sipity::WorkflowRole>]
def scope_processing_workflow_roles_for_user_and_entity_specific(user:, entity:)
entity = PowerConverter.convert_to_sipity_entity(entity)
entity = Sipity::Entity(entity)
agent_scope = scope_processing_agents_for(user: user)

Sipity::WorkflowRole.where(
Expand Down Expand Up @@ -373,7 +373,7 @@ def scope_processing_workflow_roles_for_user_and_entity_specific(user:, entity:)
# @param entity an object that can be converted into a Sipity::Entity
# @return [ActiveRecord::Relation<Sipity::WorkflowStateAction>]
def scope_permitted_entity_workflow_state_actions(user:, entity:)
entity = PowerConverter.convert_to_sipity_entity(entity)
entity = Sipity::Entity(entity)
workflow_state_actions = Sipity::WorkflowStateAction
permissions = Sipity::WorkflowStateActionPermission
role_scope = scope_processing_workflow_roles_for_user_and_entity(user: user, entity: entity)
Expand Down Expand Up @@ -405,7 +405,7 @@ def scope_permitted_entity_workflow_state_actions(user:, entity:)
# @param entity an object that can be converted into a Sipity::Entity
# @return [ActiveRecord::Relation<Sipity::WorkflowAction>]
def scope_workflow_actions_for_current_state(entity:)
entity = PowerConverter.convert_to_sipity_entity(entity)
entity = Sipity::Entity(entity)
state_actions_table = Sipity::WorkflowStateAction.arel_table
Sipity::WorkflowAction.where(
Sipity::WorkflowAction.arel_table[:id].in(
Expand Down