aasm 4.0.8 → 4.2.0

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.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -2
  3. data/CHANGELOG.md +18 -2
  4. data/CODE_OF_CONDUCT.md +13 -0
  5. data/Gemfile +6 -3
  6. data/LICENSE +1 -1
  7. data/README.md +31 -5
  8. data/aasm.gemspec +1 -5
  9. data/gemfiles/rails_3.2.gemfile +3 -2
  10. data/gemfiles/rails_4.0.gemfile +2 -6
  11. data/gemfiles/rails_4.0_bson1.gemfile +14 -0
  12. data/gemfiles/rails_4.1.gemfile +2 -6
  13. data/gemfiles/rails_4.1_bson1.gemfile +14 -0
  14. data/gemfiles/rails_4.2.gemfile +1 -5
  15. data/gemfiles/rails_4.2_bson1.gemfile +14 -0
  16. data/lib/aasm/aasm.rb +13 -7
  17. data/lib/aasm/base.rb +12 -4
  18. data/lib/aasm/core/event.rb +1 -1
  19. data/lib/aasm/core/state.rb +8 -6
  20. data/lib/aasm/errors.rb +12 -1
  21. data/lib/aasm/persistence/active_record_persistence.rb +4 -4
  22. data/lib/aasm/persistence/base.rb +3 -0
  23. data/lib/aasm/persistence/mongo_mapper_persistence.rb +174 -0
  24. data/lib/aasm/persistence/mongoid_persistence.rb +1 -5
  25. data/lib/aasm/persistence/sequel_persistence.rb +1 -0
  26. data/lib/aasm/persistence.rb +2 -0
  27. data/lib/aasm/version.rb +1 -1
  28. data/spec/models/active_record/derivate_new_dsl.rb +3 -0
  29. data/spec/models/active_record/false_state.rb +17 -0
  30. data/spec/models/active_record/gate.rb +19 -0
  31. data/spec/models/active_record/localizer_test_model.rb +34 -0
  32. data/spec/models/active_record/no_direct_assignment.rb +10 -0
  33. data/spec/models/active_record/no_scope.rb +10 -0
  34. data/spec/models/active_record/persisted_state.rb +12 -0
  35. data/spec/models/active_record/provided_and_persisted_state.rb +24 -0
  36. data/spec/models/active_record/reader.rb +7 -0
  37. data/spec/models/active_record/simple_new_dsl.rb +8 -0
  38. data/spec/models/active_record/thief.rb +14 -0
  39. data/spec/models/active_record/transient.rb +6 -0
  40. data/spec/models/active_record/with_enum.rb +19 -0
  41. data/spec/models/active_record/with_false_enum.rb +15 -0
  42. data/spec/models/active_record/with_true_enum.rb +19 -0
  43. data/spec/models/active_record/writer.rb +6 -0
  44. data/spec/models/callbacks/with_args.rb +9 -9
  45. data/spec/models/{auth_machine.rb → complex_example.rb} +1 -1
  46. data/spec/models/default_state.rb +12 -0
  47. data/spec/models/initial_state_proc.rb +15 -0
  48. data/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +10 -0
  49. data/spec/models/mongo_mapper/simple_mongo_mapper.rb +11 -0
  50. data/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +12 -0
  51. data/spec/models/mongoid/simple_new_dsl_mongoid.rb +1 -1
  52. data/spec/models/{bar.rb → no_initial_state.rb} +1 -4
  53. data/spec/models/provided_state.rb +24 -0
  54. data/spec/models/simple_example.rb +15 -0
  55. data/spec/models/state_machine_with_failed_event.rb +12 -0
  56. data/spec/models/sub_class.rb +3 -0
  57. data/spec/models/sub_class_with_more_states.rb +7 -0
  58. data/spec/models/super_class.rb +18 -0
  59. data/spec/models/{argument.rb → valid_state_name.rb} +1 -1
  60. data/spec/models/validator.rb +8 -0
  61. data/spec/spec_helper.rb +3 -4
  62. data/spec/unit/api_spec.rb +6 -1
  63. data/spec/unit/callbacks_spec.rb +1 -1
  64. data/spec/unit/complex_example_spec.rb +2 -2
  65. data/spec/unit/event_naming_spec.rb +2 -15
  66. data/spec/unit/initial_state_spec.rb +3 -18
  67. data/spec/unit/inspection_spec.rb +7 -7
  68. data/spec/unit/localizer_spec.rb +0 -38
  69. data/spec/unit/persistence/active_record_persistence_spec.rb +26 -3
  70. data/spec/unit/persistence/mongo_mapper_persistance_spec.rb +135 -0
  71. data/spec/unit/persistence/mongoid_persistance_spec.rb +4 -10
  72. data/spec/unit/persistence/sequel_persistence_spec.rb +15 -1
  73. data/spec/unit/simple_example_spec.rb +26 -42
  74. data/spec/unit/subclassing_spec.rb +10 -10
  75. data/spec/unit/transition_spec.rb +6 -1
  76. metadata +72 -19
  77. data/spec/models/active_record/api.rb +0 -75
  78. data/spec/models/father.rb +0 -21
  79. data/spec/models/persistence.rb +0 -164
  80. data/spec/models/son.rb +0 -3
