Skip to content

Instantly share code, notes, and snippets.

@odigity
Created July 20, 2012 22:08
Show Gist options
  • Save odigity/3153600 to your computer and use it in GitHub Desktop.
Save odigity/3153600 to your computer and use it in GitHub Desktop.
Validation false positive on "inclusion in" list of symbols condition
module Ledger
class Base
include Mongoid::Document
field :currency, type: Symbol, default: Money.default_currency.id
validates :currency, presence: true, inclusion: {in: Money::Currency.table.keys}
def currency
Money::Currency.new(self[:currency])
end
def currency=(c)
self[:currency] = Money::Currency.new(c).id
end
end
end
1.9.3-p194 :001 > require 'mcbooks'
=> true
1.9.3-p194 :002 > Mongoid.configure {|c| c.connect_to('mongoid_test')}
=> {:default=>{:database=>"mongoid_test", :hosts=>["localhost:27017"]}}
1.9.3-p194 :003 > a = Ledger::Base.new(name: 'test')
=> #<Ledger::Base _id: 5009d5c7aa197e9078000001, _type: nil, currency: :usd>
1.9.3-p194 :004 > a.save
=> false
1.9.3-p194 :005 > a.errors
=> #<ActiveModel::Errors:0x00000004070068 @base=#<Ledger::Base _id: 5009d5c7aa197e9078000001, _type: nil, currency: :usd>, @messages={:currency=>["is not included in the list"]}>
1.9.3-p194 :006 > Money::Currency.table.keys
=> [:aed, :afn, :all, :amd, :ang, :aoa, :ars, :aud, :awg, :azn, :bam, :bbd, :bdt, :bgn, :bhd, :bif, :bmd, :bnd, :bob, :brl, :bsd, :btn, :bwp, :byr, :bzd, :cad, :cdf, :chf, :clp, :cny, :cop, :crc, :cuc, :cup, :cve, :czk, :djf, :dkk, :dop, :dzd, :egp, :ern, :etb, :eur, :fjd, :fkp, :gbp, :gel, :ghs, :gip, :gmd, :gnf, :gtq, :gyd, :hkd, :hnl, :hrk, :htg, :huf, :idr, :ils, :inr, :iqd, :irr, :isk, :jmd, :jod, :jpy, :kes, :kgs, :khr, :kmf, :kpw, :krw, :kwd, :kyd, :kzt, :lak, :lbp, :lkr, :lrd, :lsl, :ltl, :lvl, :lyd, :mad, :mdl, :mga, :mkd, :mmk, :mnt, :mop, :mro, :mur, :mvr, :mwk, :mxn, :myr, :mzn, :nad, :ngn, :nio, :nok, :npr, :nzd, :omr, :pab, :pen, :pgk, :php, :pkr, :pln, :pyg, :qar, :ron, :rsd, :rub, :rwf, :sar, :sbd, :scr, :sdg, :sek, :sgd, :shp, :skk, :sll, :sos, :srd, :std, :svc, :syp, :szl, :thb, :tjs, :tmm, :tnd, :top, :try, :ttd, :twd, :tzs, :uah, :ugx, :usd, :uyu, :uzs, :vef, :vnd, :vuv, :wst, :xaf, :xcd, :xof, :xpf, :yer, :zar, :zmk, :zwd, :eek, :yen, :ghc]
1.9.3-p194 :007 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment