mongoid money field¶ ↑
<img src=“https://secure.travis-ci.org/glebtv/mongoid_money_field.png” alt=“Build Status” />
This is a super simple gem to use RubyMoney money type columns with 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.