ruby - How do I transfer from using state_machine to assm? Rails App -
i have code written using state_machine gem how make equivalent code using assm gem?
state_machine :state, initial: :pending after_transition on: :accept, do: :send_acceptance_email state :requested event :accept transition => :accepted end end
this whats done after accepting friendship request
def self.request(user1, user2) transaction friendship1 = create!(user: user1, friend: user2, state: 'pending') friendship2 = create!(user: user1, friend: user2, state: 'requested') friendship1.send_request_email friendship1 end end def send_request_email usernotifier.friend_requested(id).deliver end def send_acceptance_email usernotifier.friend_accepted(id).deliver end
user.rb
has_many :friends, through: :user_friendships, conditions: { user_friendships: { state: 'accepted' } } has_many :pending_user_friendships, class_name: 'userfriendship', foreign_key: :user_id, conditions: { state: 'pending' } has_many :pending_friends, through: :pending_user_friendships, source: :friend
you need include line in model
include aasm
#though used status column state machine, can try state column using. hope wont issue.
aasm column: 'state', initial: :pending, whiny_transitions: true state :requested # im not seeing event set state state :accepted #if want trigger request event state add event :request, after: proc.new { send_request_email } transitions to: :requested end #if want trigger accepted state state event :accept, after: proc.new { send_acceptance_email } transitions to: :accepted end #if want trigger accepted state requeste state event :accept, after: proc.new { send_acceptance_email } transitions from: :requested, to: :accepted end end
if don't exceptions , prefer simple true or false response, tell aasm not whiny:
:whiny_transitions => false
reference: https://github.com/aasm/aasm
Comments
Post a Comment