aasm 4.11.1 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/ISSUE_TEMPLATE/bug_report.md +27 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- data/.travis.yml +56 -23
- data/Appraisals +67 -0
- data/CHANGELOG.md +112 -0
- data/CONTRIBUTING.md +24 -0
- data/Dockerfile +44 -0
- data/Gemfile +3 -21
- data/Gemfile.lock_old +151 -0
- data/LICENSE +1 -1
- data/README.md +540 -139
- data/Rakefile +6 -1
- data/TESTING.md +25 -0
- data/aasm.gemspec +5 -0
- data/docker-compose.yml +40 -0
- data/gemfiles/norails.gemfile +10 -0
- data/gemfiles/rails_4.2.gemfile +13 -11
- data/gemfiles/rails_4.2_mongoid_5.gemfile +8 -11
- data/gemfiles/rails_4.2_nobrainer.gemfile +9 -0
- data/gemfiles/rails_5.0.gemfile +11 -18
- data/gemfiles/rails_5.0_nobrainer.gemfile +9 -0
- data/gemfiles/rails_5.1.gemfile +14 -0
- data/gemfiles/rails_5.2.gemfile +14 -0
- data/lib/aasm/aasm.rb +40 -29
- data/lib/aasm/base.rb +61 -11
- data/lib/aasm/configuration.rb +10 -0
- data/lib/aasm/core/event.rb +45 -37
- data/lib/aasm/core/invoker.rb +129 -0
- data/lib/aasm/core/invokers/base_invoker.rb +75 -0
- data/lib/aasm/core/invokers/class_invoker.rb +52 -0
- data/lib/aasm/core/invokers/literal_invoker.rb +47 -0
- data/lib/aasm/core/invokers/proc_invoker.rb +59 -0
- data/lib/aasm/core/state.rb +22 -13
- data/lib/aasm/core/transition.rb +17 -69
- data/lib/aasm/dsl_helper.rb +24 -22
- data/lib/aasm/errors.rb +4 -6
- data/lib/aasm/instance_base.rb +22 -4
- data/lib/aasm/localizer.rb +13 -3
- data/lib/aasm/minitest/allow_event.rb +13 -0
- data/lib/aasm/minitest/allow_transition_to.rb +13 -0
- data/lib/aasm/minitest/have_state.rb +13 -0
- data/lib/aasm/minitest/transition_from.rb +21 -0
- data/lib/aasm/minitest.rb +5 -0
- data/lib/aasm/minitest_spec.rb +15 -0
- data/lib/aasm/persistence/active_record_persistence.rb +49 -105
- data/lib/aasm/persistence/base.rb +20 -5
- data/lib/aasm/persistence/core_data_query_persistence.rb +2 -1
- data/lib/aasm/persistence/dynamoid_persistence.rb +1 -1
- data/lib/aasm/persistence/mongoid_persistence.rb +26 -32
- data/lib/aasm/persistence/no_brainer_persistence.rb +105 -0
- data/lib/aasm/persistence/orm.rb +154 -0
- data/lib/aasm/persistence/plain_persistence.rb +2 -1
- data/lib/aasm/persistence/redis_persistence.rb +16 -11
- data/lib/aasm/persistence/sequel_persistence.rb +36 -64
- data/lib/aasm/persistence.rb +3 -3
- data/lib/aasm/rspec/allow_event.rb +5 -1
- data/lib/aasm/rspec/allow_transition_to.rb +5 -1
- data/lib/aasm/rspec/transition_from.rb +5 -1
- data/lib/aasm/state_machine.rb +4 -2
- data/lib/aasm/state_machine_store.rb +5 -2
- data/lib/aasm/version.rb +1 -1
- data/lib/aasm.rb +5 -2
- data/lib/generators/aasm/orm_helpers.rb +6 -0
- data/lib/generators/active_record/aasm_generator.rb +3 -1
- data/lib/generators/active_record/templates/migration.rb +1 -1
- data/lib/generators/active_record/templates/migration_existing.rb +1 -1
- data/lib/generators/nobrainer/aasm_generator.rb +28 -0
- data/lib/motion-aasm.rb +3 -1
- data/spec/database.rb +20 -7
- data/spec/en.yml +0 -3
- data/spec/generators/active_record_generator_spec.rb +49 -40
- data/spec/generators/mongoid_generator_spec.rb +4 -6
- data/spec/generators/no_brainer_generator_spec.rb +29 -0
- data/spec/{en_deprecated_style.yml → localizer_test_model_deprecated_style.yml} +6 -3
- data/spec/localizer_test_model_new_style.yml +11 -0
- data/spec/models/active_record/active_record_callback.rb +93 -0
- data/spec/models/active_record/complex_active_record_example.rb +5 -1
- data/spec/models/active_record/instance_level_skip_validation_example.rb +19 -0
- data/spec/models/{invalid_persistor.rb → active_record/invalid_persistor.rb} +0 -2
- data/spec/models/active_record/localizer_test_model.rb +11 -3
- data/spec/models/active_record/namespaced.rb +16 -0
- data/spec/models/active_record/person.rb +23 -0
- data/spec/models/{silent_persistor.rb → active_record/silent_persistor.rb} +0 -2
- data/spec/models/active_record/simple_new_dsl.rb +15 -0
- data/spec/models/active_record/timestamp_example.rb +16 -0
- data/spec/models/{transactor.rb → active_record/transactor.rb} +25 -2
- data/spec/models/{validator.rb → active_record/validator.rb} +0 -2
- data/spec/models/active_record/work.rb +3 -0
- data/spec/models/{worker.rb → active_record/worker.rb} +0 -0
- data/spec/models/callbacks/basic.rb +5 -2
- data/spec/models/callbacks/with_state_arg.rb +5 -1
- data/spec/models/callbacks/with_state_arg_multiple.rb +4 -1
- data/spec/models/default_state.rb +1 -1
- data/spec/models/guard_arguments_check.rb +17 -0
- data/spec/models/guard_with_params.rb +1 -1
- data/spec/models/guardian_without_from_specified.rb +18 -0
- data/spec/models/mongoid/invalid_persistor_mongoid.rb +39 -0
- data/spec/models/mongoid/silent_persistor_mongoid.rb +39 -0
- data/spec/models/mongoid/timestamp_example_mongoid.rb +20 -0
- data/spec/models/mongoid/validator_mongoid.rb +100 -0
- data/spec/models/multiple_transitions_that_differ_only_by_guard.rb +31 -0
- data/spec/models/namespaced_multiple_example.rb +14 -0
- data/spec/models/nobrainer/complex_no_brainer_example.rb +36 -0
- data/spec/models/nobrainer/invalid_persistor_no_brainer.rb +39 -0
- data/spec/models/nobrainer/no_scope_no_brainer.rb +21 -0
- data/spec/models/nobrainer/nobrainer_relationships.rb +25 -0
- data/spec/models/nobrainer/silent_persistor_no_brainer.rb +39 -0
- data/spec/models/nobrainer/simple_new_dsl_nobrainer.rb +25 -0
- data/spec/models/{mongo_mapper/simple_mongo_mapper.rb → nobrainer/simple_no_brainer.rb} +8 -8
- data/spec/models/nobrainer/validator_no_brainer.rb +98 -0
- data/spec/models/parametrised_event.rb +7 -0
- data/spec/models/{mongo_mapper/complex_mongo_mapper_example.rb → redis/complex_redis_example.rb} +8 -5
- data/spec/models/redis/redis_multiple.rb +20 -0
- data/spec/models/redis/redis_simple.rb +20 -0
- data/spec/models/sequel/complex_sequel_example.rb +4 -3
- data/spec/models/sequel/invalid_persistor.rb +52 -0
- data/spec/models/sequel/sequel_multiple.rb +13 -13
- data/spec/models/sequel/sequel_simple.rb +13 -12
- data/spec/models/sequel/silent_persistor.rb +50 -0
- data/spec/models/sequel/transactor.rb +112 -0
- data/spec/models/sequel/validator.rb +93 -0
- data/spec/models/sequel/worker.rb +12 -0
- data/spec/models/simple_example.rb +8 -0
- data/spec/models/simple_example_with_guard_args.rb +17 -0
- data/spec/models/simple_multiple_example.rb +12 -0
- data/spec/models/sub_class.rb +34 -0
- data/spec/models/timestamps_example.rb +19 -0
- data/spec/models/timestamps_with_named_machine_example.rb +13 -0
- data/spec/spec_helper.rb +15 -33
- data/spec/spec_helpers/active_record.rb +8 -0
- data/spec/spec_helpers/dynamoid.rb +35 -0
- data/spec/spec_helpers/mongoid.rb +26 -0
- data/spec/spec_helpers/nobrainer.rb +15 -0
- data/spec/spec_helpers/redis.rb +18 -0
- data/spec/spec_helpers/remove_warnings.rb +1 -0
- data/spec/spec_helpers/sequel.rb +7 -0
- data/spec/unit/abstract_class_spec.rb +27 -0
- data/spec/unit/api_spec.rb +79 -72
- data/spec/unit/callback_multiple_spec.rb +7 -3
- data/spec/unit/callbacks_spec.rb +37 -2
- data/spec/unit/complex_example_spec.rb +12 -3
- data/spec/unit/complex_multiple_example_spec.rb +20 -4
- data/spec/unit/event_multiple_spec.rb +1 -1
- data/spec/unit/event_spec.rb +29 -4
- data/spec/unit/exception_spec.rb +1 -1
- data/spec/unit/guard_arguments_check_spec.rb +9 -0
- data/spec/unit/guard_spec.rb +17 -0
- data/spec/unit/guard_with_params_spec.rb +4 -0
- data/spec/unit/guard_without_from_specified_spec.rb +10 -0
- data/spec/unit/inspection_multiple_spec.rb +9 -5
- data/spec/unit/inspection_spec.rb +7 -3
- data/spec/unit/invoker_spec.rb +189 -0
- data/spec/unit/invokers/base_invoker_spec.rb +72 -0
- data/spec/unit/invokers/class_invoker_spec.rb +95 -0
- data/spec/unit/invokers/literal_invoker_spec.rb +86 -0
- data/spec/unit/invokers/proc_invoker_spec.rb +86 -0
- data/spec/unit/localizer_spec.rb +85 -52
- data/spec/unit/multiple_transitions_that_differ_only_by_guard_spec.rb +14 -0
- data/spec/unit/namespaced_multiple_example_spec.rb +22 -0
- data/spec/unit/override_warning_spec.rb +8 -0
- data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +468 -447
- data/spec/unit/persistence/active_record_persistence_spec.rb +639 -486
- data/spec/unit/persistence/dynamoid_persistence_multiple_spec.rb +4 -9
- data/spec/unit/persistence/dynamoid_persistence_spec.rb +4 -9
- data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +83 -13
- data/spec/unit/persistence/mongoid_persistence_spec.rb +97 -13
- data/spec/unit/persistence/no_brainer_persistence_multiple_spec.rb +198 -0
- data/spec/unit/persistence/no_brainer_persistence_spec.rb +158 -0
- data/spec/unit/persistence/redis_persistence_multiple_spec.rb +88 -0
- data/spec/unit/persistence/redis_persistence_spec.rb +8 -32
- data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +6 -11
- data/spec/unit/persistence/sequel_persistence_spec.rb +278 -10
- data/spec/unit/rspec_matcher_spec.rb +9 -0
- data/spec/unit/simple_example_spec.rb +15 -0
- data/spec/unit/simple_multiple_example_spec.rb +28 -0
- data/spec/unit/state_spec.rb +23 -7
- data/spec/unit/subclassing_multiple_spec.rb +37 -2
- data/spec/unit/subclassing_spec.rb +17 -2
- data/spec/unit/timestamps_spec.rb +32 -0
- data/spec/unit/transition_spec.rb +1 -1
- data/test/minitest_helper.rb +57 -0
- data/test/unit/minitest_matcher_test.rb +80 -0
- metadata +213 -37
- data/callbacks.txt +0 -51
- data/gemfiles/rails_3.2_stable.gemfile +0 -15
- data/gemfiles/rails_4.0.gemfile +0 -16
- data/gemfiles/rails_4.0_mongo_mapper.gemfile +0 -16
- data/gemfiles/rails_4.2_mongo_mapper.gemfile +0 -17
- data/lib/aasm/persistence/mongo_mapper_persistence.rb +0 -163
- data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +0 -21
- data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +0 -25
- data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +0 -149
- data/spec/unit/persistence/mongo_mapper_persistence_spec.rb +0 -96
@@ -13,7 +13,7 @@ describe 'subclassing' do
|
|
13
13
|
expect(SuperClass.aasm.states).not_to include(:foo)
|
14
14
|
end
|
15
15
|
|
16
|
-
it
|
16
|
+
it 'should have the same events as its parent' do
|
17
17
|
expect(SubClass.aasm.events).to eq(SuperClass.aasm.events)
|
18
18
|
end
|
19
19
|
|
@@ -27,5 +27,20 @@ describe 'subclassing' do
|
|
27
27
|
expect(son.aasm.current_state).to eq(:ended)
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
it 'should allow the child to modify its state machine' do
|
31
|
+
son = SubClass.new
|
32
|
+
expect(son.called_after).to eq(nil)
|
33
|
+
son.foo
|
34
|
+
expect(son.called_after).to eq(true)
|
35
|
+
global_callbacks = SubClass.aasm.state_machine.global_callbacks
|
36
|
+
expect(global_callbacks).to_not be_empty
|
37
|
+
expect(global_callbacks[:after_all_transitions]).to eq :after_all_event
|
38
|
+
end
|
31
39
|
|
40
|
+
it 'should not modify the parent state machine' do
|
41
|
+
super_class_event = SuperClass.aasm.events.select { |event| event.name == :foo }.first
|
42
|
+
expect(super_class_event.options).to be_empty
|
43
|
+
expect(SuperClass.aasm.state_machine.global_callbacks).to be_empty
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'timestamps option' do
|
4
|
+
it 'calls a timestamp setter based on the state name when entering a new state' do
|
5
|
+
object = TimestampsExample.new
|
6
|
+
expect { object.open }.to change { object.opened_at }.from(nil).to(instance_of(::Time))
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'overwrites any previous timestamp if a state is entered repeatedly' do
|
10
|
+
object = TimestampsExample.new
|
11
|
+
object.opened_at = ::Time.new(2000, 1, 1)
|
12
|
+
expect { object.open }.to change { object.opened_at }
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'does nothing if there is no setter matching the new state' do
|
16
|
+
object = TimestampsExample.new
|
17
|
+
expect { object.close }.not_to change { object.closed_at }
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'can be turned off and on' do
|
21
|
+
object = TimestampsExample.new
|
22
|
+
object.class.aasm.state_machine.config.timestamps = false
|
23
|
+
expect { object.open }.not_to change { object.opened_at }
|
24
|
+
object.class.aasm.state_machine.config.timestamps = true
|
25
|
+
expect { object.open }.to change { object.opened_at }
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'calls a timestamp setter when using a named state machine' do
|
29
|
+
object = TimestampsWithNamedMachineExample.new
|
30
|
+
expect { object.open }.to change { object.opened_at }.from(nil).to(instance_of(::Time))
|
31
|
+
end
|
32
|
+
end
|
@@ -6,7 +6,7 @@ describe 'transitions' do
|
|
6
6
|
process = ProcessWithNewDsl.new
|
7
7
|
expect { process.stop! }.to raise_error do |err|
|
8
8
|
expect(err.class).to eql(AASM::InvalidTransition)
|
9
|
-
expect(err.message).to eql("Event 'stop' cannot transition from 'sleeping'.
|
9
|
+
expect(err.message).to eql("Event 'stop' cannot transition from 'sleeping'.")
|
10
10
|
expect(err.object).to eql(process)
|
11
11
|
expect(err.event_name).to eql(:stop)
|
12
12
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec', 'models')))
|
3
|
+
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
|
4
|
+
require 'aasm'
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'aasm/minitest_spec'
|
7
|
+
require 'pry'
|
8
|
+
|
9
|
+
# require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = 'annoying'
|
10
|
+
# require 'ruby-debug/completion'
|
11
|
+
# require 'pry'
|
12
|
+
|
13
|
+
SEQUEL_DB = defined?(JRUBY_VERSION) ? 'jdbc:sqlite::memory:' : 'sqlite:/'
|
14
|
+
|
15
|
+
def load_schema
|
16
|
+
require 'logger'
|
17
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
18
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
19
|
+
ActiveRecord::Base.establish_connection(config['sqlite3'])
|
20
|
+
require File.dirname(__FILE__) + "/database.rb"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Dynamoid initialization
|
24
|
+
begin
|
25
|
+
require 'dynamoid'
|
26
|
+
require 'aws-sdk-resources'
|
27
|
+
|
28
|
+
ENV['ACCESS_KEY'] ||= 'abcd'
|
29
|
+
ENV['SECRET_KEY'] ||= '1234'
|
30
|
+
|
31
|
+
Aws.config.update({
|
32
|
+
region: 'us-west-2',
|
33
|
+
credentials: Aws::Credentials.new(ENV['ACCESS_KEY'], ENV['SECRET_KEY'])
|
34
|
+
})
|
35
|
+
|
36
|
+
Dynamoid.configure do |config|
|
37
|
+
config.namespace = 'dynamoid_tests'
|
38
|
+
config.endpoint = "http://#{ENV['DYNAMODB_HOST'] || '127.0.0.1'}:30180"
|
39
|
+
config.warn_on_scan = false
|
40
|
+
end
|
41
|
+
|
42
|
+
Dynamoid.logger.level = Logger::FATAL
|
43
|
+
|
44
|
+
class Minitest::Spec
|
45
|
+
before do
|
46
|
+
Dynamoid.adapter.list_tables.each do |table|
|
47
|
+
Dynamoid.adapter.delete_table(table) if table =~ /^#{Dynamoid::Config.namespace}/
|
48
|
+
end
|
49
|
+
Dynamoid.adapter.tables.clear
|
50
|
+
end
|
51
|
+
end
|
52
|
+
rescue LoadError
|
53
|
+
# Without Dynamoid settings
|
54
|
+
end
|
55
|
+
|
56
|
+
# example model classes
|
57
|
+
Dir[File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec', 'models')) + '/*.rb'].sort.each { |f| require File.expand_path(f) }
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
class StateMachineTest < Minitest::Spec
|
4
|
+
|
5
|
+
let(:simple) { SimpleExample.new }
|
6
|
+
let(:multiple) { SimpleMultipleExample.new }
|
7
|
+
|
8
|
+
describe 'transition_from' do
|
9
|
+
it "works for simple state machines" do
|
10
|
+
simple.must_transition_from :initialised, to: :filled_out, on_event: :fill_out
|
11
|
+
simple.wont_transition_from :initialised, to: :authorised, on_event: :fill_out
|
12
|
+
end
|
13
|
+
|
14
|
+
it "works for multiple state machines" do
|
15
|
+
multiple.must_transition_from :standing, to: :walking, on_event: :walk, on: :move
|
16
|
+
multiple.wont_transition_from :standing, to: :running, on_event: :walk, on: :move
|
17
|
+
|
18
|
+
multiple.must_transition_from :sleeping, to: :processing, on_event: :start, on: :work
|
19
|
+
multiple.wont_transition_from :sleeping, to: :sleeping, on_event: :start, on: :work
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'allow_transition_to' do
|
24
|
+
it "works for simple state machines" do
|
25
|
+
simple.must_allow_transition_to :filled_out
|
26
|
+
simple.wont_allow_transition_to :authorised
|
27
|
+
end
|
28
|
+
|
29
|
+
it "works for multiple state machines" do
|
30
|
+
multiple.must_allow_transition_to :walking, on: :move
|
31
|
+
multiple.wont_allow_transition_to :standing, on: :move
|
32
|
+
|
33
|
+
multiple.must_allow_transition_to :processing, on: :work
|
34
|
+
multiple.wont_allow_transition_to :sleeping, on: :work
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "have_state" do
|
39
|
+
it "works for simple state machines" do
|
40
|
+
simple.must_have_state :initialised
|
41
|
+
simple.wont_have_state :filled_out
|
42
|
+
simple.fill_out
|
43
|
+
simple.must_have_state :filled_out
|
44
|
+
end
|
45
|
+
|
46
|
+
it "works for multiple state machines" do
|
47
|
+
multiple.must_have_state :standing, on: :move
|
48
|
+
multiple.wont_have_state :walking, on: :move
|
49
|
+
multiple.walk
|
50
|
+
multiple.must_have_state :walking, on: :move
|
51
|
+
|
52
|
+
multiple.must_have_state :sleeping, on: :work
|
53
|
+
multiple.wont_have_state :processing, on: :work
|
54
|
+
multiple.start
|
55
|
+
multiple.must_have_state :processing, on: :work
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "allow_event" do
|
60
|
+
it "works for simple state machines" do
|
61
|
+
simple.must_allow_event :fill_out
|
62
|
+
simple.wont_allow_event :authorise
|
63
|
+
simple.fill_out
|
64
|
+
simple.must_allow_event :authorise
|
65
|
+
end
|
66
|
+
|
67
|
+
it "works for multiple state machines" do
|
68
|
+
multiple.must_allow_event :walk, on: :move
|
69
|
+
multiple.wont_allow_event :hold, on: :move
|
70
|
+
multiple.walk
|
71
|
+
multiple.must_allow_event :hold, on: :move
|
72
|
+
|
73
|
+
multiple.must_allow_event :start, on: :work
|
74
|
+
multiple.wont_allow_event :stop, on: :work
|
75
|
+
multiple.start
|
76
|
+
multiple.must_allow_event :stop, on: :work
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|