Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Add link to collection Actions tab to enqueue publish/unpublish job; …
Browse files Browse the repository at this point in the history
…closes #DDR-173.

Backported from DulHydra 5.  Cf. commits db6e7e8 and c4bf2be.
  • Loading branch information
Jim Coble committed Oct 19, 2016
1 parent 94298d8 commit afb6970
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 20 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq libclamav6 libclamav-dev clamav clamav-data
- sudo apt-get install -qq libvips-dev
- gem install bundler
rvm:
- 2.1.3
before_script:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ gem 'rails', '~> 4.1.6'
gem 'hydra-head', '~> 7.2.0'
gem 'ddr-alerts', '~> 1.0.0'
gem 'ddr-batch', '1.1.0.rc1'
gem 'ddr-models', '2.4.16'
gem 'ddr-models', github: 'duke-libraries/ddr-models', ref: '814c6044a969e4803c2765030e59c59498bcbac7'
gem 'rubydora', '>= 1.8.1'
gem 'devise'
gem 'deprecation'
Expand Down
42 changes: 24 additions & 18 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
GIT
remote: git://github.com/duke-libraries/ddr-models.git
revision: 814c6044a969e4803c2765030e59c59498bcbac7
ref: 814c6044a969e4803c2765030e59c59498bcbac7
specs:
ddr-models (2.5.0.pre)
active-fedora (~> 7.0)
activeresource
cancancan (~> 1.12)
ddr-antivirus (~> 2.1.1)
devise (~> 3.4)
ezid-client (~> 1.4.2)
grouper-rest-client
hashie (~> 3.4, < 3.4.4)
hydra-core (~> 7.2)
hydra-validations (~> 0.5)
net-ldap (~> 0.11)
omniauth-shibboleth (~> 1.2.0)
rails (~> 4.1.6)
rdf-vocab (~> 0.8)
resque (~> 1.25)
virtus (~> 1.0.5)

GEM
remote: http://rubygems.org/
specs:
Expand Down Expand Up @@ -97,23 +120,6 @@ GEM
log4r
paperclip (~> 4.2.0)
rails (~> 4.1.13)
ddr-models (2.4.16)
active-fedora (~> 7.0)
activeresource
cancancan (~> 1.12)
ddr-antivirus (~> 2.1.1)
devise (~> 3.4)
ezid-client (~> 1.4.2)
grouper-rest-client
hashie (~> 3.4, < 3.4.4)
hydra-core (~> 7.2)
hydra-validations (~> 0.5)
net-ldap (~> 0.11)
omniauth-shibboleth (~> 1.2.0)
rails (~> 4.1.6)
rdf-vocab (~> 0.8)
resque (~> 1.25)
virtus (~> 1.0.5)
deprecation (0.2.2)
activesupport
descendants_tracker (0.0.4)
Expand Down Expand Up @@ -458,7 +464,7 @@ DEPENDENCIES
database_cleaner
ddr-alerts (~> 1.0.0)
ddr-batch (= 1.1.0.rc1)
ddr-models (= 2.4.16)
ddr-models!
deprecation
devise
equivalent-xml
Expand Down
1 change: 1 addition & 0 deletions app/controllers/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class CollectionsController < ApplicationController
include DulHydra::Controller::HasChildrenBehavior
include DulHydra::Controller::HasAttachmentsBehavior
include DulHydra::Controller::HasTargetsBehavior
include DulHydra::Controller::PublicationBehavior

