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

[1449] Fix cart availability validation [master] #1452

Merged
merged 1 commit into from
Feb 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/controllers/equipment_models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/decorators/category_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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');"
Expand Down
6 changes: 3 additions & 3 deletions app/decorators/equipment_model_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions app/models/cart_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/equipment_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/reservation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions spec/models/cart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 20 additions & 17 deletions spec/models/reservation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down