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

Fundamental Rewrite of Validation System #644

Merged
merged 47 commits into from
Jul 10, 2014
Merged

Conversation

squidgetx
Copy link
Contributor

  • Moving all 'soft' validations to cart validations instead of reservation validations. This enables the cart to run most validations only one time instead of for every equipment model in the cart (available, renewable, duration, max cat count, max model count, reserver has overdue, blackout dates)
  • Reservation.validateSet is no more
  • Only necessary validations run when the dates are updated and when items are added/removed (doesn't check for blackout dates when adding a new item to cart, for instance)

@squidgetx
Copy link
Contributor Author

This is almost done. Still need to go over the ReservationsController spec

# Checks that reservation is not in the past
# Does not run on checked out, checked in, overdue, or missed Reservations
def not_in_past
unless due_date >= Date.today
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the start date?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I was thinking that a reservation with a start date in the past and due date in the future is still 'active', but not_in_past runs only on 'create' and should check the start date as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it shouldn't matter since start_date_before_due_date would trigger in the case that the due date is before Date.today and the start_date wasn't. Since in that case its not clear whether or not that reservation was intended to be made in the past or not it would be clearer to leave out not_in_past.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right! Good catch yourself :-)

def get_items
# Used in cart_validations
# Return items where the key is the full equipment model object
# uses 1 database call
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like that you've been documenting when you're making DB calls, let's try to make that the case across our codebase. It will not only be good for documentation purposes but could also potentially uncover some other inefficiencies.

@orenyk
Copy link
Contributor

orenyk commented Jul 10, 2014

I'm getting the spec failures on my local build (freshly checked out the 573_validations_lol branch. Are you sure you were in the right branch?

