Giter Site home page Giter Site logo

has_money_fields's Introduction

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

has_money_fields's People

Contributors

okeen avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.