@@ -0,0 +1,174 @@
1
+ require_relative 'base'
2
+
3
+ module AASM
4
+ module Persistence
5
+ module MongoMapperPersistence
6
+ # This method:
7
+ #
8
+ # * extends the model with ClassMethods
9
+ # * includes InstanceMethods
10
+ #
11
+ # Adds
12
+ #
13
+ # before_validation :aasm_ensure_initial_state, :on => :create
14
+ #
15
+ # As a result, it doesn't matter when you define your methods - the following 2 are equivalent
16
+ #
17
+ # class Foo
18
+ # include MongoMapper::Document
19
+ # def aasm_write_state(state)
20
+ # "bar"
21
+ # end
22
+ # include AASM
23
+ # end
24
+ #
25
+ # class Foo < ActiveRecord::Base
26
+ # include MongoMapper::Document
27
+ # include AASM
28
+ # def aasm_write_state(state)
29
+ # "bar"
30
+ # end
31
+ # end
32
+ #
33
+ def self.included(base)
34
+ base.send(:include, AASM::Persistence::Base)
35
+ base.extend AASM::Persistence::MongoMapperPersistence::ClassMethods
36
+ base.send(:include, AASM::Persistence::MongoMapperPersistence::InstanceMethods)
37
+
38
+ base.before_create :aasm_ensure_initial_state
39
+
40
+ # ensure state is in the list of states
41
+ base.validate :aasm_validate_states
42
+ end
43
+
44
+ module ClassMethods
45
+
46
+ def find_in_state(number, state, *args)
47
+ with_state_scope(state).find!(number, *args)
48
+ end
49
+
50
+ def count_in_state(state, *args)
51
+ with_state_scope(state).count(*args)
52
+ end
53
+
54
+ def calculate_in_state(state, *args)
55
+ with_state_scope(state).calculate(*args)
56
+ end
57
+
58
+ protected
59
+ def with_state_scope(state)
60
+ where(aasm.attribute_name.to_sym => state.to_s)
61
+ end
62
+ end
63
+
64
+ module InstanceMethods
65
+
66
+ # Writes <tt>state</tt> to the state column and persists it to the database
67
+ #
68
+ # foo = Foo.find(1)
69
+ # foo.aasm.current_state # => :opened
70
+ # foo.close!
71
+ # foo.aasm.current_state # => :closed
72
+ # Foo.find(1).aasm.current_state # => :closed
73
+ #
74
+ # NOTE: intended to be called from an event
75
+ def aasm_write_state(state)
76
+ old_value = read_attribute(self.class.aasm.attribute_name)
77
+ aasm_write_attribute state
78
+
79
+ success = if aasm_skipping_validations
80
+ value = aasm_raw_attribute_value state
81
+ self.class.where(self.class.primary_key => self.id).update_all(self.class.aasm.attribute_name => value) == 1
82
+ else
83
+ self.save
84
+ end
85
+ unless success
86
+ write_attribute(self.class.aasm.attribute_name, old_value)
87
+ return false
88
+ end
89
+
90
+ true
91
+ end
92
+
93
+ # Writes <tt>state</tt> to the state column, but does not persist it to the database
94
+ #
95
+ # foo = Foo.find(1)
96
+ # foo.aasm.current_state # => :opened
97
+ # foo.close
98
+ # foo.aasm.current_state # => :closed
99
+ # Foo.find(1).aasm.current_state # => :opened
100
+ # foo.save
101
+ # foo.aasm.current_state # => :closed
102
+ # Foo.find(1).aasm.current_state # => :closed
103
+ #
104
+ # NOTE: intended to be called from an event
105
+ def aasm_write_state_without_persistence(state)
106
+ aasm_write_attribute state
107
+ end
108
+
109
+ private
110
+ def aasm_enum
111
+ case AASM::StateMachine[self.class].config.enum
112
+ when false then nil
113
+ when true then aasm_guess_enum_method
114
+ when nil then aasm_guess_enum_method if aasm_column_looks_like_enum
115
+ else AASM::StateMachine[self.class].config.enum
116
+ end
117
+ end
118
+
119
+ def aasm_column_looks_like_enum
120
+ self.class.keys[self.class.aasm.attribute_name.to_s].type == Integer
121
+ end
122
+
123
+ def aasm_guess_enum_method
124
+ self.class.aasm.attribute_name.to_s.pluralize.to_sym
125
+ end
126
+
127
+ def aasm_skipping_validations
128
+ AASM::StateMachine[self.class].config.skip_validation_on_save
129
+ end
130
+
131
+ def aasm_write_attribute(state)
132
+ write_attribute self.class.aasm.attribute_name, aasm_raw_attribute_value(state)
133
+ end
134
+
135
+ def aasm_raw_attribute_value(state)
136
+ if aasm_enum
137
+ self.class.send(aasm_enum)[state]
138
+ else
139
+ state.to_s
140
+ end
141
+ end
142
+
143
+ # Ensures that if the aasm_state column is nil and the record is new
144
+ # that the initial state gets populated before validation on create
145
+ #
146
+ # foo = Foo.new
147
+ # foo.aasm_state # => nil
148
+ # foo.valid?
149
+ # foo.aasm_state # => "open" (where :open is the initial state)
150
+ #
151
+ #
152
+ # foo = Foo.find(:first)
153
+ # foo.aasm_state # => 1
154
+ # foo.aasm_state = nil
155
+ # foo.valid?
156
+ # foo.aasm_state # => nil
157
+ #
158
+ def aasm_ensure_initial_state
159
+ return send("#{self.class.aasm.attribute_name}=", aasm.enter_initial_state.to_s) if send(self.class.aasm.attribute_name).blank?
160
+ end
161
+
162
+ def aasm_validate_states
163
+ send("#{self.class.aasm.attribute_name}=", aasm.enter_initial_state.to_s) if send(self.class.aasm.attribute_name).blank?
164
+ unless AASM::StateMachine[self.class].config.skip_validation_on_save
165
+ if aasm.current_state && !aasm.states.include?(aasm.current_state)
166
+ self.errors.add(AASM::StateMachine[self.class].config.column , "is invalid")
167
+ end
168
+ end
169
+ end
170
+ end # InstanceMethods
171
+
172
+ end
173
+ end # Persistence
174
+ end # AASM
@@ -35,11 +35,7 @@ module AASM
35
35
  base.extend AASM::Persistence::MongoidPersistence::ClassMethods
