Skip to content

Commit

Permalink
Update batch_create_job and specs
Browse files Browse the repository at this point in the history
Importantly moves the extraction of model out of the loop, to be done
only once.

Also added some comments.
  • Loading branch information
atz committed Jan 13, 2017
1 parent 94f1fa6 commit 7e3dc73
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/controllers/concerns/sufia/works_controller_behavior.rb
Expand Up @@ -9,8 +9,8 @@ module WorksControllerBehavior
end

module ClassMethods
# We don't want the breadcrumb action to occur until after the concern has
# been loaded and authorized
# We don't want the actions to occur until after the concern has been loaded and authorized
# @note this is a terribly side-effecty kludge
def curation_concern_type=(curation_concern_type)
super
before_action :build_breadcrumbs, only: [:edit, :show]
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/batch_create_job.rb
Expand Up @@ -24,9 +24,9 @@ def perform(user, titles, resource_types, uploaded_files, attributes, log)
private

def create(user, titles, resource_types, uploaded_files, attributes, log)
model = attributes.delete(:model) || attributes.delete('model')
raise ArgumentError, 'attributes must include "model" => ClassName.to_s' unless model
uploaded_files.each do |upload_id|
model = attributes.delete(:model) || attributes.delete('model')
raise ArgumentError, 'attributes must include "model" => ClassName.to_s' unless model
title = [titles[upload_id]] if titles[upload_id]
resource_type = [resource_types[upload_id]] if resource_types[upload_id]
attributes = attributes.merge(uploaded_files: [upload_id],
Expand Down
1 change: 1 addition & 0 deletions spec/features/edit_work_spec.rb
Expand Up @@ -11,6 +11,7 @@

context 'when the user changes permissions' do
it 'confirms copying permissions to files using Sufia layout' do
# e.g. /concern/generic_works/jq085k20z/edit
visit edit_curation_concerns_generic_work_path(work)
choose('generic_work_visibility_open')
check('agreement')
Expand Down
35 changes: 12 additions & 23 deletions spec/jobs/batch_create_job_spec.rb
Expand Up @@ -5,25 +5,19 @@
before do
allow(CharacterizeJob).to receive(:perform_later)
allow(CurationConcerns.config.callback).to receive(:run)
allow(CurationConcerns.config.callback).to receive(:set?)
.with(:after_batch_create_success)
.and_return(true)
allow(CurationConcerns.config.callback).to receive(:set?)
.with(:after_batch_create_failure)
.and_return(true)
allow(CurationConcerns.config.callback).to receive(:set?).with(:after_batch_create_success).and_return(true)
allow(CurationConcerns.config.callback).to receive(:set?).with(:after_batch_create_failure).and_return(true)
end

describe "#perform" do
let(:file1) { File.open(fixture_path + '/world.png') }
let(:file2) { File.open(fixture_path + '/image.jp2') }
let(:upload1) { Sufia::UploadedFile.create(user: user, file: file1) }
let(:upload2) { Sufia::UploadedFile.create(user: user, file: file2) }
let(:upload1) { Sufia::UploadedFile.create(user: user, file: File.open(fixture_path + '/world.png')) }
let(:upload2) { Sufia::UploadedFile.create(user: user, file: File.open(fixture_path + '/image.jp2')) }
let(:title) { { upload1.id.to_s => 'File One', upload2.id.to_s => 'File Two' } }
let(:resource_types) { { upload1.id.to_s => 'Article', upload2.id.to_s => 'Image' } }
let(:metadata) { { keyword: [], model: 'GenericWork' } }
let(:uploaded_files) { [upload1.id.to_s, upload2.id.to_s] }
let(:errors) { double(full_messages: "It's broke!") }
let(:work) { double(errors: errors) }
# let(:errors) { double(full_messages: "It's broke!") }
let(:work) { build(:generic_work) }
let(:actor) { double(curation_concern: work) }

subject do
Expand All @@ -36,7 +30,7 @@
end

it "updates work metadata" do
expect(CurationConcerns::CurationConcern).to receive(:actor).and_return(actor).twice
expect(CurationConcerns::CurationConcern).to receive(:actor).with(an_instance_of(GenericWork), user).and_return(actor).twice
expect(actor).to receive(:create).with(keyword: [], title: ['File One'], resource_type: ["Article"], uploaded_files: ['1']).and_return(true)
expect(actor).to receive(:create).with(keyword: [], title: ['File Two'], resource_type: ["Image"], uploaded_files: ['2']).and_return(true)
expect(CurationConcerns.config.callback).to receive(:run).with(:after_batch_create_success, user)
Expand All @@ -46,24 +40,19 @@
end

context "when permissions_attributes are passed" do
let(:metadata) do
{ "permissions_attributes" => [{ "type" => "group", "name" => "public", "access" => "read" }] }
end
let(:permissions) { { 'permissions_attributes' => [{ 'type' => 'group', 'name' => 'public', 'access' => 'read' }] } }
let(:metadata) { super().merge(permissions) }
it "sets the groups" do
subject
work = GenericWork.last
expect(work.read_groups).to include "public"
expect(GenericWork.last.read_groups).to include "public"
end
end

context "when visibility is passed" do
let(:metadata) do
{ "visibility" => 'open' }
end
let(:metadata) { super().merge('visibility' => 'open') }
it "sets public read access" do
subject
work = GenericWork.last
expect(work.reload.read_groups).to eq ['public']
expect(GenericWork.last.reload.read_groups).to eq ['public']
end
end

Expand Down

0 comments on commit 7e3dc73

Please sign in to comment.