Comments (1)
after digging in the aasm
codebase I understood a few things and I think I found a way to make it work:
Firstly aasm_fire_event
is not the name of a hook that we can use like after
, after_commit
and others.
So event :run, aasm_fire_event: :do_something_inside_transaction
actually does nothing, the aasm_fire_event
kwarg is ignored.
Iām not sure why the README says:
There are currently 3 transactional callbacks that can be handled on the event, and 2 transactional callbacks for all events.
event before_all_transactions event before_transaction event aasm_fire_event (within transaction) event after_commit (if event successful) event after_transaction event after_all_transactions
I think that the correct hook name to use in my case is (simply) after
, which seems to run after the actual state transition, but still within the transaction.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "aasm"
gem "after_commit_everywhere"
gem "activerecord"
gem "sqlite3"
end
puts "AASM gem installed version: #{AASM::VERSION}"
conn = ActiveRecord::Base.establish_connection adapter: "sqlite3", database: "test.db"
conn.connection.execute "DROP TABLE IF EXISTS jobs;"
ActiveRecord::Migration.class_eval do
create_table :jobs do |t|
t.string :aasm_state
end
end
class Job < ActiveRecord::Base
include AASM
attr_reader :state
aasm do
state :sleeping, initial: true
state :running, :cleaning
event :run, after: :do_something_inside_transaction do
transitions from: :sleeping, to: :running
end
end
def do_something_inside_transaction
puts "in do_something_inside_transaction"
raise StandardError, "raise on purpose"
end
end
begin
job = Job.create!
puts "before, job.state is #{job.aasm_state}"
res = job.run!
puts "after, job state is #{job.reload.aasm_state}"
rescue StandardError => e
puts "rescueing from exception, job state is #{job.reload.aasm_state}"
end
# AASM gem installed version: 5.5.0
# -- create_table(:jobs)
# -> 0.0005s
# before, job.state is sleeping
# in do_something_inside_transaction
# rescueing from exception, job state is sleeping
from aasm.
Related Issues (20)
- Timestamps column name with enum and _prefix HOT 1
- Active Record upgrade 5 to 6 : AASM does not clear the activerecord cache after calling an event HOT 2
- I have a question
- AASM and graphql
- Tag releases v5.3.1, v5.4.0 and v5.5.0 HOT 1
- Invalid error is not using locale setup in the application
- Custom auto-save feature
- Namespaced scopes perform incorrect queries HOT 2
- Multiple state machines per class don't work properly with automatic scopes HOT 1
- Multiple state machines per class don't work properly with automatic state constants HOT 2
- Can't pass an empty hash as callback parameters to `transition_from` since 5.5.0 HOT 2
- callbacks endless loop HOT 2
- Update RSpec helper to reset states with `binding_event`
- Can't pass arguments on events with bangs in Ruby 3.0 HOT 14
- Stopping event without raising from after callback HOT 1
- gem misses CHANGELOG HOT 1
- Performance of aasm_ensure_initial_state (end Rails 3 support?) HOT 1
- Callback with positional and keyword args errors out in Ruby 3.2
- Revisit "Add :after_commit callbacks to events"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aasm.