36
36
  base.send(:include, AASM::Persistence::MongoidPersistence::InstanceMethods)
37
37
 
38
- # Mongoid's Validatable gem dependency goes not have a before_validation_on_xxx hook yet.
39
- # base.before_validation_on_create :aasm_ensure_initial_state
40
- base.before_validation :aasm_ensure_initial_state
41
- # ensure initial aasm state even when validations are skipped
42
- base.before_create :aasm_ensure_initial_state
38
+ base.after_initialize :aasm_ensure_initial_state
43
39
  end
44
40
 
45
41
  module ClassMethods
@@ -72,6 +72,7 @@ module AASM
72
72
  #
73
73
  def aasm_ensure_initial_state
74
74
  aasm.enter_initial_state if
75
+ (new? || values.key?(self.class.aasm.attribute_name)) &&
75
76
  send(self.class.aasm.attribute_name).to_s.strip.empty?
76
77
  end
77
78
 
@@ -10,6 +10,8 @@ module AASM
10
10
  include_persistence base, :active_record
11
11
  elsif hierarchy.include?("Mongoid::Document")
12
12
  include_persistence base, :mongoid
13
+ elsif hierarchy.include?("MongoMapper::Document")
14
+ include_persistence base, :mongo_mapper
13
15
  elsif hierarchy.include?("Sequel::Model")