EDIT: I agree with @shippy, this work is fantastic :-)

  22) ApplicationController PUT update_cart valid parameters should update cart dates
     Failure/Error: put :update_cart, cart: {start_date_cart: new_start.strftime('%m/%d/%Y'), due_date_cart: new_end.strftime('%m/%d/%Y')}, reserver_id: @new_reserver.id
     ArgumentError:
       comparison of Fixnum with nil failed
     # ./app/models/cart_validations.rb:91:in `<'
     # ./app/models/cart_validations.rb:91:in `block in validate_dates_and_items'
     # ./app/models/cart_validations.rb:88:in `each'
     # ./app/models/cart_validations.rb:88:in `validate_dates_and_items'
     # ./app/controllers/application_controller.rb:133:in `update_cart'
     # ./spec/controllers/application_controller_spec.rb:267:in `block (4 levels) in <top (required)>'

  23) ApplicationController PUT update_cart invalid parameters should set the flash
     Failure/Error: put :update_cart, cart: {start_date_cart: new_start.strftime('%m/%d/%Y'), due_date_cart: new_end.strftime('%m/%d/%Y')}, reserver_id: @new_reserver.id
     ArgumentError:
       comparison of Fixnum with nil failed
     # ./app/models/cart_validations.rb:91:in `<'
     # ./app/models/cart_validations.rb:91:in `block in validate_dates_and_items'
     # ./app/models/cart_validations.rb:88:in `each'
     # ./app/models/cart_validations.rb:88:in `validate_dates_and_items'
     # ./app/controllers/application_controller.rb:133:in `update_cart'
     # ./spec/controllers/application_controller_spec.rb:284:in `block (4 levels) in <top (required)>'

  24) CatalogController PUT remove_from_cart valid equipment_model selected 
     Failure/Error: put :add_to_cart, id: @equipment_model.id
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/cart_validations.rb:51:in `+'
     # ./app/models/cart_validations.rb:51:in `block (2 levels) in validate_items'
     # ./app/models/cart_validations.rb:50:in `upto'
     # ./app/models/cart_validations.rb:50:in `block in validate_items'
     # ./app/models/cart_validations.rb:48:in `each'
     # ./app/models/cart_validations.rb:48:in `validate_items'
     # ./app/controllers/catalog_controller.rb:59:in `change_cart'
     # ./app/controllers/catalog_controller.rb:25:in `add_to_cart'
     # ./spec/controllers/catalog_controller_spec.rb:58:in `block (4 levels) in <top (required)>'

  25) CatalogController PUT remove_from_cart valid equipment_model selected should call cart.remove_item to remove item from cart
     Failure/Error: put :add_to_cart, id: @equipment_model.id
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/cart_validations.rb:51:in `+'
     # ./app/models/cart_validations.rb:51:in `block (2 levels) in validate_items'
     # ./app/models/cart_validations.rb:50:in `upto'
     # ./app/models/cart_validations.rb:50:in `block in validate_items'
     # ./app/models/cart_validations.rb:48:in `each'
     # ./app/models/cart_validations.rb:48:in `validate_items'
     # ./app/controllers/catalog_controller.rb:59:in `change_cart'
     # ./app/controllers/catalog_controller.rb:25:in `add_to_cart'
     # ./spec/controllers/catalog_controller_spec.rb:58:in `block (4 levels) in <top (required)>'

  26) CatalogController PUT remove_from_cart valid equipment_model selected should set flash[:error] to the result of Reservation.validate_set if exists
     Failure/Error: put :add_to_cart, id: @equipment_model.id
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/cart_validations.rb:51:in `+'
     # ./app/models/cart_validations.rb:51:in `block (2 levels) in validate_items'
     # ./app/models/cart_validations.rb:50:in `upto'
     # ./app/models/cart_validations.rb:50:in `block in validate_items'
     # ./app/models/cart_validations.rb:48:in `each'
     # ./app/models/cart_validations.rb:48:in `validate_items'
     # ./app/controllers/catalog_controller.rb:59:in `change_cart'
     # ./app/controllers/catalog_controller.rb:25:in `add_to_cart'
     # ./spec/controllers/catalog_controller_spec.rb:58:in `block (4 levels) in <top (required)>'

  27) CatalogController PUT remove_from_cart valid equipment_model selected 
     Failure/Error: put :add_to_cart, id: @equipment_model.id
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/cart_validations.rb:51:in `+'
     # ./app/models/cart_validations.rb:51:in `block (2 levels) in validate_items'
     # ./app/models/cart_validations.rb:50:in `upto'
     # ./app/models/cart_validations.rb:50:in `block in validate_items'
     # ./app/models/cart_validations.rb:48:in `each'
     # ./app/models/cart_validations.rb:48:in `validate_items'
     # ./app/controllers/catalog_controller.rb:59:in `change_cart'
     # ./app/controllers/catalog_controller.rb:25:in `add_to_cart'
     # ./spec/controllers/catalog_controller_spec.rb:58:in `block (4 levels) in <top (required)>'

  28) CatalogController PUT add_to_cart valid equipment_model selected 
     Failure/Error: put :add_to_cart, id: @equipment_model.id
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/cart_validations.rb:51:in `+'
     # ./app/models/cart_validations.rb:51:in `block (2 levels) in validate_items'
     # ./app/models/cart_validations.rb:50:in `upto'
     # ./app/models/cart_validations.rb:50:in `block in validate_items'
     # ./app/models/cart_validations.rb:48:in `each'
     # ./app/models/cart_validations.rb:48:in `validate_items'
     # ./app/controllers/catalog_controller.rb:59:in `change_cart'
     # ./app/controllers/catalog_controller.rb:25:in `add_to_cart'
     # ./spec/controllers/catalog_controller_spec.rb:28:in `block (4 levels) in <top (required)>'

  29) CatalogController PUT add_to_cart valid equipment_model selected should call cart.add_item to add item to cart
     Failure/Error: put :add_to_cart, id: @equipment_model.id
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/cart_validations.rb:51:in `+'
     # ./app/models/cart_validations.rb:51:in `block (2 levels) in validate_items'
     # ./app/models/cart_validations.rb:50:in `upto'
     # ./app/models/cart_validations.rb:50:in `block in validate_items'
     # ./app/models/cart_validations.rb:48:in `each'
     # ./app/models/cart_validations.rb:48:in `validate_items'
     # ./app/controllers/catalog_controller.rb:59:in `change_cart'
     # ./app/controllers/catalog_controller.rb:25:in `add_to_cart'
     # ./spec/controllers/catalog_controller_spec.rb:28:in `block (4 levels) in <top (required)>'

  30) CatalogController PUT add_to_cart valid equipment_model selected should set flash[:error] to the result of Reservation.validate_set if exists
     Failure/Error: put :add_to_cart, id: @equipment_model.id
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/cart_validations.rb:51:in `+'
     # ./app/models/cart_validations.rb:51:in `block (2 levels) in validate_items'
     # ./app/models/cart_validations.rb:50:in `upto'
     # ./app/models/cart_validations.rb:50:in `block in validate_items'
     # ./app/models/cart_validations.rb:48:in `each'
     # ./app/models/cart_validations.rb:48:in `validate_items'
     # ./app/controllers/catalog_controller.rb:59:in `change_cart'
     # ./app/controllers/catalog_controller.rb:25:in `add_to_cart'
     # ./spec/controllers/catalog_controller_spec.rb:28:in `block (4 levels) in <top (required)>'

  31) CatalogController PUT add_to_cart valid equipment_model selected 
     Failure/Error: put :add_to_cart, id: @equipment_model.id
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/cart_validations.rb:51:in `+'
     # ./app/models/cart_validations.rb:51:in `block (2 levels) in validate_items'
     # ./app/models/cart_validations.rb:50:in `upto'
     # ./app/models/cart_validations.rb:50:in `block in validate_items'
     # ./app/models/cart_validations.rb:48:in `each'
     # ./app/models/cart_validations.rb:48:in `validate_items'
     # ./app/controllers/catalog_controller.rb:59:in `change_cart'
     # ./app/controllers/catalog_controller.rb:25:in `add_to_cart'
     # ./spec/controllers/catalog_controller_spec.rb:28:in `block (4 levels) in <top (required)>'

@squidgetx
Copy link
Contributor Author

screenshot - 07102014 - 01 57 25 pm

WHATTTTT

@orenyk
Copy link
Contributor

orenyk commented Jul 10, 2014

WOO HOO! Great job!

return false if reqs.include?(em_req)
end
return true
#binding.pry
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please delete this, even though it's commented out. :-P

# Checks that reservation is not in the past
# Does not run on checked out, checked in, overdue, or missed Reservations
def not_in_past
if due_date < Date.today
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to also check for start_date < Date.today?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the discussion on oren's first diff comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One still shouldn't be able to create a reservation with a start_date in the past.

squidgetx added a commit that referenced this pull request Jul 10, 2014
Fundamental Rewrite of Validation System
@squidgetx squidgetx merged commit 9b3674c into development Jul 10, 2014
@orenyk orenyk added this to the 3.4.0 milestone Jul 11, 2014
@squidgetx squidgetx deleted the 573_validations_lol branch July 22, 2014 16:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants