Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Use RubyMoney with mongoid
Ruby
Failed to load latest commit information.
lib drop mongoid 3
spec ruby 2.2.0 / new rspec / new money gem updates
.document chmod
.gitignore fix specs
.rspec chmod
.travis.yml drop mongoid-git
Gemfile
Gemfile.lock drop mongoid-git
LICENSE.txt
README.rdoc Update README.rdoc
Rakefile chmod
mongoid_money_field.gemspec drop mongoid 3

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.

Something went wrong with that request. Please try again.