Skip to content

okeen/has_money_fields

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

has_money_fields is a try to DRY the following code from the Money gem documentation

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") }

It turns that code it into a smal gem that you can use yo provide currency capabilities to fields on your ActiveRecord models.

Usage

You can choose whether to store the currency with you "money" or to leave it to the default you've configured for the gem. For an example Product model:

You'll need a migration like this:

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.integer :money_price, :default => 0
      t.string :currency_price

      t.string :name, :null => false
    end
  end

  def self.down
    drop_table :products
  end
end

And a model like that:

class Product < ActiveRecord::Base
  has_money_fields :price
end

money_price will store the amount and 'currency_price' will store the actual currency, so, at the end your product will have a price field that you can use like that:

p = Product.new :price => Money.new(1000, "EUR"), :name => "T-shirt"

p.price # => #<Money cents:1000 currency:EUR>
p.price.format # => "10.00 €"
p.price.cents # => 1000
p.price.currency_as_string # => "€"

To use the default currency from the money gem, you'd rather drop the currency_price field from the migration and use has_money_fields like that:

class Product < ActiveRecord::Base
  has_money_fields :price, :only_cents => true
end

Validations

You can use ActiveRecord validations, as the following example:

class Product < ActiveRecord::Base
  has_money_fields :price
  validates :price, :presence => true
end

product = Product.new :price => nil
product.save # => false
product.valid? # => false

TODO

  • Make dirty attributes work

About

It makes the use of Money with AR easier

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%