state_machines-audit_trail 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f76b02dab1bbb82a2ba360cbc86137642fc3874
4
- data.tar.gz: a40d8ce4c4f7166f9660669d8a603eb8da9d9e40
3
+ metadata.gz: b2be69a3ef20ae821d26c9cc330e19fe8d94d5a2
4
+ data.tar.gz: a4b55d4b850acf875c7489c1505c3c99152111a1
5
5
  SHA512:
6
- metadata.gz: 9818ab2f5f3146c6444645cc6a92c331c069d842b00ff309bc8c4e8ff61fd09a396e0b5240ef43d6686fcdc8e42cc4dd23e6c8f1814ccd47efa3c2fb20aa3915
7
- data.tar.gz: e85f0fee66e5323f67e1ffb4d8bb9cd42313d3e5e33ab846fc43a9b0b8bbcf79205c8cc6f2738d633ae450a4e11bf750de24a9a78d11cbc27ce0bc331f5640f3
6
+ metadata.gz: 0d89506285ebdd527499b689a4bc9d08e7996ceaed99f4cb707941e8134a086f998b73d50f414702afbbd15abfa669dca1b24446da2fef56e647792bb971b9a1
7
+ data.tar.gz: f7e486a00886044b9763ad293d27c4fce800c52e968e0b689b431b23b3b3d321050a1c229921f8ab55ac17531c48db356734c0ee010719d9a42bdbc15f3f8754
data/README.md CHANGED
@@ -97,7 +97,7 @@ In order to utilize this feature, you need to:
97
97
  #### Example 1 - Store a single attribute value
98
98
  Store `Subscription` `field1` in `Transition` field `field1`:
99
99
  ```ruby
100
- audit_trail :context: :field1
100
+ audit_trail context: :field1
101
101
  ```
102
102
 
103
103
  #### Example 2 - Store multiple attribute values
@@ -106,7 +106,25 @@ Store `Subscription` `field1` and `field2` in `Transition` fields `field1` and `
106
106
  audit_trail context: [:field1, :field2]
107
107
  ```
108
108
 
109
- #### Example 3 - Store simple method results
109
+ #### Example 3 - Store multiple values from a single context object
110
+ Store `Subscription` `user` in `Transition` fields `user_id` and `user_name`:
111
+ ```ruby
112
+ class Subscription < ActiveRecord::Base
113
+ state_machines :state, initial: :start do
114
+ audit_trail context: :user
115
+ ...
116
+ end
117
+ end
118
+
119
+ class SubscriptionStateTransition < ActiveRecord::Base
120
+ def user=(u)
121
+ self.user_id = u.id
122
+ self.user_name = u.name
123
+ end
124
+ end
125
+ ```
126
+
127
+ #### Example 4 - Store simple method results
110
128
  Store simple method results.
111
129
 
112
130
  Sometimes it can be useful to store dynamically computed information, such as those from a `Subscription` method `#plan_time_remaining`
@@ -123,7 +141,7 @@ class Subscription < ActiveRecord::Base
123
141
  ...
124
142
  ```
125
143
 
126
- #### Example 4 - Store advanced method results
144
+ #### Example 5 - Store advanced method results
127
145
  Store method results that interrogate the transition for information such as `event` arguments:
128
146
 
129
147
  ```ruby
@@ -48,6 +48,11 @@ class StateMachines::AuditTrail::Backend < Struct.new(:transition_class, :owner_
48
48
  end
49
49
  end
50
50
 
51
+ # Exists in case ORM layer has a different way of answering this question, but works for most.
52
+ def new_record?(object)
53
+ object.new_record?
54
+ end
55
+
51
56
  protected
52
57
 
53
58
  def persist(object, fields)
@@ -30,9 +30,11 @@ module StateMachines::AuditTrail::TransitionAuditing
30
30
  unless state_machine.action == nil
31
31
  # Log the initial transition from null => initial (upon object instantiation)
32
32
  state_machine.owner_class.after_initialize do |object|
33
- current_state = object.send(state_machine.attribute)
34
- if !current_state.nil?
35
- state_machine.backend.log(object, OpenStruct.new(namespace: state_machine.namespace, to: current_state))
33
+ if state_machine.backend.new_record? object
34
+ current_state = object.send(state_machine.attribute)
35
+ if !current_state.nil?
36
+ state_machine.backend.log(object, OpenStruct.new(namespace: state_machine.namespace, to: current_state))
37
+ end
36
38
  end
37
39
  end
38
40
  end
@@ -1,5 +1,5 @@
1
1
  module StateMachines
2
2
  module AuditTrail
3
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
4
4
  end
5
5
  end
@@ -21,9 +21,7 @@ describe StateMachines::AuditTrail::Backend::ActiveRecord do
21
21
  expect(target.new_record?).to be_falsey
22
22
  expect(target.ar_model_state_transitions.count).to eq 1
23
23
  state_transition = target.ar_model_state_transitions.first
24
- expect(state_transition.from).to be_nil
25
- expect(state_transition.to).to eq 'waiting'
26
- expect(state_transition.event).to be_nil
24
+ assert_transition state_transition, nil, nil, 'waiting'
27
25
  end
28
26
 
29
27
  it 'create object' do
@@ -32,9 +30,13 @@ describe StateMachines::AuditTrail::Backend::ActiveRecord do
32
30
  expect(target.new_record?).to be_falsey
33
31
  expect(target.ar_model_state_transitions.count).to eq 1
34
32
  state_transition = target.ar_model_state_transitions.first
35
- expect(state_transition.from).to be_nil
36
- expect(state_transition.to).to eq 'waiting'
37
- expect(state_transition.event).to be_nil
33
+ assert_transition state_transition, nil, nil, 'waiting'
34
+
35
+ # ensure we don't have a second initial state transition logged (issue #4)
36
+ target = target.reload()
37
+ expect(target.ar_model_state_transitions.count).to eq 1
38
+ state_transition = target.ar_model_state_transitions.first
39
+ assert_transition state_transition, nil, nil, 'waiting'
38
40
  end
39
41
  end
40
42
 
@@ -261,4 +263,13 @@ describe StateMachines::AuditTrail::Backend::ActiveRecord do
261
263
  expect { m.complete! }.not_to raise_error
262
264
  end
263
265
  end
266
+
267
+ private
268
+
269
+ def assert_transition(state_transition, event, from, to)
270
+ # expect(state_transition.namespace).to eq namespace
271
+ expect(state_transition.event).to eq event
272
+ expect(state_transition.from).to eq from
273
+ expect(state_transition.to).to eq to
274
+ end
264
275
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machines-audit_trail
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Ross
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-03-30 00:00:00.000000000 Z
13
+ date: 2015-04-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: state_machines