-
-
Save dimasusername/1f3550d2f99a04959346380ec189513c to your computer and use it in GitHub Desktop.
Money represented with the Money gem, persistence using Mongoid
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 Product | |
| include Mongoid::Document | |
| include Mongoid::Timestamps | |
| field :description, :type => String | |
| field :price_pence, :type => Integer, :default => 0 | |
| field :currency_iso, :type => String, :default => Money.default_currency.iso_code | |
| validates_presence_of :description | |
| validates_numericality_of :price_pence | |
| # Virtual price / currency attributes | |
| def price | |
| Money.new(self.price_pence, currency) | |
| end | |
| def price=(price) | |
| self.price_pence = price.cents | |
| end | |
| def currency | |
| Money::Currency.new(currency_iso) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment