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

Commit

Permalink
Repair equipment_model generator to handle ordering
Browse files Browse the repository at this point in the history
Resolves #1662
- set ordering to number of existing models in equipment_model generation
- add validations for generating multiple objects
- resolves error that can occur when randomly generating reservations for
  equipment_model with max_checkout_length = 1
zeffman authored and Sydney Young committed Feb 11, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 0e87e1a commit a50e6f4
Showing 5 changed files with 23 additions and 11 deletions.
7 changes: 4 additions & 3 deletions db/migrate/20160329015527_add_ordering.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class AddOrdering < ActiveRecord::Migration
def up
unless column_exists?(:equipment_models, :ordering)
add_column :equipment_models, :ordering, :integer, :null => false
end
return if column_exists?(:equipment_models, :ordering)

add_column :equipment_models, :ordering, :integer, :null => false

# store ActiveRecord connection to run queries
conn = ActiveRecord::Base.connection

2 changes: 1 addition & 1 deletion lib/seed/equipment_model_generator.rb
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ def self.generate
em.renewal_days_before_due = rand(0..9001)
em.photo = File.open(IMAGES.sample) unless NO_PICS
em.associated_equipment_models = EquipmentModel.all.sample(6)
em.ordering = 1
em.ordering = EquipmentModel.count
end
end
end
3 changes: 2 additions & 1 deletion lib/seed/reservation_generator_helper.rb
Original file line number Diff line number Diff line change
@@ -8,8 +8,9 @@ def gen_res(random = false)
notes: FFaker::HipsterIpsum.paragraph(2),
start_date: Time.zone.today)
max_checkout_len = r.equipment_model.maximum_checkout_length
last = [max_checkout_len - 1, 1].max
duration = max_checkout_len -
rand_val(first: 1, last: max_checkout_len - 1,
rand_val(first: 1, last: last,
default: 1, random: random)
r.due_date = r.start_date + duration.days
r
2 changes: 1 addition & 1 deletion spec/factories/equipment_models.rb
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
sequence(:unique_id) { |n| n }
sequence(:ordering) { |n| n }
sequence(:ordering) { |n| n + 1 }
factory :equipment_model do
name
description 'This is a model'
20 changes: 15 additions & 5 deletions spec/lib/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -8,25 +8,35 @@
expect(Generator.send(method)).to be_truthy
end
end
shared_examples 'generates multiple valid' do |method, klass|
it method.to_s do
expect(5.times { Generator.send(method) }).to be_truthy
expect(klass.count).to eq(5)
end
end
OBJECTS.each { |o| it_behaves_like 'generates a valid', o }

context 'blackout generation' do
before { Generator.user }
it_behaves_like 'generates a valid', :blackout
it_behaves_like 'generates multiple valid', :blackout, Blackout
end
context 'equipment_model generation' do
before { Generator.category }
it_behaves_like 'generates a valid', :equipment_model
it_behaves_like 'generates multiple valid', :equipment_model,
EquipmentModel
end

context 'requiring a category and equipment model' do
before do
Generator.category
Generator.equipment_model
end
it_behaves_like 'generates a valid', :equipment_item
it_behaves_like 'generates a valid', :checkout_procedure
it_behaves_like 'generates a valid', :checkin_procedure
it_behaves_like 'generates multiple valid', :equipment_item,
EquipmentItem
it_behaves_like 'generates multiple valid', :checkout_procedure,
CheckoutProcedure
it_behaves_like 'generates multiple valid', :checkin_procedure,
CheckinProcedure
end

context 'reservation generation' do

0 comments on commit a50e6f4

Please sign in to comment.