Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Remove composed_of example in README #179

Merged
merged 1 commit into from

3 participants

Hakan Ensari Don't Add Me To Your Organization a.k.a The Travis Bot Shane Emmons
Hakan Ensari

This replaces the composed_of example in the README with accessor definitions that accomplish the same task.

rails/rails#6743

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged a66ddfa into 4facf60).

Shane Emmons semmons99 merged commit 2178526 into from
Shane Emmons
Owner

Awesome. Merged!

Shane Emmons
Owner

@hakanensari would you update the wiki with this information too?

Hakan Ensari

@semmons99 no worries, I've done that already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 24, 2012
  1. Hakan Ensari
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 20 deletions.
  1. +12 −20 README.md
32 README.md
View
@@ -231,36 +231,28 @@ implementations.
## Ruby on Rails
-To integrate money in a rails application use [money-rails](http://github.com/RubyMoney/money-rails)
+To integrate money in a Rails application use [money-rails](http://github.com/RubyMoney/money-rails)
gem or follow the instructions below.
-Use the `composed_of` helper to let Active Record deal with embedding the money
+Define accessor methods to let Active Record deal with embedding the money
object in your models. The following example requires 2 columns:
``` ruby
:price_cents, :integer, :default => 0, :null => false
-:currency, :string
+:price_currency, :string
```
Then in your model file:
``` ruby
-composed_of :price,
- :class_name => "Money",
- :mapping => [%w(price_cents cents), %w(currency currency_as_string)],
- :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
- :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
-```
-
-For Money 2.2.x and previous versions, simply use the following `composed_of`
-definition:
+def price
+ Money.new price_cents || 0, price_currency || Money.default_currency
+end
-``` ruby
-composed_of :price,
- :class_name => "Money",
- :mapping => [%w(price_cents cents), %w(currency currency)],
- :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }
+def price=(value)
+ Money.parse(value).tap do |price|
+ write_attribute :price_cents, price.cents
+ write_attribute :price_currency, price.currency_as_string
+ end
+end
```
-
-Note the difference in the currency column mapping (currency_as_string vs. currency) - this matters! For further details read the full discussion
-[here](http://github.com/RubyMoney/money/issues/4#comment_224880).
Something went wrong with that request. Please try again.