aasm 2.0.4 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -4
- data/lib/aasm.rb +1 -1
- data/lib/persistence/active_record_persistence.rb +14 -14
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -41,19 +41,19 @@ Here's a quick example highlighting some of the features.
|
|
41
41
|
class Conversation
|
42
42
|
include AASM
|
43
43
|
|
44
|
-
aasm_initial_state :
|
44
|
+
aasm_initial_state :unread
|
45
45
|
|
46
|
-
aasm_state :
|
46
|
+
aasm_state :unread
|
47
47
|
aasm_state :read
|
48
48
|
aasm_state :closed
|
49
49
|
|
50
50
|
|
51
51
|
aasm_event :view do
|
52
|
-
transitions :to => :read, :from => [:
|
52
|
+
transitions :to => :read, :from => [:unread]
|
53
53
|
end
|
54
54
|
|
55
55
|
aasm_event :close do
|
56
|
-
transitions :to => :closed, :from => [:read, :
|
56
|
+
transitions :to => :closed, :from => [:read, :unread]
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
data/lib/aasm.rb
CHANGED
@@ -37,10 +37,10 @@ module AASM
|
|
37
37
|
base.send(:include, AASM::Persistence::ActiveRecordPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
|
38
38
|
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
|
39
39
|
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)
|
40
|
-
|
40
|
+
|
41
41
|
if base.respond_to?(:named_scope)
|
42
42
|
base.extend(AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods)
|
43
|
-
|
43
|
+
|
44
44
|
base.class_eval do
|
45
45
|
class << self
|
46
46
|
alias_method :aasm_state_without_named_scope, :aasm_state
|
@@ -48,7 +48,7 @@ module AASM
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
base.before_validation_on_create :aasm_ensure_initial_state
|
53
53
|
end
|
54
54
|
|
@@ -119,7 +119,7 @@ module AASM
|
|
119
119
|
|
120
120
|
# Returns the current aasm_state of the object. Respects reload and
|
121
121
|
# any changes made to the aasm_state field directly
|
122
|
-
#
|
122
|
+
#
|
123
123
|
# Internally just calls <tt>aasm_read_state</tt>
|
124
124
|
#
|
125
125
|
# foo = Foo.find(1)
|
@@ -136,7 +136,7 @@ module AASM
|
|
136
136
|
end
|
137
137
|
|
138
138
|
private
|
139
|
-
|
139
|
+
|
140
140
|
# Ensures that if the aasm_state column is nil and the record is new
|
141
141
|
# that the initial state gets populated before validation on create
|
142
142
|
#
|
@@ -160,7 +160,7 @@ module AASM
|
|
160
160
|
|
161
161
|
module WriteStateWithoutPersistence
|
162
162
|
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
163
|
-
#
|
163
|
+
#
|
164
164
|
# foo = Foo.find(1)
|
165
165
|
# foo.aasm_current_state # => :opened
|
166
166
|
# foo.close
|
@@ -179,7 +179,7 @@ module AASM
|
|
179
179
|
module WriteState
|
180
180
|
# Writes <tt>state</tt> to the state column and persists it to the database
|
181
181
|
# using update_attribute (which bypasses validation)
|
182
|
-
#
|
182
|
+
#
|
183
183
|
# foo = Foo.find(1)
|
184
184
|
# foo.aasm_current_state # => :opened
|
185
185
|
# foo.close!
|
@@ -190,12 +190,12 @@ module AASM
|
|
190
190
|
def aasm_write_state(state)
|
191
191
|
old_value = read_attribute(self.class.aasm_column)
|
192
192
|
write_attribute(self.class.aasm_column, state.to_s)
|
193
|
-
|
193
|
+
|
194
194
|
unless self.save
|
195
195
|
write_attribute(self.class.aasm_column, old_value)
|
196
196
|
return false
|
197
197
|
end
|
198
|
-
|
198
|
+
|
199
199
|
true
|
200
200
|
end
|
201
201
|
end
|
@@ -212,17 +212,17 @@ module AASM
|
|
212
212
|
# aasm_state :opened
|
213
213
|
# aasm_state :closed
|
214
214
|
# end
|
215
|
-
#
|
215
|
+
#
|
216
216
|
# foo = Foo.new
|
217
217
|
# foo.current_state # => :opened
|
218
218
|
# foo.close
|
219
219
|
# foo.current_state # => :closed
|
220
|
-
#
|
220
|
+
#
|
221
221
|
# foo = Foo.find(1)
|
222
222
|
# foo.current_state # => :opened
|
223
223
|
# foo.aasm_state = nil
|
224
224
|
# foo.current_state # => nil
|
225
|
-
#
|
225
|
+
#
|
226
226
|
# NOTE: intended to be called from an event
|
227
227
|
#
|
228
228
|
# This allows for nil aasm states - be sure to add validation to your model
|
@@ -238,8 +238,8 @@ module AASM
|
|
238
238
|
module NamedScopeMethods
|
239
239
|
def aasm_state_with_named_scope name, options = {}
|
240
240
|
aasm_state_without_named_scope name, options
|
241
|
-
self.named_scope name, :conditions => {self.aasm_column => name.to_s} unless self.
|
242
|
-
end
|
241
|
+
self.named_scope name, :conditions => {self.aasm_column => name.to_s} unless self.respond_to?(name)
|
242
|
+
end
|
243
243
|
end
|
244
244
|
end
|
245
245
|
end
|