14
16
  include_persistence base, :sequel
15
17
  else
data/lib/aasm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module AASM
2
- VERSION = "4.0.8"
2
+ VERSION = "4.2.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ require_relative 'simple_new_dsl'
2
+ class DerivateNewDsl < SimpleNewDsl
3
+ end
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,19 @@
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
@@ -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,10 @@
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
@@ -0,0 +1,10 @@
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
@@ -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
14
+ :gamma
15
+ end
16
+
17
+ def aasm_write_state(new_state)
18
+ @persisted_store = new_state
19
+ end
20
+
21
+ def aasm_write_state_without_persistence(new_state)
22
+ @transient_store = new_state
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ class Reader < ActiveRecord::Base
2
+ include AASM
3
+
4
+ def aasm_read_state
5
+ "fi"
6
+ end
7
+ end
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,6 @@
1
+ class Transient < ActiveRecord::Base
2
+ def aasm_write_state_without_persistence(state)
3
+ "fum"
4
+ end
5
+ include AASM
6
+ end
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,6 @@
1
+ class Writer < ActiveRecord::Base
2
+ def aasm_write_state(state)
3
+ "fo"
4
+ end
5
+ include AASM
6
+ end
@@ -44,15 +44,15 @@ module Callbacks
44
44
 
45
45
  def aasm_write_state(*args); log('aasm_write_state'); true; end
46
46
 
47
- def before_enter_open; log('before_enter_open'); end
48
- def before_exit_open; log('before_exit_open'); end
49
- def after_enter_open; log('after_enter_open'); end
50
- def after_exit_open; log('after_exit_open'); end
51
-
52
- def before_enter_closed; log('before_enter_closed'); end
53
- def before_exit_closed; log('before_enter_closed'); end
54
- def after_enter_closed; log('after_enter_closed'); end
55
- def after_exit_closed; log('after_exit_closed'); end
47
+ def before_enter_open(*args); log("before_enter_open(#{args.map(&:inspect).join(',')})"); end
48
+ def before_exit_open(*args); log("before_exit_open(#{args.map(&:inspect).join(',')})"); end
49
+ def after_enter_open(*args); log("after_enter_open(#{args.map(&:inspect).join(',')})"); end
50
+ def after_exit_open(*args); log("after_exit_open(#{args.map(&:inspect).join(',')})"); end
51
+
52
+ def before_enter_closed(*args); log("before_enter_closed(#{args.map(&:inspect).join(',')})"); end
53
+ def before_exit_closed(*args); log("before_enter_closed(#{args.map(&:inspect).join(',')})"); end
54
+ def after_enter_closed(*args); log("after_enter_closed(#{args.map(&:inspect).join(',')})"); end
55
+ def after_exit_closed(*args); log("after_exit_closed(#{args.map(&:inspect).join(',')})"); end
56
56
 
