aasm 4.0.8 → 4.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +7 -2
- data/CHANGELOG.md +43 -2
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +6 -3
- data/LICENSE +1 -1
- data/PLANNED_CHANGES.md +8 -4
- data/README.md +171 -14
- data/aasm.gemspec +1 -5
- data/gemfiles/rails_3.2.gemfile +3 -2
- data/gemfiles/rails_4.0.gemfile +2 -6
- data/gemfiles/rails_4.0_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.1.gemfile +2 -6
- data/gemfiles/rails_4.1_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.2.gemfile +1 -5
- data/gemfiles/rails_4.2_mongo_mapper.gemfile +14 -0
- data/gemfiles/rails_4.2_mongoid_5.gemfile +12 -0
- data/lib/aasm/aasm.rb +69 -36
- data/lib/aasm/base.rb +51 -21
- data/lib/aasm/core/event.rb +7 -6
- data/lib/aasm/core/state.rb +11 -8
- data/lib/aasm/core/transition.rb +7 -5
- data/lib/aasm/errors.rb +15 -1
- data/lib/aasm/instance_base.rb +17 -13
- data/lib/aasm/localizer.rb +1 -1
- data/lib/aasm/persistence/active_record_persistence.rb +63 -73
- data/lib/aasm/persistence/base.rb +55 -20
- data/lib/aasm/persistence/mongo_mapper_persistence.rb +157 -0
- data/lib/aasm/persistence/mongoid_persistence.rb +16 -41
- data/lib/aasm/persistence/plain_persistence.rb +8 -7
- data/lib/aasm/persistence/sequel_persistence.rb +12 -9
- data/lib/aasm/persistence.rb +2 -0
- data/lib/aasm/rspec/allow_event.rb +22 -0
- data/lib/aasm/rspec/allow_transition_to.rb +22 -0
- data/lib/aasm/rspec/have_state.rb +22 -0
- data/lib/aasm/rspec/transition_from.rb +32 -0
- data/lib/aasm/rspec.rb +5 -0
- data/lib/aasm/state_machine.rb +20 -6
- data/lib/aasm/version.rb +1 -1
- data/spec/database.rb +27 -1
- data/spec/models/active_record/basic_active_record_two_state_machines_example.rb +25 -0
- data/spec/models/active_record/complex_active_record_example.rb +33 -0
- data/spec/models/active_record/derivate_new_dsl.rb +7 -0
- data/spec/models/active_record/false_state.rb +35 -0
- data/spec/models/active_record/gate.rb +39 -0
- data/spec/models/active_record/localizer_test_model.rb +34 -0
- data/spec/models/active_record/no_direct_assignment.rb +21 -0
- data/spec/models/active_record/no_scope.rb +21 -0
- data/spec/models/active_record/persisted_state.rb +12 -0
- data/spec/models/active_record/provided_and_persisted_state.rb +24 -0
- data/spec/models/active_record/reader.rb +7 -0
- data/spec/models/active_record/readme_job.rb +21 -0
- data/spec/models/active_record/simple_new_dsl.rb +17 -0
- data/spec/models/active_record/thief.rb +29 -0
- data/spec/models/active_record/transient.rb +6 -0
- data/spec/models/active_record/with_enum.rb +39 -0
- data/spec/models/active_record/with_false_enum.rb +31 -0
- data/spec/models/active_record/with_true_enum.rb +39 -0
- data/spec/models/active_record/writer.rb +6 -0
- data/spec/models/basic_two_state_machines_example.rb +25 -0
- data/spec/models/callbacks/basic.rb +3 -0
- data/spec/models/callbacks/basic_multiple.rb +75 -0
- data/spec/models/callbacks/guard_within_block_multiple.rb +66 -0
- data/spec/models/callbacks/multiple_transitions_transition_guard_multiple.rb +65 -0
- data/spec/models/callbacks/private_method_multiple.rb +44 -0
- data/spec/models/callbacks/with_args.rb +9 -9
- data/spec/models/callbacks/with_args_multiple.rb +61 -0
- data/spec/models/callbacks/{with_state_args.rb → with_state_arg.rb} +1 -1
- data/spec/models/callbacks/with_state_arg_multiple.rb +26 -0
- data/spec/models/complex_example.rb +222 -0
- data/spec/models/conversation.rb +47 -1
- data/spec/models/default_state.rb +12 -0
- data/spec/models/foo.rb +57 -0
- data/spec/models/foo_callback_multiple.rb +45 -0
- data/spec/models/guardian.rb +10 -0
- data/spec/models/guardian_multiple.rb +48 -0
- data/spec/models/initial_state_proc.rb +31 -0
- data/spec/models/invalid_persistor.rb +15 -0
- data/spec/models/mongo_mapper/complex_mongo_mapper_example.rb +37 -0
- data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +21 -0
- data/spec/models/mongo_mapper/simple_mongo_mapper.rb +23 -0
- data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +25 -0
- data/spec/models/mongoid/complex_mongoid_example.rb +37 -0
- data/spec/models/mongoid/no_scope_mongoid.rb +11 -0
- data/spec/models/mongoid/simple_mongoid.rb +12 -0
- data/spec/models/mongoid/simple_new_dsl_mongoid.rb +14 -1
- data/spec/models/no_initial_state.rb +25 -0
- data/spec/models/parametrised_event.rb +1 -1
- data/spec/models/parametrised_event_multiple.rb +29 -0
- data/spec/models/provided_state.rb +24 -0
- data/spec/models/sequel/complex_sequel_example.rb +45 -0
- data/spec/models/sequel/sequel_multiple.rb +25 -0
- data/spec/models/sequel/sequel_simple.rb +25 -0
- data/spec/models/silencer.rb +5 -0
- data/spec/models/simple_example.rb +15 -0
- data/spec/models/simple_multiple_example.rb +30 -0
- data/spec/models/state_machine_with_failed_event.rb +12 -0
- data/spec/models/sub_class.rb +7 -0
- data/spec/models/sub_class_with_more_states.rb +18 -0
- data/spec/models/super_class.rb +46 -0
- data/spec/models/transactor.rb +27 -0
- data/spec/models/valid_state_name.rb +23 -0
- data/spec/models/validator.rb +47 -0
- data/spec/spec_helper.rb +4 -4
- data/spec/unit/api_spec.rb +6 -1
- data/spec/unit/basic_two_state_machines_example_spec.rb +10 -0
- data/spec/unit/callback_multiple_spec.rb +295 -0
- data/spec/unit/callbacks_spec.rb +3 -2
- data/spec/unit/complex_example_spec.rb +2 -2
- data/spec/unit/complex_multiple_example_spec.rb +99 -0
- data/spec/unit/edge_cases_spec.rb +16 -0
- data/spec/unit/event_multiple_spec.rb +73 -0
- data/spec/unit/event_naming_spec.rb +2 -15
- data/spec/unit/event_spec.rb +11 -6
- data/spec/unit/guard_multiple_spec.rb +60 -0
- data/spec/unit/guard_spec.rb +12 -0
- data/spec/unit/initial_state_multiple_spec.rb +15 -0
- data/spec/unit/initial_state_spec.rb +3 -18
- data/spec/unit/inspection_multiple_spec.rb +201 -0
- data/spec/unit/inspection_spec.rb +12 -7
- data/spec/unit/localizer_spec.rb +0 -38
- data/spec/unit/persistence/active_record_persistence_multiple_spec.rb +573 -0
- data/spec/unit/persistence/active_record_persistence_spec.rb +70 -13
- data/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb +146 -0
- data/spec/unit/persistence/mongo_mapper_persistence_spec.rb +93 -0
- data/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +127 -0
- data/spec/unit/persistence/mongoid_persistence_spec.rb +79 -0
- data/spec/unit/persistence/sequel_persistence_multiple_spec.rb +153 -0
- data/spec/unit/persistence/sequel_persistence_spec.rb +20 -23
- data/spec/unit/readme_spec.rb +42 -0
- data/spec/unit/reloading_spec.rb +1 -1
- data/spec/unit/rspec_matcher_spec.rb +79 -0
- data/spec/unit/simple_example_spec.rb +26 -42
- data/spec/unit/simple_multiple_example_spec.rb +63 -0
- data/spec/unit/state_spec.rb +3 -1
- data/spec/unit/subclassing_multiple_spec.rb +39 -0
- data/spec/unit/subclassing_spec.rb +10 -10
- data/spec/unit/transition_spec.rb +39 -25
- metadata +152 -23
- data/spec/models/active_record/api.rb +0 -75
- data/spec/models/argument.rb +0 -11
- data/spec/models/auth_machine.rb +0 -88
- data/spec/models/bar.rb +0 -15
- data/spec/models/father.rb +0 -21
- data/spec/models/persistence.rb +0 -164
- data/spec/models/son.rb +0 -3
- data/spec/unit/persistence/mongoid_persistance_spec.rb +0 -152
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
RSpec::Matchers.define :allow_transition_to do |state|
|
|
2
|
+
match do |obj|
|
|
3
|
+
@state_machine_name ||= :default
|
|
4
|
+
obj.aasm(@state_machine_name).states(:permitted => true).include?(state)
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
chain :on do |state_machine_name|
|
|
8
|
+
@state_machine_name = state_machine_name
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
description do
|
|
12
|
+
"allow transition to #{expected} (on :#{@state_machine_name})"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
failure_message do |obj|
|
|
16
|
+
"expected that the state :#{expected} would be reachable (on :#{@state_machine_name})"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
failure_message_when_negated do |obj|
|
|
20
|
+
"expected that the state :#{expected} would not be reachable (on :#{@state_machine_name})"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
RSpec::Matchers.define :have_state do |state|
|
|
2
|
+
match do |obj|
|
|
3
|
+
@state_machine_name ||= :default
|
|
4
|
+
obj.aasm(@state_machine_name).current_state == state.to_sym
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
chain :on do |state_machine_name|
|
|
8
|
+
@state_machine_name = state_machine_name
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
description do
|
|
12
|
+
"have state #{expected} (on :#{@state_machine_name})"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
failure_message do |obj|
|
|
16
|
+
"expected that :#{obj.aasm(@state_machine_name).current_state} would be :#{expected} (on :#{@state_machine_name})"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
failure_message_when_negated do |obj|
|
|
20
|
+
"expected that :#{obj.aasm(@state_machine_name).current_state} would not be :#{expected} (on :#{@state_machine_name})"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
RSpec::Matchers.define :transition_from do |from_state|
|
|
2
|
+
match do |obj|
|
|
3
|
+
@state_machine_name ||= :default
|
|
4
|
+
obj.aasm(@state_machine_name).current_state = from_state.to_sym
|
|
5
|
+
# expect(obj).to receive(:broadcast_event).with(@event.to_s, obj, from_state, @to_state)
|
|
6
|
+
obj.send(@event) && obj.aasm(@state_machine_name).current_state == @to_state.to_sym
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
chain :on do |state_machine_name|
|
|
10
|
+
@state_machine_name = state_machine_name
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
chain :to do |state|
|
|
14
|
+
@to_state = state
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
chain :on_event do |event|
|
|
18
|
+
@event = event
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
description do
|
|
22
|
+
"transition state to :#{@to_state} from :#{expected} on event :#{@event} (on :#{@state_machine_name})"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
failure_message do |obj|
|
|
26
|
+
"expected that :#{obj.aasm(@state_machine_name).current_state} would be :#{@to_state} (on :#{@state_machine_name})"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
failure_message_when_negated do |obj|
|
|
30
|
+
"expected that :#{obj.aasm(@state_machine_name).current_state} would not be :#{@to_state} (on :#{@state_machine_name})"
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/aasm/rspec.rb
ADDED
data/lib/aasm/state_machine.rb
CHANGED
|
@@ -10,13 +10,15 @@ module AASM
|
|
|
10
10
|
(@machines ||= {})[klass.to_s] = machine
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
attr_accessor :states, :events, :initial_state, :config
|
|
13
|
+
attr_accessor :states, :events, :initial_state, :config, :name, :global_callbacks
|
|
14
14
|
|
|
15
|
-
def initialize
|
|
15
|
+
def initialize(name)
|
|
16
16
|
@initial_state = nil
|
|
17
17
|
@states = []
|
|
18
18
|
@events = {}
|
|
19
|
+
@global_callbacks = {}
|
|
19
20
|
@config = AASM::Configuration.new
|
|
21
|
+
@name = name
|
|
20
22
|
end
|
|
21
23
|
|
|
22
24
|
# called internally by Ruby 1.9 after clone()
|
|
@@ -26,13 +28,25 @@ module AASM
|
|
|
26
28
|
@events = @events.dup
|
|
27
29
|
end
|
|
28
30
|
|
|
29
|
-
def add_state(
|
|
30
|
-
set_initial_state(
|
|
31
|
+
def add_state(state_name, klass, options)
|
|
32
|
+
set_initial_state(state_name, options)
|
|
31
33
|
|
|
32
34
|
# allow reloading, extending or redefining a state
|
|
33
|
-
@states.delete(
|
|
35
|
+
@states.delete(state_name) if @states.include?(state_name)
|
|
34
36
|
|
|
35
|
-
@states << AASM::Core::State.new(
|
|
37
|
+
@states << AASM::Core::State.new(state_name, klass, self, options)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def add_event(name, options, &block)
|
|
41
|
+
@events[name] = AASM::Core::Event.new(name, self, options, &block)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def add_global_callbacks(name, *callbacks, &block)
|
|
45
|
+
@global_callbacks[name] ||= []
|
|
46
|
+
callbacks.each do |callback|
|
|
47
|
+
@global_callbacks[name] << callback
|
|
48
|
+
end
|
|
49
|
+
@global_callbacks[name] << block if block
|
|
36
50
|
end
|
|
37
51
|
|
|
38
52
|
private
|
data/lib/aasm/version.rb
CHANGED
data/spec/database.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
ActiveRecord::Migration.suppress_messages do
|
|
2
|
-
%w{gates readers writers transients simples
|
|
2
|
+
%w{gates multiple_gates readers writers transients simples no_scopes multiple_no_scopes no_direct_assignments multiple_no_direct_assignments thieves multiple_thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums false_states multiple_with_enums multiple_with_true_enums multiple_with_false_enums multiple_false_states readme_jobs}.each do |table_name|
|
|
3
3
|
ActiveRecord::Migration.create_table table_name, :force => true do |t|
|
|
4
4
|
t.string "aasm_state"
|
|
5
5
|
end
|
|
@@ -8,17 +8,34 @@ ActiveRecord::Migration.suppress_messages do
|
|
|
8
8
|
ActiveRecord::Migration.create_table "simple_new_dsls", :force => true do |t|
|
|
9
9
|
t.string "status"
|
|
10
10
|
end
|
|
11
|
+
ActiveRecord::Migration.create_table "multiple_simple_new_dsls", :force => true do |t|
|
|
12
|
+
t.string "status"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
ActiveRecord::Migration.create_table "complex_active_record_examples", :force => true do |t|
|
|
16
|
+
t.string "left"
|
|
17
|
+
t.string "right"
|
|
18
|
+
end
|
|
11
19
|
|
|
12
20
|
ActiveRecord::Migration.create_table "validators", :force => true do |t|
|
|
13
21
|
t.string "name"
|
|
14
22
|
t.string "status"
|
|
15
23
|
end
|
|
24
|
+
ActiveRecord::Migration.create_table "multiple_validators", :force => true do |t|
|
|
25
|
+
t.string "name"
|
|
26
|
+
t.string "status"
|
|
27
|
+
end
|
|
16
28
|
|
|
17
29
|
ActiveRecord::Migration.create_table "transactors", :force => true do |t|
|
|
18
30
|
t.string "name"
|
|
19
31
|
t.string "status"
|
|
20
32
|
t.integer "worker_id"
|
|
21
33
|
end
|
|
34
|
+
ActiveRecord::Migration.create_table "multiple_transactors", :force => true do |t|
|
|
35
|
+
t.string "name"
|
|
36
|
+
t.string "status"
|
|
37
|
+
t.integer "worker_id"
|
|
38
|
+
end
|
|
22
39
|
|
|
23
40
|
ActiveRecord::Migration.create_table "workers", :force => true do |t|
|
|
24
41
|
t.string "name"
|
|
@@ -29,9 +46,18 @@ ActiveRecord::Migration.suppress_messages do
|
|
|
29
46
|
t.string "name"
|
|
30
47
|
t.string "status"
|
|
31
48
|
end
|
|
49
|
+
ActiveRecord::Migration.create_table "multiple_invalid_persistors", :force => true do |t|
|
|
50
|
+
t.string "name"
|
|
51
|
+
t.string "status"
|
|
52
|
+
end
|
|
32
53
|
|
|
33
54
|
ActiveRecord::Migration.create_table "fathers", :force => true do |t|
|
|
34
55
|
t.string "aasm_state"
|
|
35
56
|
t.string "type"
|
|
36
57
|
end
|
|
58
|
+
|
|
59
|
+
ActiveRecord::Migration.create_table "basic_active_record_two_state_machines_examples", :force => true do |t|
|
|
60
|
+
t.string "search"
|
|
61
|
+
t.string "sync"
|
|
62
|
+
end
|
|
37
63
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
class BasicActiveRecordTwoStateMachinesExample < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
aasm :search do
|
|
5
|
+
state :initialised, :initial => true
|
|
6
|
+
state :queried
|
|
7
|
+
state :requested
|
|
8
|
+
|
|
9
|
+
event :query do
|
|
10
|
+
transitions :from => [:initialised, :requested], :to => :queried
|
|
11
|
+
end
|
|
12
|
+
event :request do
|
|
13
|
+
transitions :from => :queried, :to => :requested
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
aasm :sync do
|
|
18
|
+
state :unsynced, :initial => true
|
|
19
|
+
state :synced
|
|
20
|
+
|
|
21
|
+
event :synchronise do
|
|
22
|
+
transitions :from => :unsynced, :to => :synced
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
class ComplexActiveRecordExample < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
aasm :left, :column => 'left' do
|
|
5
|
+
state :one, :initial => true
|
|
6
|
+
state :two
|
|
7
|
+
state :three
|
|
8
|
+
|
|
9
|
+
event :increment do
|
|
10
|
+
transitions :from => :one, :to => :two
|
|
11
|
+
transitions :from => :two, :to => :three
|
|
12
|
+
end
|
|
13
|
+
event :reset do
|
|
14
|
+
transitions :from => :three, :to => :one
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
aasm :right, :column => 'right' do
|
|
19
|
+
state :alpha, :initial => true
|
|
20
|
+
state :beta
|
|
21
|
+
state :gamma
|
|
22
|
+
|
|
23
|
+
event :level_up do
|
|
24
|
+
transitions :from => :alpha, :to => :beta
|
|
25
|
+
transitions :from => :beta, :to => :gamma
|
|
26
|
+
end
|
|
27
|
+
event :level_down do
|
|
28
|
+
transitions :from => :gamma, :to => :beta
|
|
29
|
+
transitions :from => :beta, :to => :alpha
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
class FalseState < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
def initialize(*args)
|
|
5
|
+
super
|
|
6
|
+
self.aasm_state = false
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
aasm do
|
|
10
|
+
state :opened
|
|
11
|
+
state :closed
|
|
12
|
+
|
|
13
|
+
event :view do
|
|
14
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class MultipleFalseState < ActiveRecord::Base
|
|
20
|
+
include AASM
|
|
21
|
+
|
|
22
|
+
def initialize(*args)
|
|
23
|
+
super
|
|
24
|
+
self.aasm_state = false
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
aasm :left do
|
|
28
|
+
state :opened
|
|
29
|
+
state :closed
|
|
30
|
+
|
|
31
|
+
event :view do
|
|
32
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
class Gate < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
# Fake this column for testing purposes
|
|
5
|
+
# attr_accessor :aasm_state
|
|
6
|
+
|
|
7
|
+
def value
|
|
8
|
+
'value'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
aasm do
|
|
12
|
+
state :opened
|
|
13
|
+
state :closed
|
|
14
|
+
|
|
15
|
+
event :view do
|
|
16
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class MultipleGate < ActiveRecord::Base
|
|
22
|
+
include AASM
|
|
23
|
+
|
|
24
|
+
# Fake this column for testing purposes
|
|
25
|
+
# attr_accessor :aasm_state
|
|
26
|
+
|
|
27
|
+
def value
|
|
28
|
+
'value'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
aasm :left, :column => :aasm_state do
|
|
32
|
+
state :opened
|
|
33
|
+
state :closed
|
|
34
|
+
|
|
35
|
+
event :view do
|
|
36
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
class LocalizerTestModel < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
aasm do
|
|
5
|
+
state :opened, :initial => true
|
|
6
|
+
state :closed
|
|
7
|
+
event :close
|
|
8
|
+
event :open
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe 'localized state names' do
|
|
13
|
+
before(:all) do
|
|
14
|
+
I18n.load_path << 'spec/en.yml'
|
|
15
|
+
I18n.default_locale = :en
|
|
16
|
+
I18n.reload!
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
after(:all) do
|
|
20
|
+
I18n.load_path.clear
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'should localize' do
|
|
24
|
+
state = LocalizerTestModel.aasm.states.detect {|s| s == :opened}
|
|
25
|
+
expect(state.localized_name).to eq("It's open now!")
|
|
26
|
+
expect(state.human_name).to eq("It's open now!")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it 'should use fallback' do
|
|
30
|
+
state = LocalizerTestModel.aasm.states.detect {|s| s == :closed}
|
|
31
|
+
expect(state.localized_name).to eq('Closed')
|
|
32
|
+
expect(state.human_name).to eq('Closed')
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class NoDirectAssignment < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
aasm :no_direct_assignment => true do
|
|
4
|
+
state :pending, :initial => true
|
|
5
|
+
state :running
|
|
6
|
+
event :run do
|
|
7
|
+
transitions :from => :pending, :to => :running
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class MultipleNoDirectAssignment < ActiveRecord::Base
|
|
13
|
+
include AASM
|
|
14
|
+
aasm :left, :column => :aasm_state, :no_direct_assignment => true do
|
|
15
|
+
state :pending, :initial => true
|
|
16
|
+
state :running
|
|
17
|
+
event :run do
|
|
18
|
+
transitions :from => :pending, :to => :running
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class NoScope < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
aasm :create_scopes => false do
|
|
4
|
+
state :pending, :initial => true
|
|
5
|
+
state :running
|
|
6
|
+
event :run do
|
|
7
|
+
transitions :from => :pending, :to => :running
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class MultipleNoScope < ActiveRecord::Base
|
|
13
|
+
include AASM
|
|
14
|
+
aasm :left, :column => :aasm_state, :create_scopes => false do
|
|
15
|
+
state :pending, :initial => true
|
|
16
|
+
state :running
|
|
17
|
+
event :run do
|
|
18
|
+
transitions :from => :pending, :to => :running
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
class PersistedState < ActiveRecord::Base
|
|
2
|
+
attr_accessor :transient_store, :persisted_store
|
|
3
|
+
include AASM
|
|
4
|
+
aasm do
|
|
5
|
+
state :alpha, :initial => true
|
|
6
|
+
state :beta
|
|
7
|
+
state :gamma
|
|
8
|
+
event :release do
|
|
9
|
+
transitions :from => [:alpha, :beta, :gamma], :to => :beta
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class ProvidedAndPersistedState < ActiveRecord::Base
|
|
2
|
+
attr_accessor :transient_store, :persisted_store
|
|
3
|
+
include AASM
|
|
4
|
+
aasm do
|
|
5
|
+
state :alpha, :initial => true
|
|
6
|
+
state :beta
|
|
7
|
+
state :gamma
|
|
8
|
+
event :release do
|
|
9
|
+
transitions :from => [:alpha, :beta, :gamma], :to => :beta
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def aasm_read_state(*args)
|
|
14
|
+
:gamma
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def aasm_write_state(new_state, *args)
|
|
18
|
+
@persisted_store = new_state
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def aasm_write_state_without_persistence(new_state, *args)
|
|
22
|
+
@transient_store = new_state
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class ReadmeJob < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
aasm do
|
|
5
|
+
state :sleeping, :initial => true
|
|
6
|
+
state :running
|
|
7
|
+
state :cleaning
|
|
8
|
+
|
|
9
|
+
event :run do
|
|
10
|
+
transitions :from => :sleeping, :to => :running
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
event :clean do
|
|
14
|
+
transitions :from => :running, :to => :cleaning
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
event :sleep do
|
|
18
|
+
transitions :from => [:running, :cleaning], :to => :sleeping
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class SimpleNewDsl < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
aasm :column => :status
|
|
4
|
+
aasm do
|
|
5
|
+
state :unknown_scope
|
|
6
|
+
state :new
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class MultipleSimpleNewDsl < ActiveRecord::Base
|
|
11
|
+
include AASM
|
|
12
|
+
aasm :left, :column => :status
|
|
13
|
+
aasm :left do
|
|
14
|
+
state :unknown_scope
|
|
15
|
+
state :new
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class Thief < ActiveRecord::Base
|
|
2
|
+
if ActiveRecord::VERSION::MAJOR >= 3
|
|
3
|
+
self.table_name = 'thieves'
|
|
4
|
+
else
|
|
5
|
+
set_table_name "thieves"
|
|
6
|
+
end
|
|
7
|
+
include AASM
|
|
8
|
+
aasm do
|
|
9
|
+
state :rich
|
|
10
|
+
state :jailed
|
|
11
|
+
initial_state Proc.new {|thief| thief.skilled ? :rich : :jailed }
|
|
12
|
+
end
|
|
13
|
+
attr_accessor :skilled, :aasm_state
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class MultipleThief < ActiveRecord::Base
|
|
17
|
+
if ActiveRecord::VERSION::MAJOR >= 3
|
|
18
|
+
self.table_name = 'multiple_thieves'
|
|
19
|
+
else
|
|
20
|
+
set_table_name "multiple_thieves"
|
|
21
|
+
end
|
|
22
|
+
include AASM
|
|
23
|
+
aasm :left, :column => :aasm_state do
|
|
24
|
+
state :rich
|
|
25
|
+
state :jailed
|
|
26
|
+
initial_state Proc.new {|thief| thief.skilled ? :rich : :jailed }
|
|
27
|
+
end
|
|
28
|
+
attr_accessor :skilled, :aasm_state
|
|
29
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
class WithEnum < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
# Fake this column for testing purposes
|
|
5
|
+
attr_accessor :aasm_state
|
|
6
|
+
|
|
7
|
+
def self.test
|
|
8
|
+
{}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
aasm :enum => :test do
|
|
12
|
+
state :opened
|
|
13
|
+
state :closed
|
|
14
|
+
|
|
15
|
+
event :view do
|
|
16
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class MultipleWithEnum < ActiveRecord::Base
|
|
22
|
+
include AASM
|
|
23
|
+
|
|
24
|
+
# Fake this column for testing purposes
|
|
25
|
+
attr_accessor :aasm_state
|
|
26
|
+
|
|
27
|
+
def self.test
|
|
28
|
+
{}
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
aasm :left, :enum => :test do
|
|
32
|
+
state :opened
|
|
33
|
+
state :closed
|
|
34
|
+
|
|
35
|
+
event :view do
|
|
36
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
class WithFalseEnum < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
# Fake this column for testing purposes
|
|
5
|
+
attr_accessor :aasm_state
|
|
6
|
+
|
|
7
|
+
aasm :enum => false do
|
|
8
|
+
state :opened
|
|
9
|
+
state :closed
|
|
10
|
+
|
|
11
|
+
event :view do
|
|
12
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class MultipleWithFalseEnum < ActiveRecord::Base
|
|
18
|
+
include AASM
|
|
19
|
+
|
|
20
|
+
# Fake this column for testing purposes
|
|
21
|
+
attr_accessor :aasm_state
|
|
22
|
+
|
|
23
|
+
aasm :left, :enum => false do
|
|
24
|
+
state :opened
|
|
25
|
+
state :closed
|
|
26
|
+
|
|
27
|
+
event :view do
|
|
28
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
class WithTrueEnum < ActiveRecord::Base
|
|
2
|
+
include AASM
|
|
3
|
+
|
|
4
|
+
# Fake this column for testing purposes
|
|
5
|
+
attr_accessor :aasm_state
|
|
6
|
+
|
|
7
|
+
def value
|
|
8
|
+
'value'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
aasm :enum => true do
|
|
12
|
+
state :opened
|
|
13
|
+
state :closed
|
|
14
|
+
|
|
15
|
+
event :view do
|
|
16
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class MultipleWithTrueEnum < ActiveRecord::Base
|
|
22
|
+
include AASM
|
|
23
|
+
|
|
24
|
+
# Fake this column for testing purposes
|
|
25
|
+
attr_accessor :aasm_state
|
|
26
|
+
|
|
27
|
+
def value
|
|
28
|
+
'value'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
aasm :left, :enum => true do
|
|
32
|
+
state :opened
|
|
33
|
+
state :closed
|
|
34
|
+
|
|
35
|
+
event :view do
|
|
36
|
+
transitions :to => :read, :from => [:needs_attention]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|