The following Q&A is based on the examples given in the Trailblazer book pp. ~50-60 as adapted for my specific requirements. You can simply think of ARInvoice
and ar_invoice
as Thing
and thing
to get the general drift if following in the book.
My operation.rb
file for this was:
class ARInvoice < GLTransaction class Create <
Trailblazer::Operation
include( Model )
model( ARInvoice, :create )
contract() do
property( :invoice_number )
property( :currency_code )
property( :forex_rate )
property( :gl_account_id )
validates( :invoice_number, :presence => true )
validates( :currency_code, :presence => true )
end
def process( params )
@model = ARInvoice.new # have to use instance variable here
validate( params[ :ar_invoice ], @model ) do |f|
f.save
end
end
end
end
I adapted this test from the trailblazer book:
it("INSERTs a valid invoice") do
test_time = Time.now
puts(test_time)
ar_invoice = ARInvoice::Create.(
:ar_invoice => {
:invoice_number => 101,
:gl_account_id => 1,
:effective_from => test_time.to_s
}
).model
ar_invoice.persisted?.must_equal(true)
ar_invoice.invoice_number.must_equal(101)
ar_invoice.transaction_type.must_equal('IN')
ar_invoice.effective_from.must_equal(test_time)
ar_invoice.superseded_after.must_equal(nil)
end
And I got this error:
ar_invoice crud Create#test_0001_INSERTs a valid invoice: \
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: \
gl_transactions.effective_from may not be NULL: \
INSERT INTO "gl_transactions" . . .
But I also see this:
# Running:
2016-02-08 11:24:35 -0500
E
So, test_time value is set. Why is it not getting into the effective_from
attribute?
The answer I give below.