crowdint / acts_as_shopping_cart

Simple Shopping Cart implementation, Official repo: https://github.com/dabit/acts_as_shopping_cart
MIT License
257 stars 88 forks source link

Due to monkey patching `empty?` validates presence no longer works. #40

Open krainboltgreene opened 8 years ago

krainboltgreene commented 8 years ago
class Redemption < ActiveRecord::Base
  belongs_to :shopping_cart

  validates :shopping_cart, presence: true
end

cart = ShoppingCart.create(...)
redemption = Redemption.new(shopping_cart: cart)
redemption.valid?
redemption.errors # => #<ActiveModel::Errors:... @base=#<Redemption id: nil, shopping_cart_id: ..., created_at: nil, updated_at: nil>, @messages={:shopping_cart=>["can't be blank"]}>

This was pretty annoying to run into :/

krainboltgreene commented 8 years ago

Like, I get why you might want this, but it make testing just that much harder since I now have to create an entire cart object graph to satisfy a single monkey patch.

kriskhaira commented 8 years ago

This annoyed me simply because I was trying to check if a user had a cart through user.cart.present?. The user had a cart but present? was returning false with this gem because the cart was empty.

kriskhaira commented 8 years ago

@krainboltgreene You can use my organization's fork for now if you prefer. Nothing's changed except empty? removed from the Collection module.

dabit commented 8 years ago

Crap! you are correct. I have removed the empty? method in favor of has_items? and has_no_items?.

This change will be included in its next release, but the official repo of this gem is over here: https://github.com/dabit/acts_as_shopping_cart