Skip to content

Commit

Permalink
Moves UniUpdatesBatch as model method for each controller and adds fi…
Browse files Browse the repository at this point in the history
…lter_duplicate_barcodes concern
  • Loading branch information
jgreben committed Mar 20, 2018
1 parent ba0374d commit d137ad1
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/controllers/change_current_locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def create
@change_current_location = ChangeCurrentLocation.new(params[:change_current_location])
if @change_current_location.valid?
array_of_item_ids = @change_current_location.parse_uploaded_file
filtered_item_ids = UniUpdates.filter_duplicates(array_of_item_ids)
filtered_item_ids = FilterDuplicateBarcodes.filtered_item_ids(array_of_item_ids)
uniques = filtered_item_ids[0]
duplicates = filtered_item_ids[1].join(' ')
@uni_updates_batch = UniUpdatesBatch.create_current_location_batch(params, uniques.count)
@uni_updates_batch = ChangeCurrentLocation.batch_for_transfer_item(params, uniques)
UniUpdates.create_for_batch(uniques, @uni_updates_batch)
WebformsMailer.batch_upload_email(@uni_updates_batch, duplicates).deliver_now
flash[:notice] = 'Batch uploaded!'
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/change_home_locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def create
@change_home_location = ChangeHomeLocation.new(params[:change_home_location])
if @change_home_location.valid?
array_of_item_ids = @change_home_location.parse_uploaded_file
filtered_item_ids = UniUpdates.filter_duplicates(array_of_item_ids)
filtered_item_ids = FilterDuplicateBarcodes.filtered_item_ids(array_of_item_ids)
uniques = filtered_item_ids[0]
duplicates = filtered_item_ids[1].join(' ')
@uni_updates_batch = UniUpdatesBatch.create_home_location_batch(params, uniques.count)
@uni_updates_batch = ChangeHomeLocation.batch_for_transfer_item(params, uniques)
UniUpdates.create_for_batch(uniques, @uni_updates_batch)
WebformsMailer.batch_upload_email(@uni_updates_batch, duplicates).deliver_now
flash[:notice] = 'Batch uploaded!'
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/change_item_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def create
@change_item_type = ChangeItemType.new(params[:change_item_type])
if @change_item_type.valid?
array_of_item_ids = @change_item_type.parse_uploaded_file
filtered_item_ids = UniUpdates.filter_duplicates(array_of_item_ids)
filtered_item_ids = FilterDuplicateBarcodes.filtered_item_ids(array_of_item_ids)
uniques = filtered_item_ids[0]
duplicates = filtered_item_ids[1].join(' ')
@uni_updates_batch = UniUpdatesBatch.create_item_type_batch(params, uniques.count)
@uni_updates_batch = ChangeItemType.batch_for_transfer_item(params, uniques)
UniUpdates.create_for_batch(uniques, @uni_updates_batch)
WebformsMailer.batch_upload_email(@uni_updates_batch, duplicates).deliver_now
flash[:notice] = 'Batch uploaded!'
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/concerns/filter_duplicate_barcodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
###
# Utilities to Filter duplicate barcodes from UniUpdates
###
module FilterDuplicateBarcodes
extend ActiveSupport::Concern

def self.filtered_item_ids(array_of_item_ids)
UniUpdates.filter_duplicates(array_of_item_ids)
end
end
4 changes: 2 additions & 2 deletions app/controllers/transfer_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def create
@transfer_item = TransferItem.new(params[:transfer_item])
if @transfer_item.valid?
array_of_item_ids = @transfer_item.parse_uploaded_file
filtered_item_ids = UniUpdates.filter_duplicates(array_of_item_ids)
filtered_item_ids = FilterDuplicateBarcodes.filtered_item_ids(array_of_item_ids)
uniques = filtered_item_ids[0]
duplicates = filtered_item_ids[1].join(' ')
@uni_updates_batch = UniUpdatesBatch.create_transfer_item_batch(params, uniques.count)
@uni_updates_batch = TransferItem.batch_for_transfer_item(params, uniques)
UniUpdates.create_for_batch(uniques, @uni_updates_batch)
WebformsMailer.batch_upload_email(@uni_updates_batch, duplicates).deliver_now
flash[:notice] = 'Batch uploaded!'
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/withdraw_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def create
@withdraw_item = WithdrawItem.new(params[:withdraw_item])
if @withdraw_item.valid?
array_of_item_ids = @withdraw_item.parse_uploaded_file
filtered_item_ids = UniUpdates.filter_duplicates(array_of_item_ids)
filtered_item_ids = FilterDuplicateBarcodes.filtered_item_ids(array_of_item_ids)
uniques = filtered_item_ids[0]
duplicates = filtered_item_ids[1].join(' ')
@uni_updates_batch = UniUpdatesBatch.create_withdraw_item_batch(params, uniques.count)
@uni_updates_batch = WithdrawItem.batch_for_transfer_item(params, uniques)
UniUpdates.create_for_batch(uniques, @uni_updates_batch)
WebformsMailer.batch_upload_email(@uni_updates_batch, duplicates).deliver_now
flash[:notice] = 'Batch uploaded!'
Expand Down
6 changes: 6 additions & 0 deletions app/models/change_current_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class ChangeCurrentLocation
attr_accessor :priority
attr_accessor :export_yn
attr_accessor :check_bc_first
attr_accessor :params
attr_accessor :uniques
validates :current_library, :new_current_location, :item_ids, presence: true
validates :email, format: { with: Rails.configuration.email_pattern }, allow_blank: true

