state_machines-audit_trail 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,98 @@
1
+ # reset integrations so that something like ActiveRecord is not loaded and conflicting
2
+ require 'state_machines'
3
+ StateMachines::Integrations.reset
4
+
5
+ require 'spec_helper'
6
+ require 'state_machines-mongoid'
7
+ require 'helpers/mongoid'
8
+
9
+ describe StateMachines::AuditTrail::Backend::Mongoid do
10
+
11
+ context '#create_for' do
12
+ it 'should create a Mongoid backend' do
13
+ backend = StateMachines::AuditTrail::Backend.create_for(MongoidTestModelStateTransition, MongoidTestModel)
14
+ expect(backend).to be_instance_of(StateMachines::AuditTrail::Backend::Mongoid)
15
+ end
16
+ end
17
+
18
+ context 'single state machine' do
19
+ let!(:target) { MongoidTestModel.create! }
20
+
21
+ it 'should populate all fields' do
22
+ target.start!
23
+ last_transition = MongoidTestModelStateTransition.where(:mongoid_test_model_id => target.id).last
24
+
25
+ expect(last_transition.event).to eq 'start'
26
+ expect(last_transition.from).to eq 'waiting'
27
+ expect(last_transition.to).to eq 'started'
28
+ expect(last_transition.created_at).to be_within(10.seconds).of(DateTime.now)
29
+ end
30
+
31
+ it 'should log multiple events' do
32
+ expect { target.start && target.stop && target.start }.to change(MongoidTestModelStateTransition, :count).by(3)
33
+ end
34
+
35
+ it 'do nothing on failed transition' do
36
+ expect { target.stop }.not_to change(MongoidTestModelStateTransition, :count)
37
+ end
38
+ end
39
+
40
+ context 'multiple state machines' do
41
+ let!(:target) { MongoidTestModelWithMultipleStateMachines.create! }
42
+
43
+ it 'should log a state transition for the affected state machine' do
44
+ expect { target.begin_first! }.to change(MongoidTestModelWithMultipleStateMachinesFirstTransition, :count).by(1)
45
+ end
46
+
47
+ it 'should not log a state transition for the unaffected state machine' do
48
+ expect { target.begin_first! }.not_to change(MongoidTestModelWithMultipleStateMachinesSecondTransition, :count)
49
+ end
50
+ end
51
+
52
+ context 'on an object with a state machine having an initial state' do
53
+ let(:target_class) { MongoidTestModelWithMultipleStateMachines }
54
+ let(:state_transition_class) { MongoidTestModelWithMultipleStateMachinesFirstTransition }
55
+
56
+ it 'should log a state transition for the inital state' do
57
+ expect { target_class.create! }.to change(state_transition_class, :count).by(1)
58
+ end
59
+
60
+ it 'should only set the :to state for the initial transition' do
61
+ target_class.create!
62
+ initial_transition = state_transition_class.last
63
+ expect(initial_transition.event).to be_nil
64
+ expect(initial_transition.from).to be_nil
65
+ expect(initial_transition.to).to eq 'beginning'
66
+ expect(initial_transition.created_at).to be_within(10.seconds).of(DateTime.now)
67
+ end
68
+ end
69
+
70
+ context 'on an object with a state machine not having an initial state' do
71
+ let(:target_class) { MongoidTestModelWithMultipleStateMachines }
72
+ let(:state_transition_class) { MongoidTestModelWithMultipleStateMachinesSecondTransition }
73
+
74
+ it 'should not log a transition when the object is created' do
75
+ expect { target_class.create! }.not_to change(state_transition_class, :count)
76
+ end
77
+
78
+ it 'should log a transition for the first event' do
79
+ expect { target_class.create.begin_second! }.to change(state_transition_class, :count).by(1)
80
+ end
81
+
82
+ it 'should not set a value for the :from state on the first transition' do
83
+ target_class.create.begin_second!
84
+ first_transition = state_transition_class.last
85
+ expect(first_transition.event).to eq 'begin_second'
86
+ expect(first_transition.from).to be_nil
87
+ expect(first_transition.to).to eq 'beginning_second'
88
+ expect(first_transition.created_at).to be_within(10.seconds).of(DateTime.now)
89
+ end
90
+ end
91
+
92
+ context 'on a class using STI' do
93
+ it 'should properly grab the class name from STI models' do
94
+ m = MongoidTestModelDescendant.create!
95
+ expect { m.start! }.not_to raise_error
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,54 @@
1
+ # FIXME: Unable to get this test to run, though the generator does work. Someone fix me please.
2
+ #
3
+ # require 'rails'
4
+ # require 'spec_helper'
5
+ # require 'state_machines-activerecord'
6
+ # require 'rails/generators/active_model'
7
+ # require 'state_machines/audit_trail_generator'
8
+ # require 'generator_spec'
9
+ #
10
+ # describe StateMachines::AuditTrailGenerator, type: :generator do
11
+ #
12
+ # destination File.expand_path('../../../../tmp', __FILE__)
13
+ # arguments %w(SomeNamespace::Subscription state)
14
+ #
15
+ # before(:all) do
16
+ # prepare_destination
17
+ # end
18
+ #
19
+ # # create db/migrate/20150326190913_create_some_namespace_subscription_state_transitions.rb
20
+ # # create app/models/some_namespace/subscription_state_transition.rb
21
+ # # create app/models/some_namespace.rb
22
+ # # invoke test_unit
23
+ # # create test/models/some_namespace/subscription_state_transition_test.rb
24
+ # # invoke factory_girl
25
+ # # create test/factories/af_core_subscription_state_transitions.rb
26
+ #
27
+ #
28
+ # specify do
29
+ # run_generator
30
+ # expect(destination_root).to have_structure {
31
+ # # no_file 'test.rb'
32
+ # directory 'app' do
33
+ # directory 'models' do
34
+ # file 'some_namespace.rb' do
35
+ # contains 'def self.table_name_prefix'
36
+ # end
37
+ # directory 'some_namespace' do
38
+ # file 'subscription_state_transition.rb' do
39
+ # contains 'class SomeNamespace::SubscriptionStateTransition'
40
+ # end
41
+ # end
42
+ # end
43
+ # end
44
+ # directory 'db' do
45
+ # directory 'migrate' do
46
+ # file '123_create_some_namespace_subscription_state_transitions.rb'
47
+ # migration 'create_tests' do
48
+ # contains 'class TestMigration'
49
+ # end
50
+ # end
51
+ # end
52
+ # }
53
+ # end
54
+ # end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe StateMachines::AuditTrail do
4
+
5
+ it 'should include the auditing module into StateMachines::Machine' do
6
+ expect(StateMachines::Machine.included_modules).to include(StateMachines::AuditTrail::TransitionAuditing)
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'rspec'
5
+ require 'state_machines/audit_trail'
6
+
7
+ RSpec.configure do |config|
8
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'state_machines/audit_trail/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'state_machines-audit_trail'
9
+ s.version = StateMachines::AuditTrail::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ['Kevin Ross', 'Willem van Bergen', 'Jesse Storimer']
12
+ s.email = ['kevin.ross@alienfast.com', 'willem@shopify.com', 'jesse@shopify.com']
13
+
14
+ s.homepage = 'https://github.com/state-machines/state_machines-audit_trail'
15
+ s.summary = %q{Log transitions on a state_machines to support auditing and business process analytics.}
16
+ s.description = %q{Log transitions on a state_machines to support auditing and business process analytics.}
17
+ s.license = 'MIT'
18
+
19
+ s.add_runtime_dependency('state_machines')
20
+
21
+ s.add_development_dependency('state_machines-activerecord')
22
+ s.add_development_dependency('state_machines-mongoid')
23
+ s.add_development_dependency('rake')
24
+ s.add_development_dependency('rspec', '>= 3.0.0')
25
+ s.add_development_dependency('activerecord', '>= 4.0.0')
26
+ s.add_development_dependency('sqlite3')
27
+ s.add_development_dependency('mongoid', '>= 4.0.0')
28
+ s.add_development_dependency('bson_ext')
29
+ s.add_development_dependency('generator_spec')
30
+ s.add_development_dependency('rails', '>= 4.0.0')
31
+
32
+ s.files = `git ls-files`.split($/).reject { |f| f =~ /^samples\// }
33
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
34
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
35
+ s.require_paths = ['lib']
36
+ end
metadata ADDED
@@ -0,0 +1,237 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state_machines-audit_trail
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Ross
8
+ - Willem van Bergen
9
+ - Jesse Storimer
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-03-30 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: state_machines
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: state_machines-activerecord
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: state_machines-mongoid
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 3.0.0
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 3.0.0
85
+ - !ruby/object:Gem::Dependency
86
+ name: activerecord
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 4.0.0
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 4.0.0
99
+ - !ruby/object:Gem::Dependency
100
+ name: sqlite3
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: mongoid
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 4.0.0
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 4.0.0
127
+ - !ruby/object:Gem::Dependency
128
+ name: bson_ext
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ - !ruby/object:Gem::Dependency
142
+ name: generator_spec
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ type: :development
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ - !ruby/object:Gem::Dependency
156
+ name: rails
157
+ requirement: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: 4.0.0
162
+ type: :development
163
+ prerelease: false
164
+ version_requirements: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: 4.0.0
169
+ description: Log transitions on a state_machines to support auditing and business
170
+ process analytics.
171
+ email:
172
+ - kevin.ross@alienfast.com
173
+ - willem@shopify.com
174
+ - jesse@shopify.com
175
+ executables: []
176
+ extensions: []
177
+ extra_rdoc_files: []
178
+ files:
179
+ - ".gitignore"
180
+ - ".rspec"
181
+ - ".travis.yml"
182
+ - Gemfile
183
+ - LICENSE
184
+ - README.md
185
+ - Rakefile
186
+ - lib/state_machines-audit_trail.rb
187
+ - lib/state_machines/audit_trail.rb
188
+ - lib/state_machines/audit_trail/backend.rb
189
+ - lib/state_machines/audit_trail/backend/active_record.rb
190
+ - lib/state_machines/audit_trail/backend/mongoid.rb
191
+ - lib/state_machines/audit_trail/railtie.rb
192
+ - lib/state_machines/audit_trail/transition_auditing.rb
193
+ - lib/state_machines/audit_trail/version.rb
194
+ - lib/state_machines/audit_trail_generator.rb
195
+ - spec/helpers/active_record.rb
196
+ - spec/helpers/mongoid.rb
197
+ - spec/helpers/mongoid.yml
198
+ - spec/lib/state_machines/audit_trail/backend/active_record_spec.rb
199
+ - spec/lib/state_machines/audit_trail/backend/mongoid_spec.rb
200
+ - spec/lib/state_machines/audit_trail_generator_spec.rb
201
+ - spec/lib/state_machines/audit_trail_spec.rb
202
+ - spec/spec_helper.rb
203
+ - state_machines-audit_trail.gemspec
204
+ homepage: https://github.com/state-machines/state_machines-audit_trail
205
+ licenses:
206
+ - MIT
207
+ metadata: {}
208
+ post_install_message:
209
+ rdoc_options: []
210
+ require_paths:
211
+ - lib
212
+ required_ruby_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ required_rubygems_version: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - ">="
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ requirements: []
223
+ rubyforge_project:
224
+ rubygems_version: 2.4.5
225
+ signing_key:
226
+ specification_version: 4
227
+ summary: Log transitions on a state_machines to support auditing and business process
228
+ analytics.
229
+ test_files:
230
+ - spec/helpers/active_record.rb
231
+ - spec/helpers/mongoid.rb
232
+ - spec/helpers/mongoid.yml
233
+ - spec/lib/state_machines/audit_trail/backend/active_record_spec.rb
234
+ - spec/lib/state_machines/audit_trail/backend/mongoid_spec.rb
235
+ - spec/lib/state_machines/audit_trail_generator_spec.rb
236
+ - spec/lib/state_machines/audit_trail_spec.rb
237
+ - spec/spec_helper.rb