before_action :set_desc_metadata, only: :create
self.tabs += [ :tab_actions ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module DulHydra
module Controller
module PublicationBehavior

def publish
Resque.enqueue(PublishJob, current_object.id, current_user.email)
flash[:success] = "Collection (and descendants) queued to be published"
redirect_to action: :show
end

def unpublish
Resque.enqueue(UnPublishJob, current_object.id, current_user.email)
flash[:success] = "Collection (and descendants) queued to be un-published"
redirect_to action: :show
end

end
end
end
9 changes: 9 additions & 0 deletions app/jobs/publication_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class PublicationJob

def self.send_notification(email:, subject: 'Publication Job', message:)
mail = JobMailer.basic(to: email,
subject: subject,
message: message)
end

end
19 changes: 19 additions & 0 deletions app/jobs/publish_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class PublishJob < PublicationJob

@queue = :publication

def self.perform(id, email_addr)
obj = ActiveFedora::Base.find(id)
obj.publish!
send_notification(email: email_addr,
subject: 'Publication Job COMPLETED',
message: "Publication of #{id} (and its descendants) has completed.")
end

def self.on_failure_send_notification(id, email_addr)
send_notification(email: email_addr,
subject: 'Publication Job FAILED',
message: "Publication of #{id} (and its descendants) FAILED.")
end

end
19 changes: 19 additions & 0 deletions app/jobs/un_publish_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class UnPublishJob < PublicationJob

@queue = :publication

def self.perform(id, email_addr)
obj = ActiveFedora::Base.find(id)
obj.unpublish!
send_notification(email: email_addr,
subject: 'Un-Publication Job COMPLETED',
message: "Un-Publication of #{id} (and its descendants) has completed.")
end

def self.on_failure_send_notification(id, email_addr)
send_notification(email: email_addr,
subject: 'Un-Publication Job FAILED',
message: "Un-Publication of #{id} (and its descendants) FAILED.")
end

end
12 changes: 12 additions & 0 deletions app/views/collections/_actions.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@
export_collection_path(id: current_object, format: "csv", type: "techmd")
%>
</li>
<li class="list-group-item">
<%= link_to_if can?(:publish, current_object),
'Publish Collection and Descendants',
publish_collection_path(id: current_object)
%>
</li>
<li class="list-group-item">
<%= link_to_if can?(:unpublish, current_object),
'Un-Publish Collection and Descendants',
unpublish_collection_path(id: current_object)
%>
</li>
</ul>
11 changes: 10 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def event_routes
get 'events/:event_id', to: :event
end

def publication_routes
get 'publish'
get 'unpublish'
end

def roles_routes
get 'roles'
patch 'roles'
Expand Down Expand Up @@ -85,16 +90,20 @@ def repository_content_resource name
end

repository_resource :collections do
publication_routes
get 'items'
get 'attachments'
get 'targets'
get 'export'
post 'export'
end
repository_resource :items do
publication_routes
get 'components'
end
repository_content_resource :components
repository_content_resource :components do
publication_routes
end
repository_content_resource :attachments
repository_content_resource :targets
resources :thumbnail, only: :show, constraints: {id: pid_constraint}
Expand Down
43 changes: 43 additions & 0 deletions spec/controllers/collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,47 @@ def new_collection
end
end

describe "#publish" do
let(:collection) { FactoryGirl.create(:collection, :has_item) }
describe "when the user can publish the collection" do
before do
collection.roles.grant role_type: "Curator", agent: user
collection.save!
end
it "publishes the collection and its descendants" do
get :publish, id: collection
expect(flash[:success]).to be_present
expect(response).to redirect_to(collection_url)
end
end
describe "when the user cannot publish the collection" do
it "is unauthorized" do
get :publish, id: collection
expect(response.response_code).to eq 403
end
end
end

describe "#unpublish" do
let(:collection) { FactoryGirl.create(:collection, :has_item) }
before { collection.publish! }
describe "when the user can un-publish the collection" do
before do
collection.roles.grant role_type: "Curator", agent: user
collection.save!
end
it "unpublishes the collection and its descendants" do
get :unpublish, id: collection
expect(flash[:success]).to be_present
expect(response).to redirect_to(collection_url)
end
end
describe "when the user cannot unpublish the collection" do
it "is unauthorized" do
get :unpublish, id: collection
expect(response.response_code).to eq 403
end
end
end

end
24 changes: 24 additions & 0 deletions spec/jobs/publish_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

RSpec.describe PublishJob, type: :job do

describe ".perform" do
let!(:obj) { double(id: 'test-1', publish!: nil) }
let(:email) { '[email protected]' }
let(:expected_msg) { "Publication of #{obj.id} (and its descendants) has completed." }
before do
allow(ActiveFedora::Base).to receive(:find).with(obj.id) { obj }
end
it "should call 'publish!' on the object" do
expect(obj).to receive(:publish!)
described_class.perform('test-1', email)
end
it "should generate an email" do
expect(JobMailer).to receive(:basic).with(to: email,
subject: 'Publication Job COMPLETED',
message: expected_msg)
described_class.perform('test-1', email)
end
end

end
23 changes: 23 additions & 0 deletions spec/jobs/un_publish_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'spec_helper'

RSpec.describe UnPublishJob, type: :job do

describe ".perform" do
let!(:obj) { double(id: 'test-1', unpublish!: nil) }
let(:email) { '[email protected]' }
let(:expected_msg) { "Un-Publication of #{obj.id} (and its descendants) has completed." }
before do
allow(ActiveFedora::Base).to receive(:find).with(obj.id) { obj }
end
it "should call 'unpublish!' on the object" do
expect(obj).to receive(:unpublish!)
described_class.perform('test-1', email)
end
it "should generate an email" do
expect(JobMailer).to receive(:basic).with(to: email,
subject: 'Un-Publication Job COMPLETED',
message: expected_msg)
described_class.perform('test-1', email)
end
end
end
11 changes: 11 additions & 0 deletions spec/mailers/job_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

module Mailers

RSpec.describe JobMailer, type: :mailer do
it "should generate an email" do
JobMailer.basic(subject: 'Job', to: '[email protected]', message: 'Job completed').deliver!
expect(ActionMailer::Base.deliveries).not_to be_empty
end
end
end

0 comments on commit afb6970

Please sign in to comment.