def self.batch_for_transfer_item(params, uniques)
UniUpdatesBatch.create_transfer_item_batch(params, uniques.count)
end
end
4 changes: 4 additions & 0 deletions app/models/change_home_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ class ChangeHomeLocation
attr_accessor :check_bc_first
validates :current_library, :new_home_location, :item_ids, presence: true
validates :email, format: { with: Rails.configuration.email_pattern }, allow_blank: true

def self.batch_for_transfer_item(params, uniques)
UniUpdatesBatch.create_transfer_item_batch(params, uniques.count)
end
end
4 changes: 4 additions & 0 deletions app/models/change_item_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ class ChangeItemType
attr_accessor :check_bc_first
validates :current_library, :new_item_type, :item_ids, presence: true
validates :email, format: { with: Rails.configuration.email_pattern }, allow_blank: true

def self.batch_for_transfer_item(params, uniques)
UniUpdatesBatch.create_transfer_item_batch(params, uniques.count)
end
end
4 changes: 4 additions & 0 deletions app/models/transfer_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class TransferItem
validates :current_library, :new_library, :new_homeloc, :item_ids,
presence: true
validates :email, format: { with: Rails.configuration.email_pattern }, allow_blank: true

def self.batch_for_transfer_item(params, uniques)
UniUpdatesBatch.create_transfer_item_batch(params, uniques.count)
end
end
6 changes: 6 additions & 0 deletions app/models/withdraw_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ class WithdrawItem
include ActiveModel::Model
attr_accessor :current_library, :item_ids, :email, :comments, :load_date,
:user_name, :action, :priority, :export_yn, :check_bc_first
attr_accessor :params
attr_accessor :uniques
validates :current_library, :item_ids, presence: true
validates :email, format: { with: Rails.configuration.email_pattern }, allow_blank: true

def self.batch_for_transfer_item(params, uniques)
UniUpdatesBatch.create_transfer_item_batch(params, uniques.count)
end
end
7 changes: 5 additions & 2 deletions spec/controllers/change_current_locations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
end
end
describe 'post#create' do
before do
stub_current_user(FactoryBot.create(:authorized_user))
allow(ChangeCurrentLocation).to receive(:batch_for_transfer_item)
.and_return(FactoryBot.create(:uni_updates_batch))
end
let(:barcode_file) do
extend ActionDispatch::TestProcess
fixture_file_upload('files/test_file.txt', 'text/plain')
end
it 'returns 302 when changing_current_location' do
stub_current_user(FactoryBot.create(:authorized_user))
post :create, change_current_location: { current_library: 'GREEN',
new_current_location: 'SHADOW',
item_ids: barcode_file }
expect(response).to have_http_status(302)
end
it 'renders new template with an invalid object' do
stub_current_user(FactoryBot.create(:authorized_user))
post :create, change_current_location: { current_library: '', new_current_location: '' }
expect(response).to render_template('new')
end
Expand Down
6 changes: 4 additions & 2 deletions spec/controllers/change_home_locations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
end
end
describe 'post#create' do
before do
stub_current_user(FactoryBot.create(:authorized_user))
allow(ChangeHomeLocation).to receive(:batch_for_transfer_item).and_return(FactoryBot.create(:uni_updates_batch))
end
let(:barcode_file) do
extend ActionDispatch::TestProcess
fixture_file_upload('files/test_file.txt', 'text/plain')
end
it 'returns 302 when changing_home_location' do
stub_current_user(FactoryBot.create(:authorized_user))
post :create, change_home_location: { current_library: 'GREEN',
new_home_location: 'STACKS',
item_ids: barcode_file }
expect(response).to have_http_status(302)
end
it 'renders new template with an invalid object' do
stub_current_user(FactoryBot.create(:authorized_user))
post :create, change_current_location: { current_library: '',
new_current_location: '' }
expect(response).to render_template('new')
Expand Down
6 changes: 4 additions & 2 deletions spec/controllers/change_item_types_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
end
end
describe 'post#create' do
before do
stub_current_user(FactoryBot.create(:authorized_user))
allow(ChangeItemType).to receive(:batch_for_transfer_item).and_return(FactoryBot.create(:uni_updates_batch))
end
let(:barcode_file) do
extend ActionDispatch::TestProcess
fixture_file_upload('files/test_file.txt', 'text/plain')
end
it 'returns 302 when changing_item_type' do
stub_current_user(FactoryBot.create(:authorized_user))
post :create, change_item_type: { current_library: 'GREEN', new_item_type: 'UNKNOWN', item_ids: barcode_file }
expect(response).to have_http_status(302)
end
it 'renders new template with an invalid object' do
stub_current_user(FactoryBot.create(:authorized_user))
post :create, change_item_type: { current_library: '', new_item_type: '' }
expect(response).to render_template('new')
end
Expand Down
6 changes: 4 additions & 2 deletions spec/controllers/withdraw_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
end
end
describe 'post#create' do
before do
stub_current_user(FactoryBot.create(:authorized_user))
allow(WithdrawItem).to receive(:batch_for_transfer_item).and_return(FactoryBot.create(:uni_updates_batch))
end
let(:barcode_file) do
extend ActionDispatch::TestProcess
fixture_file_upload('files/test_file.txt', 'text/plain')
end
it 'returns 302 when withdraw_item' do
stub_current_user(FactoryBot.create(:authorized_user))
post :create, withdraw_item: { current_library: 'GREEN', item_ids: barcode_file }
expect(response).to have_http_status(302)
end
it 'renders new template with an invalid object' do
stub_current_user(FactoryBot.create(:authorized_user))
post :create, withdraw_item: { current_library: '' }
expect(response).to render_template('new')
end
Expand Down

0 comments on commit d137ad1

Please sign in to comment.