Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #447 from sul-dlss/initiate-apo
Browse files Browse the repository at this point in the history
Extract a service to create workflows
  • Loading branch information
justinlittman committed Dec 21, 2018
2 parents f51c2ee + e49b801 commit 86e3d7d
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 70 deletions.
3 changes: 2 additions & 1 deletion lib/dor-services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def logger
autoload :DigitalStacksService
autoload :SdrIngestService
autoload :CleanupService
autoload :CreateWorkflowService
autoload :ProvenanceMetadataService
autoload :TechnicalMetadataService
autoload :FileMetadataMergeService
Expand All @@ -171,12 +172,12 @@ def logger
autoload :PublicDescMetadataService
autoload :PublicXmlService
autoload :PublishMetadataService
autoload :TagService
autoload :ThumbnailService
autoload :Ontology
autoload :CreativeCommonsLicenseService
autoload :OpenDataLicenseService
autoload :VersionService
autoload :TagService
end

# Workflow Classes
Expand Down
18 changes: 2 additions & 16 deletions lib/dor/models/concerns/governable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,9 @@ module Governable
end

def initiate_apo_workflow(name)
create_workflow(name, !new_record?)
end

# Returns the default lane_id from the item's APO. Will return 'default' if the item does not have
# and APO, or if the APO does not have a default_lane
# @return [String] the lane id
def default_workflow_lane
return 'default' if admin_policy_object.nil? # TODO: log warning?

admin_md = admin_policy_object.datastreams['administrativeMetadata']
return 'default' unless admin_md.respond_to?(:default_workflow_lane) # Some APOs don't have this datastream

lane = admin_md.default_workflow_lane
return 'default' if lane.blank?

lane
CreateWorkflowService.create_workflow(self, name: name, create_ds: !new_record?)
end
deprecation_deprecate initiate_apo_workflow: 'Use Dor::CreateWorkflowService.create_workflow instead'

def reset_to_apo_default
rightsMetadata.content = admin_policy_object.rightsMetadata.ng_xml
Expand Down
7 changes: 2 additions & 5 deletions lib/dor/models/concerns/processable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,9 @@ def status(include_time = false)
# @param [Boolean] create_ds create a 'workflows' datastream in Fedora for the object
# @param [Integer] priority the workflow's priority level
def create_workflow(name, create_ds = true, priority = 0)
priority = workflows.current_priority if priority == 0
opts = { create_ds: create_ds, lane_id: default_workflow_lane }
opts[:priority] = priority if priority > 0
Dor::Config.workflow.client.create_workflow(Dor::WorkflowObject.initial_repo(name), pid, name, Dor::WorkflowObject.initial_workflow(name), opts)
workflows.content(true) # refresh the copy of the workflows datastream
CreateWorkflowService.create_workflow(self, name: name, create_ds: create_ds, priority: priority)
end
deprecation_deprecate create_workflow: 'Use CreateWorkflowService.create_workflow'

def initialize_workflow(name, create_ds = true, priority = 0)
warn 'WARNING: initialize_workflow is deprecated, use create_workflow instead'
Expand Down
51 changes: 51 additions & 0 deletions lib/dor/services/create_workflow_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

module Dor
class CreateWorkflowService
# Initilizes workflow for the object in the workflow service
# It will set the priorty of the new workflow to the current_priority if it is > 0
# It will set lane_id from the item's APO default workflow lane
# @param [String] name of the workflow to be initialized
# @param [Boolean] create_ds create a 'workflows' datastream in Fedora for the object
# @param [Integer] priority the workflow's priority level
def self.create_workflow(item, name:, create_ds: true, priority: 0)
new(item).create_workflow(name: name, create_ds: create_ds, priority: priority)
end

def initialize(item)
@item = item
end

def create_workflow(name:, create_ds: true, priority: 0)
priority = item.workflows.current_priority if priority == 0
opts = { create_ds: create_ds, lane_id: default_workflow_lane }
opts[:priority] = priority if priority > 0
Dor::Config.workflow.client.create_workflow(Dor::WorkflowObject.initial_repo(name),
item.pid,
name,
Dor::WorkflowObject.initial_workflow(name),
opts)
item.workflows.content(true) # refresh the copy of the workflows datastream
end

private

attr_reader :item
delegate :admin_policy_object, to: :item

# Returns the default lane_id from the item's APO. Will return 'default' if the item does not have
# and APO, or if the APO does not have a default_lane
# @return [String] the lane id
def default_workflow_lane
return 'default' if admin_policy_object.nil? # TODO: log warning?

admin_md = admin_policy_object.administrativeMetadata
return 'default' unless admin_md.respond_to?(:default_workflow_lane) # Some APOs don't have this datastream

lane = admin_md.default_workflow_lane
return lane unless lane.blank?

'default'
end
end
end
44 changes: 3 additions & 41 deletions spec/models/concerns/governable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,45 +269,14 @@ class GovernableItem < ActiveFedora::Base
end

describe 'initiate_apo_workflow' do
it 'calls Processable.create_workflow without creating a datastream when the object is new' do
it 'calls CreateWorkflowService.create_workflow without creating a datastream when the object is new' do
expect(Deprecation).to receive(:warn)
i = GovernableItem.new
expect(i).to receive(:create_workflow).with('accessionWF', false)
expect(Dor::CreateWorkflowService).to receive(:create_workflow).with(i, name: 'accessionWF', create_ds: false)
i.initiate_apo_workflow('accessionWF')
end
end

