ar validations

1 view
Skip to first unread message

Charly

unread,
Aug 22, 2009, 6:03:59 AM8/22/09
to state-fu
hi,
i played around with aasm and state_machine and really love state-fu
dsl most.
But i didn't succed in having it work well with ar validation.
I have a machine on an invoice model :

validates_format_of :number, :with => /^\d{4}-\d{5}/, :if
=> :completed?
....
state "draft" do
event :complete, :to => :completed
end
state "completed" do
requires :valid?
event :email_it, :to => :emailed
end
state "emailed" do
accepted :payment_to_be_confirmed
end

but this spec says : name should not have changed, but did change
from :draft to :completed

it "fails transitioning to completed if invalid" do
lambda do
@invoice.status.complete!
end.should_not change(@invoice.status, :name)
end

and requires doesn't seem of any use.
I'm confused... Could someone tell me what's the right way to do it.
Thanks

David Lee

unread,
Aug 23, 2009, 11:31:01 PM8/23/09
to state-fu
Hi Charly,

Thanks for taking the time to post.

From what I can see, it appears the issue is that you expected the
method #completed? to be defined, and to evaluate to true when the
current_state_name is :completed.

This *is* the case in the experimental branch of StateFu (not yet
available for public consumption, but should be in the next few days);
however currently query methods are not defined for states, only
events; as #completed? does not exist, :if => :completed? is
evaluating to false and your validation is never taking effect (as far
as i can tell). Try adding the following method:

def completed?
state_fu.current_state == :completed
end

And let me know if this helps.

In the next release of StateFu, #completed? will be defined for you
and this should work out of the box.

best regards,
David Lee

David Lee

unread,
Aug 23, 2009, 11:47:51 PM8/23/09
to state-fu
Hi again,

I've just pushed version 0.9 of StateFu to edge - you might like to try it and see if it's an improvement for you.

best regards,
David
--
cheers,
David Lee

Charly

unread,
Aug 26, 2009, 11:05:13 AM8/26/09
to state-fu
Hi David,

Thanks for your response.
Actually the completed? method was defined on invoice with this little
hack in the invoice model (i should 've said it) :

machines[:status].states.each do |state|
#creates boolean methods from states e.g: paid?
define_method("#{state.name}?".to_sym) do
self.status.name == state.name
end
named_scope state.name, :conditions => "#{table_name}.state = '#
{state.name}'"
end

meanwhile what I did is, i removed the "states { accepted
{ object.save } }", and the require :valid? and relied only on ar
save.
That way i had my errors display if the state was in completed mode.
But that means the invoice IS in a completed state although it cannot
save!

Ideally i'd like to have the validation to trigger only if in a
defined state, and if invalid, have it rollback to the origin state.
thanks again and tell me if ever you have the answer to the above
question, while i rush to 0.9

charly
Reply all
Reply to author
Forward
0 new messages