Say am writing a function that will add a product to the shopping cart.
I have a cart.rb model, and the method signature looks like:
def self.add_product(store, user, product, quantity, ...)
# store.id == product.store_id
# quantity > 0 ?
# product is active?
# if product is in cart, update quantity
end
So I have to pass in around 4 other models, and then to some sanity checks also.
So if store.id != product.store_id, I want to return with some kind of an error or status saying the product doesn’t belong to this store so I can’t continue.
If the quanitity is 0, I want to tell the user the quantity has to be > 0.
etc.
Where should all this logic be? There are many other models involved so I’m very confused.
Should I use the vote error collection? Or pass back status codes?
What is the rails way? Please clarify.
Thanks!
To elaborate on my comment above, here’s how your
CartandCartItemclasses might look/work.