describe '#default_workflow_lane' do
before :each do
@item = instantiate_fixture('druid:ab123cd4567', GovernableItem)
end
it "returns the default lane as defined in the object's APO" do
apo = instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject)
allow(@item).to receive(:admin_policy_object) { apo }
expect(@item.default_workflow_lane).to eq 'fast'
end
it "returns the value 'default' if the object does not have an APO" do
allow(@item).to receive(:admin_policy_object) { nil }
expect(@item.default_workflow_lane).to eq 'default'
end
it "returns the value 'default' if the object's APO does not have a default lane defined" do
apo = instantiate_fixture('druid:zt570tx3016', Dor::AdminPolicyObject)
allow(@item).to receive(:admin_policy_object) { apo }
expect(@item.default_workflow_lane).to eq 'default'
end
it "returns the value 'default' if the object's APO does not have administrativeMetadata" do
apo = instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject)
allow(@item).to receive(:admin_policy_object) { apo }
allow(apo.datastreams).to receive(:[]).with('administrativeMetadata').and_return(nil)
expect(@item.default_workflow_lane).to eq 'default'
end
it "returns the value 'default' for a newly created object" do
apo = instantiate_fixture('druid:zt570tx3016', Dor::AdminPolicyObject)
item = GovernableItem.new
item.admin_policy_object = apo
expect(item.default_workflow_lane).to eq 'default'
end
end

describe 'add_collection' do
it 'should add a collection' do
@item.add_collection('druid:oo201oo0002')
Expand All @@ -323,13 +292,6 @@ class GovernableItem < ActiveFedora::Base
end
end

describe 'initiate_apo_workflow' do
it 'calls Processable.create_workflow without creating a datastream when the object is new' do
i = GovernableItem.new
expect(i).to receive(:create_workflow).with('accessionWF', false)
i.initiate_apo_workflow('accessionWF')
end
end
describe 'can_manage_item?' do
it 'should match a group that has rights' do
expect(@item.can_manage_item?(['dor-administrator'])).to be_truthy
Expand Down
12 changes: 5 additions & 7 deletions spec/models/concerns/processable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,11 @@ class ProcessableWithApoItem < ActiveFedora::Base
end

describe '#create_workflow' do
it "sets the lane_id option from the object's APO" do
apo = instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject)
item = instantiate_fixture('druid:ab123cd4567', ProcessableWithApoItem)
allow(item).to receive(:admin_policy_object) { apo }
expect(Dor::WorkflowObject).to receive(:initial_workflow).and_return('<xml/>')
expect(Dor::WorkflowObject).to receive(:initial_repo).and_return('dor')
expect(Dor::Config.workflow.client).to receive(:create_workflow).with('dor', 'druid:ab123cd4567', 'accessionWF', '<xml/>', create_ds: true, lane_id: 'fast')
let(:item) { instantiate_fixture('druid:ab123cd4567', ProcessableWithApoItem) }

it 'delegates to CreateWorkflowService' do
expect(Deprecation).to receive(:warn)
expect(Dor::CreateWorkflowService).to receive(:create_workflow).with(item, name: 'accessionWF', create_ds: true, priority: 0)
item.create_workflow('accessionWF')
end
end
Expand Down
70 changes: 70 additions & 0 deletions spec/services/create_workflow_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Dor::CreateWorkflowService do
describe '.create_workflow' do
subject(:create_workflow) { described_class.create_workflow(item, name: 'accessionWF') }
before do
allow(item).to receive(:admin_policy_object).and_return(apo)
end
let(:apo) { instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject) }
let(:item) { instantiate_fixture('druid:ab123cd4567', Dor::Item) }

it "sets the lane_id option from the object's APO" do
expect(Dor::WorkflowObject).to receive(:initial_workflow).and_return('<xml/>')
expect(Dor::WorkflowObject).to receive(:initial_repo).and_return('dor')
expect(Dor::Config.workflow.client).to receive(:create_workflow).with('dor', 'druid:ab123cd4567', 'accessionWF', '<xml/>', create_ds: true, lane_id: 'fast')
create_workflow
end
end

describe '#default_workflow_lane' do
let(:item) { instantiate_fixture('druid:ab123cd4567', Dor::Item) }
subject { described_class.new(item).send(:default_workflow_lane) }

context 'when the object has an APO' do
before do
allow(item).to receive(:admin_policy_object).and_return(apo)
end

context 'with the fast lane defined in the apo' do
let(:apo) { instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject) }

it { is_expected.to eq 'fast' }
end

context 'without a lane defined' do
let(:apo) { instantiate_fixture('druid:zt570tx3016', Dor::AdminPolicyObject) }

it { is_expected.to eq 'default' }
end

context 'without administrativeMetadata' do
let(:apo) { instantiate_fixture('druid:fg890hi1234', Dor::AdminPolicyObject) }
before do
allow(apo.datastreams).to receive(:[]).with('administrativeMetadata').and_return(nil)
end

it { is_expected.to eq 'default' }
end
end

context 'when the object does not have an apo' do
before do
allow(item).to receive(:admin_policy_object).and_return(nil)
end

it { is_expected.to eq 'default' }
end

context 'when the object is newly created' do
let(:item) do
Dor::Item.new.tap do |i|
i.admin_policy_object = instantiate_fixture('druid:zt570tx3016', Dor::AdminPolicyObject)
end
end
it { is_expected.to eq 'default' }
end
end
end

0 comments on commit 86e3d7d

Please sign in to comment.