Skip to content

Commit

Permalink
Refactor to put all publish and unpublish workflow logic in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
bess committed Dec 6, 2017
1 parent fa784e4 commit 2c98dba
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 36 deletions.
8 changes: 2 additions & 6 deletions app/controllers/tufts/publication_status_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ class PublicationStatusController < ApplicationController
def publish
work = ActiveFedora::Base.find(params[:id])
work.delete_draft
subject = Hyrax::WorkflowActionInfo.new(work, current_user)
sipity_workflow_action = PowerConverter.convert_to_sipity_action("publish", scope: subject.entity.workflow) { nil }
Hyrax::Workflow::WorkflowActionService.run(subject: subject, action: sipity_workflow_action, comment: "Published by #{current_user}")
Tufts::WorkflowStatus.publish(work: work, current_user: current_user, comment: "Published by #{current_user}")
flash[:notice] = "ID #{params[:id]} has been published."
redirect_back(fallback_location: root_path)
end

def unpublish
work = ActiveFedora::Base.find(params[:id])
subject = Hyrax::WorkflowActionInfo.new(work, current_user)
sipity_workflow_action = PowerConverter.convert_to_sipity_action("unpublish", scope: subject.entity.workflow) { nil }
Hyrax::Workflow::WorkflowActionService.run(subject: subject, action: sipity_workflow_action, comment: "Unpublished by #{current_user}")
Tufts::WorkflowStatus.unpublish(work: work, current_user: current_user, comment: "Unpublished by #{current_user}")
flash[:notice] = "ID #{params[:id]} has been unpublished."
redirect_back(fallback_location: root_path)
end
Expand Down
4 changes: 1 addition & 3 deletions app/jobs/publish_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
class PublishJob < BatchableJob
def perform(id)
work = ActiveFedora::Base.find(id)
subject = Hyrax::WorkflowActionInfo.new(work, workflow_user)
sipity_workflow_action = PowerConverter.convert_to_sipity_action("publish", scope: subject.entity.workflow) { nil }
Hyrax::Workflow::WorkflowActionService.run(subject: subject, action: sipity_workflow_action, comment: "Published by #{workflow_user.display_name} #{Time.zone.now}")
Tufts::WorkflowStatus.publish(work: work, current_user: workflow_user, comment: "Published by #{workflow_user.display_name} #{Time.zone.now}")
end

# The workflow needs the action to be performed by a user with admin rights
Expand Down
4 changes: 1 addition & 3 deletions app/jobs/unpublish_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
class UnpublishJob < BatchableJob
def perform(id)
work = ActiveFedora::Base.find(id)
subject = Hyrax::WorkflowActionInfo.new(work, workflow_user)
sipity_workflow_action = PowerConverter.convert_to_sipity_action("unpublish", scope: subject.entity.workflow) { nil }
Hyrax::Workflow::WorkflowActionService.run(subject: subject, action: sipity_workflow_action, comment: "Unpublished by #{workflow_user.display_name} #{Time.zone.now}")
Tufts::WorkflowStatus.unpublish(work: work, current_user: workflow_user, comment: "Unpublished by #{workflow_user.display_name} #{Time.zone.now}")
end

# The workflow needs the action to be performed by a user with admin rights
Expand Down
16 changes: 16 additions & 0 deletions app/lib/tufts/workflow_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ def self.draft_exist?(id)
Rails.application.config.drafts_storage_dir.join(id).exist?
end

##
# publish a work, and kick off a handle registration job if appropriate
def self.publish(work:, current_user:, comment:)
subject = Hyrax::WorkflowActionInfo.new(work, current_user)
sipity_workflow_action = PowerConverter.convert_to_sipity_action("publish", scope: subject.entity.workflow) { nil }
Hyrax::Workflow::WorkflowActionService.run(subject: subject, action: sipity_workflow_action, comment: comment)
end

##
# unpublish a work
def self.unpublish(work:, current_user:, comment:)
subject = Hyrax::WorkflowActionInfo.new(work, current_user)
sipity_workflow_action = PowerConverter.convert_to_sipity_action("unpublish", scope: subject.entity.workflow) { nil }
Hyrax::Workflow::WorkflowActionService.run(subject: subject, action: sipity_workflow_action, comment: comment)
end

