Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aasm after callback with argument

I'm using the aasm (formerly acts_as_state_machine) gem in my rails 4 application. I have something like this on my Post model

  ...
  aasm column: :state do
    state :pending_approval, initial: true
    state :active
    state :pending_removal

    event :accept_approval, :after => Proc.new { |user| binding.pry } do
      transitions from: :pending_approval, to: :active
    end
  end
  ...

When I call @post.accept_approval!(:active, current_user) and the after callback gets triggered, in my console I can inspect what user is (that was passed into the Proc) and it's nil!

What's going on here? What is the correct way to call this transition?

like image 857
Kyle Decot Avatar asked Jan 28 '14 20:01

Kyle Decot


2 Answers

Look aasm docs in section callbacks.

...
  aasm column: :state do
    state :pending_approval, initial: true
    state :active
    state :pending_removal

    after_all_transition :log_all_events

    event :accept_approval, after: :log_approval do
      transitions from: :pending_approval, to: :active
    end
  end
  ...
  del log_all_events(user)
    logger.debug "aasm #{aasm.current_event} from #{user}"
  end

  def log_approval(user)
    logger.debug "aasm log_aproove from #{user}"
  end

You can call events with needed params:

  @post.accept_approval! current_user
like image 50
user1810122 Avatar answered Oct 03 '22 04:10

user1810122


It works in the current version (4.3.0):

event :finish do
  before do |user|
    # do something with user
  end

  transitions from: :active, to: :finished
end
like image 20
Lucas Caton Avatar answered Oct 03 '22 06:10

Lucas Caton