mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
65073e360a
* Add persistence for NoBrainer * Add tests for NoBrainer * Show the ORM version when running the tests * Add a Dockerfile and a docker-compose.yml file allowing to run from anywhere very easily the test suite with `docker-compose run --rm aasm\' * Moves the Mongoid database name definition in the gem loader (avoiding code repetition) * Update the CHANGELOG.md and the README.md files * Add Rails generator for NoBrainer
98 lines
1.9 KiB
Ruby
98 lines
1.9 KiB
Ruby
class ValidatorNoBrainer
|
|
include NoBrainer::Document
|
|
include AASM
|
|
|
|
field :name
|
|
field :status
|
|
|
|
attr_accessor :invalid
|
|
|
|
validate do |_|
|
|
errors.add(:validator, 'invalid') if invalid
|
|
end
|
|
|
|
aasm column: :status, whiny_persistence: true do
|
|
before_all_transactions :before_all_transactions
|
|
after_all_transactions :after_all_transactions
|
|
|
|
state :sleeping, initial: true
|
|
state :running
|
|
state :failed, after_enter: :fail
|
|
|
|
event :run, after_commit: :change_name! do
|
|
transitions to: :running, from: :sleeping
|
|
end
|
|
|
|
event :sleep do
|
|
after_commit do |name|
|
|
change_name_on_sleep name
|
|
end
|
|
transitions to: :sleeping, from: :running
|
|
end
|
|
|
|
event :fail do
|
|
transitions to: :failed, from: %i[sleeping running]
|
|
end
|
|
end
|
|
|
|
validates_presence_of :name
|
|
|
|
def change_name!
|
|
self.name = 'name changed'
|
|
save!
|
|
end
|
|
|
|
def change_name_on_sleep(name)
|
|
self.name = name
|
|
save!
|
|
end
|
|
|
|
def fail
|
|
raise StandardError, 'failed on purpose'
|
|
end
|
|
end
|
|
|
|
class MultipleValidatorNoBrainer
|
|
include NoBrainer::Document
|
|
include AASM
|
|
|
|
field :name
|
|
field :status
|
|
|
|
attr_accessor :invalid
|
|
|
|
aasm :left, column: :status, whiny_persistence: true do
|
|
state :sleeping, initial: true
|
|
state :running
|
|
state :failed, after_enter: :fail
|
|
|
|
event :run, after_commit: :change_name! do
|
|
transitions to: :running, from: :sleeping
|
|
end
|
|
event :sleep do
|
|
after_commit do |name|
|
|
change_name_on_sleep name
|
|
end
|
|
transitions to: :sleeping, from: :running
|
|
end
|
|
event :fail do
|
|
transitions to: :failed, from: %i[sleeping running]
|
|
end
|
|
end
|
|
|
|
validates_presence_of :name
|
|
|
|
def change_name!
|
|
self.name = 'name changed'
|
|
save!
|
|
end
|
|
|
|
def change_name_on_sleep(name)
|
|
self.name = name
|
|
save!
|
|
end
|
|
|
|
def fail
|
|
raise StandardError, 'failed on purpose'
|
|
end
|
|
end
|