Use RubyMoney with mongoid
Ruby
Latest commit 1901bb3 May 16, 2017 @glebtv bump money dependency
Permalink
Failed to load latest commit information.
lib bump money dependency May 16, 2017
spec bump May 16, 2017
.document chmod Nov 6, 2013
.gitignore fix specs Jan 10, 2014
.rspec chmod Nov 6, 2013
.travis.yml drop mongoid-git Jul 7, 2015
Gemfile bump May 16, 2017
Gemfile.lock bump money dependency May 16, 2017
LICENSE.txt chmod Nov 6, 2013
README.rdoc Update README.rdoc Sep 18, 2014
Rakefile chmod Nov 6, 2013
mongoid_money_field.gemspec bump money dependency May 16, 2017

README.rdoc

mongoid money field

Build Status Dependency
Status

This is a super simple gem to use RubyMoney money type columns with mongoid

github.com/RubyMoney/money

github.com/mongoid/mongoid

Description

A simple gem that creates a Money datatype using RubyMoney for Mongoid.

Inspired by gist.github.com/840500

Alternatives

github.com/RubyMoney/money-rails

Since v4 storage format of mongoid money field is identical to that of money-rails

Changelog

v5 drops compatibility with v3 storage format. Please use v4 until you upgraded your DB.

v4 is a massive refactor. Storage format is now a hash, like money-rails, instead of two separate cents and currency fields Old values should be still accessible after you update. To migrate to new storage format do:

YourModel.migrate_from_money_field_3!(your_money_fields)

v3 has a minor break of backwards compatibility in favour of having syntax and defaults similar to field mongoid macro

To get the same behaviour as 1.0 please add “default: 0” to field declaration, like this

money_field :price, default: 0

please avoid using v2, i realized that API was stupid after commiting it.

Installation

Include the gem in your Gemfile

gem 'mongoid_money_field', '~> 5.2.1'

Usage

class DummyMoney
  include Mongoid::Document
  include Mongoid::MoneyField

  field :description

  # defaults to 0
  money_field :cart_total, default: 0

  # defaults to nil
  money_field :price, :old_price, default: nil # defaults to nil

  # to disallow changing currency (and to not store it in database)
  money_field :price2, fixed_currency: 'GBP'

  # set a default
  money_field :price3, default: '1.23 RUB'

  # make required
  money_field :price4, required: true, default_currency: nil

  # default_currency is Money.default_currency.iso_code if not specified
  # fixed_currency overrides default_currency if set

  # field can be validated as numeric value (with localized separators)
  validates_numericality_of :price, greater_than: 0
end

simple_form_for do |f|
  f.input :price, as: :money
end

All Money values are converted and stored in mongo as cents and currency in two separate fields.

Finding by price

DummyMoney.where(price_cents: 123).first

Copyright

Copyright © 2012-2013 glebtv (rocketscience.pro). MIT License.