Created
August 7, 2012 11:43
-
-
Save wallace/3284741 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Order < ActiveRecord::Base | |
| has_many :line_items | |
| validates :total_price, numericality: true | |
| before_create :apply_returning_customer_discount, if: lambda { |order| order.returning_customer? } | |
| def apply_returning_customer_discount | |
| self.total_price = self.total_price * 0.9 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh my... not only is the grammar horrible, but I mixed several different drafts, and some crass language. Please, allow me to try again:
Why use callbacks at all? Rails' notion of callbacks are a hack which allow you to be lazy and not think about the domain; instead delve more deeply into the problem space and work to uncover a better abstraction. In domain language - what the heck is an
Order? To me, anOrderis probably an artifact that only exists after one has finished a purchase. After you've "checked out."Maybe a better abstraction is a
Cartwhich contains a collection ofItems, orSKUs a customer intends to purchase? ACartcould delegate to aPriceRegisteror some form of calculator to sum totals, and then after checkout anOrder, or perhapsReceipt, or maybeBillOfSalecould be stored for later use.