57
57
  def before(arg1, *args); log("before(#{arg1.inspect},#{args.map(&:inspect).join(',')})"); end
58
58
  def transition_proc(arg1, arg2); log("transition_proc(#{arg1.inspect},#{arg2.inspect})"); end
@@ -1,4 +1,4 @@
1
- class AuthMachine
1
+ class ComplexExample
2
2
  include AASM
3
3
 
4
4
  attr_accessor :activation_code, :activated_at, :deleted_at
@@ -0,0 +1,12 @@
1
+ class DefaultState
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,15 @@
1
+ class InitialStateProc
2
+ RICH = 1_000_000
3
+
4
+ attr_accessor :balance
5
+
6
+ include AASM
7
+ aasm do
8
+ state :retired
9
+ state :selling_bad_mortgages
10
+ initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages }
11
+ end
12
+
13
+ def initialize(balance = 0); self.balance = balance; end
14
+ def rich?; self.balance >= RICH; end
15
+ end
@@ -0,0 +1,10 @@
1
+ class NoScopeMongoMapper
2
+ include MongoMapper::Document
3
+ include AASM
4
+
5
+ key :status, String
6
+
7
+ aasm :create_scopes => false, :column => :status do
8
+ state :ignored_scope
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ class SimpleMongoMapper
2
+ include MongoMapper::Document
3
+ include AASM
4
+
5
+ key :status, String
6
+
7
+ aasm column: :status do
8
+ state :unknown_scope
9
+ state :next
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class SimpleNewDslMongoMapper
2
+ include MongoMapper::Document
3
+ include AASM
4
+
5
+ key :status, String
6
+
7
+ aasm :column => :status
8
+ aasm do
9
+ state :unknown_scope
10
+ state :next
11
+ end
12
+ end
@@ -6,7 +6,7 @@ class SimpleNewDslMongoid
6
6
 
7
7
  aasm :column => :status
8
8
  aasm do
9
- state :unknown_scope
9
+ state :unknown_scope, :initial => true
10
10
  state :new
11
11
  end
12
12
  end
@@ -1,4 +1,4 @@
1
- class Bar
1
+ class NoInitialState
2
2
  include AASM
3
3
 
4
4
  aasm do
@@ -10,6 +10,3 @@ class Bar
10
10
  end
11
11
  end
12
12
  end
13
-
14
- class Baz < Bar
15
- end
@@ -0,0 +1,24 @@
1
+ class ProvidedState
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
14
+ :beta
15
+ end
16
+
17
+ def aasm_write_state(new_state)
18
+ @persisted_store = new_state
19
+ end
20
+
21
+ def aasm_write_state_without_persistence(new_state)
22
+ @transient_store = new_state
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ class SimpleExample
2
+ include AASM
3
+ aasm do
4
+ state :initialised, :initial => true
5
+ state :filled_out
6
+ state :authorised
7
+
8
+ event :fill_out do
9
+ transitions :from => :initialised, :to => :filled_out
10
+ end
11
+ event :authorise do
12
+ transitions :from => :filled_out, :to => :authorised
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ class StateMachineWithFailedEvent
2
+ include AASM
3
+
4
+ aasm do
5
+ state :init, :initial => true
6
+ state :failed
7
+
8
+ event :failed do
9
+ transitions :from => :init, :to => :failed
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ require_relative 'super_class'
2
+ class SubClass < SuperClass
3
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'super_class'
2
+ class SubClassWithMoreStates < SuperClass
3
+ include AASM
4
+ aasm do
5
+ state :foo
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ class SuperClass
2
+ include AASM
3
+
4
+ aasm do
5
+ state :read
6
+ state :ended
7
+
8
+ event :foo do
9
+ transitions :to => :ended, :from => [:read]
10
+ end
11
+ end
12
+
13
+ def update_state
14
+ if may_foo?
15
+ foo!
16
+ end
17
+ end
18
+ end