RubyMoney/money — Issue #112: validates_presence_of with Shoulda Matchers
Author: johnrees
Created: 2011-10-22 12:46:19+00:00
State: open

Failure/Error: it {should validate_presence_of(:total_cost)}
ArgumentError:
Can't convert NilClass to Money
# ./app/models/line_item.rb:20:in `block in '
# ./spec/models/line_item_spec.rb:9:in `block (2 levels) in '

I understand that the problem is probably being caused because of the composed_of statement and it not being an actual field. What is the proper way to go about validating presence, am I supposed to validate on the *_cents fields?

composed_of :total_cost,
:class_name => "Money",
:mapping => [%w(total_cost_cents cents), %w(GBP 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") }

validates_presence_of :total_cost