stator 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -119,6 +119,8 @@ u.activated_status_at
119
119
  # => (now)
120
120
  ```
121
121
 
122
+ `track: true` will also look for a "state_changed_at" field and will update that if it's present.
123
+
122
124
  You can have multiple state machines for your model:
123
125
 
124
126
  ```ruby
@@ -66,6 +66,7 @@ module Stator
66
66
  def track_transition
67
67
  self.attempt_to_track_state(self.state_was)
68
68
  self.attempt_to_track_state(self.state)
69
+ self.attempt_to_track_state_changed_timestamp
69
70
 
70
71
  true
71
72
  end
@@ -86,6 +87,17 @@ module Stator
86
87
  end
87
88
  end
88
89
 
90
+ def attempt_to_track_state_changed_timestamp
91
+ return unless self.state_changed?
92
+
93
+ field_name = "#{@machine.field}_changed_at"
94
+
95
+ return unless @record.respond_to?(field_name)
96
+ return unless @record.respond_to?("#{field_name}=")
97
+
98
+ @record.send("#{field_name}=", (Time.zone || Time).now)
99
+ end
100
+
89
101
 
90
102
  end
91
103
  end
@@ -1,7 +1,7 @@
1
1
  module Stator
2
2
  MAJOR = 0
3
3
  MINOR = 0
4
- PATCH = 16
4
+ PATCH = 17
5
5
  PRERELEASE = nil
6
6
 
7
7
  VERSION = [MAJOR, MINOR, PATCH, PRERELEASE].compact.join('.')
@@ -121,7 +121,7 @@ describe Stator::Model do
121
121
  f = Factory.new
122
122
  f.state.should be_nil
123
123
 
124
- f.construct.should be_true
124
+ f.construct.should eql(true)
125
125
 
126
126
  f.state.should eql('constructed')
127
127
  end
@@ -141,11 +141,11 @@ describe Stator::Model do
141
141
 
142
142
  it 'should determine if it can validly execute a transition' do
143
143
  a = Animal.new
144
- a.can_birth?.should be_true
144
+ a.can_birth?.should eql(true)
145
145
 
146
146
  a.birth
147
147
 
148
- a.can_birth?.should be_false
148
+ a.can_birth?.should eql(false)
149
149
  end
150
150
 
151
151
  end
@@ -165,6 +165,23 @@ describe Stator::Model do
165
165
  a.born_status_at.should be_within(1).of(Time.zone.now)
166
166
  end
167
167
 
168
+ it 'should store when a record change states' do
169
+ a = Animal.new
170
+ a.status_changed_at.should be_nil
171
+
172
+ a.birth
173
+
174
+ a.status_changed_at.should be_within(1).of(Time.zone.now)
175
+
176
+ previous_status_changed_at = a.status_changed_at
177
+
178
+ a.name = "new name"
179
+ a.save
180
+
181
+ a.status_changed_at.should eql(previous_status_changed_at)
182
+
183
+ end
184
+
168
185
  end
169
186
 
170
187
  describe 'aliasing' do
@@ -207,7 +224,7 @@ describe Stator::Model do
207
224
 
208
225
  it 'should allow for explicit constant and scope names to be provided' do
209
226
  User.should respond_to(:luke_warmers)
210
- defined?(User::LUKE_WARMERS).should be_true
227
+ (!!defined?(User::LUKE_WARMERS)).should eql(true)
211
228
  u = User.new
212
229
  u.should respond_to(:luke_warm?)
213
230
  end
@@ -215,7 +232,7 @@ describe Stator::Model do
215
232
  it 'should not create constants or scopes by default' do
216
233
  u = User.new
217
234
  u.should respond_to(:iced_tea?)
218
- defined?(User::ICED_TEA_STATES).should be_false
235
+ (!!defined?(User::ICED_TEA_STATES)).should eql(false)
219
236
  User.should_not respond_to(:iced_tea)
220
237
  end
221
238
  end
@@ -16,6 +16,7 @@ ActiveRecord::Schema.define(:version => 20130628161227) do
16
16
  t.string "status", :default => 'unborn'
17
17
  t.datetime "created_at", :null => false
18
18
  t.datetime "updated_at", :null => false
19
+ t.datetime "status_changed_at"
19
20
  t.datetime "unborn_status_at"
20
21
  t.datetime "born_status_at"
21
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-31 00:00:00.000000000 Z
12
+ date: 2014-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord