New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

has_money_fields

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

has_money_fields

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Build Status

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

FAQs

Package last updated on 20 Jan 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc