diff --git a/app/controllers/equipment_models_controller.rb b/app/controllers/equipment_models_controller.rb index 3e0cb59e7..ffa678aa6 100644 --- a/app/controllers/equipment_models_controller.rb +++ b/app/controllers/equipment_models_controller.rb @@ -60,9 +60,9 @@ def show # rubocop:disable AbcSize, MethodLength @restricted = @equipment_model.model_restricted?(cart.reserver_id) # For pending reservations table - @pending = - relevant_reservations.reserved_in_date_range(Time.zone.today, - Time.zone.today + 8.days) + @pending = relevant_reservations.reserved + .overlaps_with_date_range(Time.zone.today, + Time.zone.today + 8.days) # Future reservations using Query object @future = @pending.future end diff --git a/app/decorators/category_decorator.rb b/app/decorators/category_decorator.rb index 0b116f478..33e1f08f2 100644 --- a/app/decorators/category_decorator.rb +++ b/app/decorators/category_decorator.rb @@ -15,9 +15,9 @@ def make_deactivate_btn # find reservations for models in the category in the next week res = 0 object.equipment_models.each do |em| - res += Reservation.for_eq_model(em.id).finalized - .reserved_in_date_range(Time.zone.today - 1.day, - Time.zone.today + 7.days) + res += Reservation.for_eq_model(em.id).active + .overlaps_with_date_range(Time.zone.today - 1.day, + Time.zone.today + 7.days) .count end onclick_str = "handleBigDeactivation(this, #{res}, 'category');" diff --git a/app/decorators/equipment_model_decorator.rb b/app/decorators/equipment_model_decorator.rb index f619adb57..7f489e12d 100644 --- a/app/decorators/equipment_model_decorator.rb +++ b/app/decorators/equipment_model_decorator.rb @@ -13,9 +13,9 @@ class EquipmentModelDecorator < ApplicationDecorator def make_deactivate_btn unless object.deleted_at # find reservations in the next week - res = Reservation.for_eq_model(object.id).finalized - .reserved_in_date_range(Time.zone.today - 1.day, - Time.zone.today + 7.days) + res = Reservation.for_eq_model(object.id).active + .overlaps_with_date_range(Time.zone.today - 1.day, + Time.zone.today + 7.days) .count onclick_str = "handleBigDeactivation(this, #{res}, 'equipment model');" end diff --git a/app/models/cart_validations.rb b/app/models/cart_validations.rb index e4c5a70d3..fe6075a63 100644 --- a/app/models/cart_validations.rb +++ b/app/models/cart_validations.rb @@ -26,7 +26,7 @@ def validate_all(renew = false) # rubocop:disable AbcSize source_res = Reservation.where(equipment_model_id: items.keys) - .reserved_in_date_range(start_date, due_date).all + .overlaps_with_date_range(start_date, due_date).active.all models.each do |model, quantity| errors += check_availability(model, quantity, source_res) @@ -128,8 +128,9 @@ def check_max_cat def check_availability(model = EquipmentModel.find(items.keys.first), quantity = 1, - source_res = Reservation.for_eq_model(id) - .active.all) + source_res = + Reservation.for_eq_model(items.keys.first) + .active.all) # checks that the model is available for the given quantity given the # existence of the source_reservations diff --git a/app/models/equipment_model.rb b/app/models/equipment_model.rb index be4b91965..a132ce05d 100644 --- a/app/models/equipment_model.rb +++ b/app/models/equipment_model.rb @@ -191,8 +191,8 @@ def num_available_from_source(start_date, due_date, source_reservations) def num_available(start_date, due_date) # for if you just want the number available, 1 query to get # relevant reservations - relevant_reservations = Reservation.for_eq_model(id).finalized - .reserved_in_date_range(start_date, due_date) + relevant_reservations = Reservation.for_eq_model(id).active + .overlaps_with_date_range(start_date, due_date) .all num_available_from_source(start_date, due_date, relevant_reservations) end diff --git a/app/models/reservation.rb b/app/models/reservation.rb index 63dac1f9c..06e21aa91 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -79,7 +79,7 @@ class Reservation < ActiveRecord::Base scope :ends_on_days, Reservations::EndsOnDaysQuery scope :future, Reservations::FutureQuery scope :notes_unsent, Reservations::NotesUnsentQuery - scope :reserved_in_date_range, Reservations::ReservedInDateRangeQuery + scope :overlaps_with_date_range, Reservations::OverlapsWithDateRangeQuery scope :reserved_on_date, Reservations::ReservedOnDateQuery scope :starts_on_days, Reservations::StartsOnDaysQuery scope :upcoming, Reservations::UpcomingQuery diff --git a/app/queries/reservations/reserved_in_date_range_query.rb b/app/queries/reservations/overlaps_with_date_range_query.rb similarity index 64% rename from app/queries/reservations/reserved_in_date_range_query.rb rename to app/queries/reservations/overlaps_with_date_range_query.rb index f186742e3..a1f89db87 100644 --- a/app/queries/reservations/reserved_in_date_range_query.rb +++ b/app/queries/reservations/overlaps_with_date_range_query.rb @@ -1,9 +1,8 @@ module Reservations - class ReservedInDateRangeQuery < Reservations::ReservationsQueryBase + class OverlapsWithDateRangeQuery < Reservations::ReservationsQueryBase def call(start_date, end_date) @relation .where('start_date <= ? and due_date >= ?', end_date, start_date) - .reserved end end end diff --git a/spec/models/cart_spec.rb b/spec/models/cart_spec.rb index 21bb884df..2e9fa6a3a 100644 --- a/spec/models/cart_spec.rb +++ b/spec/models/cart_spec.rb @@ -136,4 +136,47 @@ expect { @cart.fix_items }.to change { @cart.items.length }.by(-1) end end + + describe 'check_availability' do + before(:each) do + @em = FactoryGirl.create(:equipment_model) + FactoryGirl.create(:equipment_item, equipment_model: @em) + @cart.add_item(@em) + end + + shared_examples 'validates availability' do |offset| + before do + @start_date = @cart.start_date - offset + @due_date = @cart.due_date + offset + end + + it 'fails if there is a reserved reservation for that model' do + FactoryGirl.build(:reservation, equipment_model: @em, + start_date: @start_date, + due_date: @due_date) + .save(validate: false) + + expect(@cart.check_availability).not_to eq([]) + expect(@cart.validate_all).not_to eq([]) + end + + it 'fails if there is a checked out reservation for that model' do + FactoryGirl.build(:checked_out_reservation, + equipment_model: @em, start_date: @start_date, + due_date: @due_date, + equipment_item: @em.equipment_items.first) + .save(validate: false) + + expect(@cart.check_availability).not_to eq([]) + expect(@cart.validate_all).not_to eq([]) + end + end + + it 'passes if there are equipment items available' do + expect(@cart.check_availability).to eq([]) + end + + it_behaves_like 'validates availability', 0.days + it_behaves_like 'validates availability', 1.day + end end diff --git a/spec/models/reservation_spec.rb b/spec/models/reservation_spec.rb index 21240ff5a..b7808b40a 100644 --- a/spec/models/reservation_spec.rb +++ b/spec/models/reservation_spec.rb @@ -334,27 +334,30 @@ end end - # this all fails - problem w/ available context 'with equipment item available problems' do - let!(:available_reservation) do + before(:each) do FactoryGirl.create(:checked_out_reservation, - equipment_model: reservation.equipment_model) + equipment_model: reservation.equipment_model, + start_date: reservation.start_date, + due_date: reservation.due_date, + equipment_item: reservation.equipment_model + .equipment_items.first) + end + + it { is_expected.not_to be_valid } + it 'should not save' do + expect(reservation.save).to be_falsey + expect(Reservation.all.size).to eq(1) + end + it 'cannot be updated' do + reservation.start_date = Time.zone.today + 1.day + expect(reservation.save).to be_falsey + end + it 'fails appropriate validations' do + expect(reservation.available).not_to eq([]) + expect(reservation).not_to be_valid end - # it { should_not be_valid } #fails - # it 'should not save' do #fails - # reservation.save.should be_falsey - # Reservation.all.size.should == 0 - # end - # it 'cannot be updated' do #fails - # reservation.start_date = Time.zone.today + 1.day - # reservation.save.should be_falsey - # end - # it 'fails appropriate validations' do # fails - # reservation.should_not be_available - # Reservation.validate_set(reservation.reserver, - # [] << reservation).should_not == [] - # end it 'passes other custom validations' do expect(reservation.start_date_before_due_date).to be_nil expect(reservation.not_empty).to be_nil