def self.published_or_edited(id)
if draft_exist?(id)
'edited'
Expand Down
44 changes: 23 additions & 21 deletions spec/controllers/tufts/publication_status_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,29 @@
end

describe 'POST #publish' do
it 'sets the workflow status to published' do
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "unpublished"
post :publish, params: { "id" => work.id }
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "published"
end
it 'sets the workflow status to unpublished' do
post :publish, params: { "id" => work.id }
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "published"
post :unpublish, params: { "id" => work.id }
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "unpublished"
end
it "gets the workflow status" do
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "unpublished"
post :status, params: { "id" => work.id }
parsed_body = JSON.parse(response.body)
expect(parsed_body["status"]).to eq("unpublished")
post :publish, params: { "id" => work.id }
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "published"
post :status, params: { "id" => work.id }
parsed_body = JSON.parse(response.body)
expect(parsed_body["status"]).to eq("published")
context "publication status" do
it 'sets the workflow status to published' do
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "unpublished"
post :publish, params: { "id" => work.id }
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "published"
end
it 'sets the workflow status to unpublished' do
post :publish, params: { "id" => work.id }
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "published"
post :unpublish, params: { "id" => work.id }
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "unpublished"
end
it "gets the workflow status" do
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "unpublished"
post :status, params: { "id" => work.id }
parsed_body = JSON.parse(response.body)
expect(parsed_body["status"]).to eq("unpublished")
post :publish, params: { "id" => work.id }
expect(work.to_sipity_entity.reload.workflow_state_name).to eq "published"
post :status, params: { "id" => work.id }
parsed_body = JSON.parse(response.body)
expect(parsed_body["status"]).to eq("published")
end
end
end
end
29 changes: 26 additions & 3 deletions spec/lib/tufts/workflow_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
end

it 'returns unpublished for an unpublished work when given its ID' do
subject = Hyrax::WorkflowActionInfo.new(work, current_user)
sipity_workflow_action = PowerConverter.convert_to_sipity_action("unpublish", scope: subject.entity.workflow) { nil }
Hyrax::Workflow::WorkflowActionService.run(subject: subject, action: sipity_workflow_action, comment: "Published by #{current_user}")
expect(workflow_status.status(work.id)).to eq('unpublished')
end

Expand All @@ -37,4 +34,30 @@
expect(workflow_status.status(work.id)).to eq('edited')
File.delete(Rails.application.config.drafts_storage_dir.join(work.id))
end
describe "#publish" do
# rubocop:disable RSpec/MultipleExpectations
it "publishes a work" do
expect(workflow_status.status(work.id)).to eq('unpublished')
Tufts::WorkflowStatus.publish(work: work, current_user: current_user, comment: "Published by #{current_user}")
expect(workflow_status.status(work.id)).to eq('published')
Tufts::WorkflowStatus.publish(work: work, current_user: current_user, comment: "Published by #{current_user}")
expect(workflow_status.status(work.id)).to eq('published')
end
# rubocop:enable RSpec/MultipleExpectations
end
describe "#unpublish" do
# rubocop:disable RSpec/MultipleExpectations
# rubocop:disable RSpec/ExampleLength
it "unpublishes a work" do
expect(workflow_status.status(work.id)).to eq('unpublished')
Tufts::WorkflowStatus.publish(work: work, current_user: current_user, comment: "Published by #{current_user}")
expect(workflow_status.status(work.id)).to eq('published')
Tufts::WorkflowStatus.unpublish(work: work, current_user: current_user, comment: "Published by #{current_user}")
expect(workflow_status.status(work.id)).to eq('unpublished')
Tufts::WorkflowStatus.unpublish(work: work, current_user: current_user, comment: "Published by #{current_user}")
expect(workflow_status.status(work.id)).to eq('unpublished')
end
# rubocop:enable RSpec/MultipleExpectations
# rubocop:enable RSpec/ExampleLength
end
end

0 comments on commit 2c98dba

Please sign in to comment.