Sha256: fecabfe069efbd0746e210d9a6e26e87237020d30889025316851c9f29ccd9f0

Contents?: true

Size: 1.91 KB

Versions: 10

Compression:

Stored size: 1.91 KB

Contents

module Hyrax
  module Forms
    # Responsible for processing that the :current_ability (and
    # associated current_user) has taken a Sipity::WorkflowAction on
    # an object that has a Sipity::Entity.
    #
    # The form enforces that the action taken is valid.
    #
    # @see Ability
    # @see Sipity::WorkflowAction
    # @see Sipity::Entity
    # @see Hyrax::Workflow::ActionTakenService
    # @see Hyrax::Workflow::NotificationService
    # @see Hyrax::Workflow::PermissionQuery
    class WorkflowActionForm
      include ActiveModel::Validations
      extend ActiveModel::Translation

      def initialize(current_ability:, work:, attributes: {})
        @current_ability = current_ability
        @work = work
        @name = attributes.fetch(:name, false)
        @comment = attributes.fetch(:comment, nil)
        convert_to_sipity_objects!
      end

      attr_reader :current_ability, :work, :name, :comment

      def save
        return false unless valid?
        Workflow::WorkflowActionService.run(subject: subject,
                                            action: sipity_workflow_action,
                                            comment: comment)
        true
      end

      validates :name, presence: true
      validate :authorized_for_processing

      def authorized_for_processing
        return true if Hyrax::Workflow::PermissionQuery.authorized_for_processing?(
          user: subject.user,
          entity: subject.entity,
          action: sipity_workflow_action
        )
        errors.add(:base, :unauthorized)
        false
      end

      private

        def convert_to_sipity_objects!
          @subject = WorkflowActionInfo.new(work, current_user)
          @sipity_workflow_action = PowerConverter.convert_to_sipity_action(name, scope: subject.entity.workflow)
        end

        attr_reader :subject, :sipity_workflow_action

        delegate :current_user, to: :current_ability
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
hyrax-1.1.1 app/forms/hyrax/forms/workflow_action_form.rb
hyrax-1.1.0 app/forms/hyrax/forms/workflow_action_form.rb
hyrax-1.0.5 app/forms/hyrax/forms/workflow_action_form.rb
hyrax-1.0.4 app/forms/hyrax/forms/workflow_action_form.rb
hyrax-1.0.3 app/forms/hyrax/forms/workflow_action_form.rb
hyrax-1.0.2 app/forms/hyrax/forms/workflow_action_form.rb
hyrax-1.0.1 app/forms/hyrax/forms/workflow_action_form.rb
hyrax-1.0.0.rc2 app/forms/hyrax/forms/workflow_action_form.rb
hyrax-1.0.0.rc1 app/forms/hyrax/forms/workflow_action_form.rb
test_hyrax-0.0.1.alpha app/forms/hyrax/forms/workflow_action_form.rb