Aasm after callback with argument

I use aasm (previously acts_as_state_machine) gem in my rails 4 app. I have something like this on my modelPost

  ...
  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 after the callback fires, in my console I can check that user(which was passed to Proc) and it is nil!

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

+4
source share
3 answers

See the aasm docs in the callbacks section.

...
  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 trigger events with the necessary parameters:

  @post.accept_approval! current_user
+3

(4.3.0):

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

  transitions from: :active, to: :finished
end
+1
event :accept_approval do
  transitions from: :pending_approval, to: :active
end

post.accept_approval!{post.set_approvaler(current_user)}

the block to bang method will be called after a successful transition, if any activerecord operation is completed in the transition transaction, you may need a lock to prevent the concurrency problem with the option requires_lock: true.

0
source

Source: https://habr.com/ru/post/1524158/


All Articles