Ruby on Rails - basic state with asmm in ruby on rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
- Usually we'll end up creating models which will contain a state, and that state will be changing during the lifespan of the object.
- AASM is a finite state machine enabler library that can help we out with dealing with having an easy passing through the process design of our objects.
- To install, in Gemfile
gem 'aasm'
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Consider an App where the user Quotes a product for a price.
class Quote
include AASM
aasm do
state :requested, initial: true # User sees a product and requests a quote
state :priced # Seller sets the price
state :payed # Buyer pays the price
state :canceled # The buyer is not willing to pay the price
state :completed # The product has been delivered.
event :price do
transitions from: requested, to: :priced
end
event :pay do
transitions from: :priced, to: :payed, success: :set_payment_date
end
event :complete do
transitions from: :payed, to: :completed, guard: product_delivered?
end
event :cancel do
transitions from: [:requested, :priced], to: :canceled
transitions from: :payed, to: canceled, success: :reverse_charges
end
end
private
def set_payment_date
update payed_at: Time.zone.now
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
The Quote class' states can go however it's best for our process.
We can think of the states as being past, like in the previous example or algo in other tense.
Example
We can think of the states as being past, like in the previous example or algo in other tense, for example: pricing, paying, delivering, etc. The naming of the states depends on us. From a personal point a view, past states work better because your end state will surely be a past action and links up better with the event names, which will be explained below.
NOTE: Be careful what names we use, we have to worry about not using Ruby or Ruby on Rails reserved keywords, like valid, end, being, etc.
Example:
Quote.priced # Shows all Quotes with priced events
quote.priced? # Indicates if that specific quote has been priced
quote.price! # Triggers the event the would transition from requested to priced.
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- As we can see the event has transitions, this transitions determine the way the state will change upon the event call. If the event is invalid due to the current state an Error will be raised.
Example
- The events and transitions also have some other callbacks.
guard: product_delivered?
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Will call the product_delivered? method which will return a boolean. If it turns out false, the transition will not be applied and if the no other transitions are available, the state won't change.
success: :reverse_charges
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- If that translation successfully happens the :reverse_charges method will be invoked.
- There are several other methods in AASM with more callbacks in the process but this will help we creating our first models with finite states.