acts_as_money 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README +16 -1
  2. data/lib/acts_as_money.rb +2 -1
  3. metadata +3 -3
data/README CHANGED
@@ -1,19 +1,34 @@
1
1
  Money
2
2
  =====
3
3
 
4
- acts_as_money is a fairly trivial plugin that makes it easier to work with the money gem.
4
+ acts_as_money is a fairly trivial plugin that makes it easier to work with the money gem. It's available in Rubygems - just 'gem install acts_as_money'.
5
5
 
6
6
  class Product < ActiveRecord::Base
7
+ acts_as_money
7
8
  money :price
8
9
  end
9
10
 
10
11
  This assumes that there are 2 columns in the database, cents (integer) and currency (string). These fields can be changed by setting the :cents and :currency options. To use the default currency, you can simple set :currency to false
11
12
 
12
13
  class Room < ActiveRecord::Base
14
+ acts_as_money
13
15
  money :rate, :cents => :rate_in_cents, :currency => :rate_currency
14
16
  money :discount, :cents => :discount_in_cents, :currency => false
15
17
  end
16
18
 
19
+ By default, your money field will default to a value of 0. If you require it to default to nil, you may set the :allow_nil option:
20
+
21
+ class Meal < ActiveRecord::Base
22
+ acts_as_money
23
+ money :bill, :allow_nil => true
24
+ end
25
+
26
+ m = Meal.new
27
+ m.bill #returns nil
28
+
29
+ r = Room.new
30
+ r.rate #returns <Money:0x24fd53a6 @currency="USD", @cents=0>
31
+
17
32
  acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.
18
33
 
19
34
  r = Room.new :rate => "100.00"
@@ -18,11 +18,12 @@ module ActsAsMoney #:nodoc:
18
18
  def money(name, options = {})
19
19
  composed_of name, {
20
20
  :class_name => 'Money',
21
+ :allow_nil => options[:allow_nil],
21
22
  :mapping => [
22
23
  [(options[:cents] || :cents).to_s, 'cents'],
23
24
  [(options[:currency] || :currency).to_s, 'currency_as_string']
24
25
  ],
25
- :constructor => lambda {|cents, currency| Money.new(cents || 0, currency || Money.default_currency)}
26
+ :constructor => lambda {|cents, currency| options[:allow_nil] && !cents ? nil : Money.new(cents || 0, currency || Money.default_currency)}
26
27
  }
27
28
  end
28
29
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_money
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tim Cowlishaw