statesman 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +388 -0
  3. data/.rubocop.yml +13 -1
  4. data/.rubocop_todo.yml +17 -0
  5. data/CHANGELOG.md +6 -0
  6. data/CONTRIBUTING.md +0 -7
  7. data/Gemfile +1 -1
  8. data/Guardfile +2 -2
  9. data/LICENSE.txt +1 -1
  10. data/README.md +38 -15
  11. data/Rakefile +9 -1
  12. data/lib/generators/statesman/active_record_transition_generator.rb +0 -4
  13. data/lib/generators/statesman/generator_helpers.rb +4 -0
  14. data/lib/generators/statesman/templates/active_record_transition_model.rb.erb +2 -2
  15. data/lib/generators/statesman/templates/create_migration.rb.erb +2 -2
  16. data/lib/generators/statesman/templates/update_migration.rb.erb +2 -2
  17. data/lib/statesman.rb +2 -1
  18. data/lib/statesman/adapters/active_record.rb +2 -2
  19. data/lib/statesman/config.rb +0 -1
  20. data/lib/statesman/machine.rb +3 -2
  21. data/lib/statesman/utils.rb +15 -0
  22. data/lib/statesman/version.rb +1 -1
  23. data/spec/generators/statesman/active_record_transition_generator_spec.rb +4 -4
  24. data/spec/generators/statesman/migration_generator_spec.rb +1 -1
  25. data/spec/generators/statesman/mongoid_transition_generator_spec.rb +2 -2
  26. data/spec/spec_helper.rb +2 -2
  27. data/spec/statesman/adapters/active_record_queries_spec.rb +2 -2
  28. data/spec/statesman/adapters/active_record_spec.rb +8 -1
  29. data/spec/statesman/callback_spec.rb +1 -1
  30. data/spec/statesman/machine_spec.rb +11 -11
  31. data/spec/statesman/utils_spec.rb +51 -0
  32. data/spec/support/active_record.rb +31 -22
  33. data/spec/support/generators_shared_examples.rb +2 -2
  34. data/statesman.gemspec +17 -14
  35. metadata +31 -14
  36. data/.travis.yml +0 -37
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ describe Statesman::Utils do
4
+ describe ".rails_major_version" do
5
+ subject { described_class.rails_major_version }
6
+
7
+ context "for Rails 4" do
8
+ before { allow(Rails).to receive(:version).and_return("4.1.2") }
9
+
10
+ it { is_expected.to eq(4) }
11
+ end
12
+
13
+ context "for Rails 5" do
14
+ before { allow(Rails).to receive(:version).and_return("5.0.0") }
15
+
16
+ it { is_expected.to eq(5) }
17
+ end
18
+ end
19
+
20
+ describe ".rails_5_or_higher?" do
21
+ subject { described_class.rails_5_or_higher? }
22
+
23
+ context "for a pre-Rails 5 Rails version" do
24
+ before { allow(Rails).to receive(:version).and_return("4.1.2") }
25
+
26
+ it { is_expected.to be(false) }
27
+ end
28
+
29
+ context "for Rails 5 or a later version" do
30
+ before { allow(Rails).to receive(:version).and_return("5.0.0") }
31
+
32
+ it { is_expected.to be(true) }
33
+ end
34
+ end
35
+
36
+ describe ".rails_4_or_higher?" do
37
+ subject { described_class.rails_4_or_higher? }
38
+
39
+ context "for a pre-Rails 4 Rails version" do
40
+ before { allow(Rails).to receive(:version).and_return("3.0.0") }
41
+
42
+ it { is_expected.to be(false) }
43
+ end
44
+
45
+ context "for Rails 4 or a later version" do
46
+ before { allow(Rails).to receive(:version).and_return("4.1.2") }
47
+
48
+ it { is_expected.to be(true) }
49
+ end
50
+ end
51
+ end
@@ -1,6 +1,12 @@
1
- require "support/active_record"
2
1
  require "json"
3
2
 
3
+ MIGRATION_CLASS = if Rails.version.split(".").map(&:to_i).first >= 5
4
+ migration_version = ActiveRecord::Migration.current_version
5
+ ActiveRecord::Migration[migration_version]
6
+ else
7
+ ActiveRecord::Migration
8
+ end
9
+
4
10
  class MyStateMachine
5
11
  include Statesman::Machine
6
12
 
@@ -8,17 +14,18 @@ class MyStateMachine
8
14
  state :succeeded
9
15
  state :failed
10
16
 
11
- transition from: :initial, to: [:succeeded, :failed]
17
+ transition from: :initial, to: %i[succeeded failed]
12
18
  transition from: :failed, to: :initial
13
19
  end
14
20
 
15
21
  class MyActiveRecordModel < ActiveRecord::Base
16
22
  has_many :my_active_record_model_transitions, autosave: false
17
- alias_method :transitions, :my_active_record_model_transitions
23
+ alias transitions my_active_record_model_transitions
18
24
 
19
25
  def state_machine
20
26
  @state_machine ||= MyStateMachine.new(
21
- self, transition_class: MyActiveRecordModelTransition)
27
+ self, transition_class: MyActiveRecordModelTransition
28
+ )
22
29
  end
23
30
 
24
31
  def metadata
@@ -33,7 +40,7 @@ class MyActiveRecordModelTransition < ActiveRecord::Base
33
40
  serialize :metadata, JSON
34
41
  end
35
42
 
36
- class CreateMyActiveRecordModelMigration < ActiveRecord::Migration
43
+ class CreateMyActiveRecordModelMigration < MIGRATION_CLASS
37
44
  def change
38
45
  create_table :my_active_record_models do |t|
39
46
  t.string :current_state
@@ -44,7 +51,7 @@ end
44
51
 
45
52
  # TODO: make this a module we can extend from the app? Or a generator?
46
53
  # rubocop:disable MethodLength
47
- class CreateMyActiveRecordModelTransitionMigration < ActiveRecord::Migration
54
+ class CreateMyActiveRecordModelTransitionMigration < MIGRATION_CLASS
48
55
  def change
49
56
  create_table :my_active_record_model_transitions do |t|
50
57
  t.string :to_state
@@ -68,19 +75,19 @@ class CreateMyActiveRecordModelTransitionMigration < ActiveRecord::Migration
68
75
  end
69
76
 
70
77
  add_index :my_active_record_model_transitions,
71
- [:my_active_record_model_id, :sort_key],
78
+ %i[my_active_record_model_id sort_key],
72
79
  unique: true, name: "sort_key_index"
73
80
 
74
81
  if Statesman::Adapters::ActiveRecord.database_supports_partial_indexes?
75
82
  add_index :my_active_record_model_transitions,
76
- [:my_active_record_model_id, :most_recent],
83
+ %i[my_active_record_model_id most_recent],
77
84
  unique: true,
78
85
  where: "most_recent",
79
86
  name: "index_my_active_record_model_transitions_"\
80
87
  "parent_most_recent"
81
88
  else
82
89
  add_index :my_active_record_model_transitions,
83
- [:my_active_record_model_id, :most_recent],
90
+ %i[my_active_record_model_id most_recent],
84
91
  unique: true,
85
92
  name: "index_my_active_record_model_transitions_"\
86
93
  "parent_most_recent"
@@ -91,11 +98,12 @@ end
91
98
 
92
99
  class OtherActiveRecordModel < ActiveRecord::Base
93
100
  has_many :other_active_record_model_transitions, autosave: false
94
- alias_method :transitions, :other_active_record_model_transitions
101
+ alias transitions other_active_record_model_transitions
95
102
 
96
103
  def state_machine
97
104
  @state_machine ||= MyStateMachine.new(
98
- self, transition_class: OtherActiveRecordModelTransition)
105
+ self, transition_class: OtherActiveRecordModelTransition
106
+ )
99
107
  end
100
108
 
101
109
  def metadata
@@ -110,7 +118,7 @@ class OtherActiveRecordModelTransition < ActiveRecord::Base
110
118
  serialize :metadata, JSON
111
119
  end
112
120
 
113
- class CreateOtherActiveRecordModelMigration < ActiveRecord::Migration
121
+ class CreateOtherActiveRecordModelMigration < MIGRATION_CLASS
114
122
  def change
115
123
  create_table :other_active_record_models do |t|
116
124
  t.string :current_state
@@ -121,7 +129,7 @@ class CreateOtherActiveRecordModelMigration < ActiveRecord::Migration
121
129
  end
122
130
 
123
131
  # rubocop:disable MethodLength
124
- class CreateOtherActiveRecordModelTransitionMigration < ActiveRecord::Migration
132
+ class CreateOtherActiveRecordModelTransitionMigration < MIGRATION_CLASS
125
133
  def change
126
134
  create_table :other_active_record_model_transitions do |t|
127
135
  t.string :to_state
@@ -145,19 +153,19 @@ class CreateOtherActiveRecordModelTransitionMigration < ActiveRecord::Migration
145
153
  end
146
154
 
147
155
  add_index :other_active_record_model_transitions,
148
- [:other_active_record_model_id, :sort_key],
156
+ %i[other_active_record_model_id sort_key],
149
157
  unique: true, name: "other_sort_key_index"
150
158
 
151
159
  if Statesman::Adapters::ActiveRecord.database_supports_partial_indexes?
152
160
  add_index :other_active_record_model_transitions,
153
- [:other_active_record_model_id, :most_recent],
161
+ %i[other_active_record_model_id most_recent],
154
162
  unique: true,
155
163
  where: "most_recent",
156
164
  name: "index_other_active_record_model_transitions_"\
157
165
  "parent_most_recent"
158
166
  else
159
167
  add_index :other_active_record_model_transitions,
160
- [:other_active_record_model_id, :most_recent],
168
+ %i[other_active_record_model_id most_recent],
161
169
  unique: true,
162
170
  name: "index_other_active_record_model_transitions_"\
163
171
  "parent_most_recent"
@@ -166,7 +174,7 @@ class CreateOtherActiveRecordModelTransitionMigration < ActiveRecord::Migration
166
174
  end
167
175
  # rubocop:enable MethodLength
168
176
 
169
- class DropMostRecentColumn < ActiveRecord::Migration
177
+ class DropMostRecentColumn < MIGRATION_CLASS
170
178
  def change
171
179
  remove_index :my_active_record_model_transitions,
172
180
  name: "index_my_active_record_model_transitions_"\
@@ -188,7 +196,8 @@ module MyNamespace
188
196
  def state_machine
189
197
  @state_machine ||= MyStateMachine.new(
190
198
  self, transition_class: MyNamespace::MyActiveRecordModelTransition,
191
- association_name: :my_active_record_model_transitions)
199
+ association_name: :my_active_record_model_transitions
200
+ )
192
201
  end
193
202
 
194
203
  def metadata
@@ -207,7 +216,7 @@ module MyNamespace
207
216
  end
208
217
  end
209
218
 
210
- class CreateNamespacedARModelMigration < ActiveRecord::Migration
219
+ class CreateNamespacedARModelMigration < MIGRATION_CLASS
211
220
  def change
212
221
  create_table :my_namespace_my_active_record_models do |t|
213
222
  t.string :current_state
@@ -217,7 +226,7 @@ class CreateNamespacedARModelMigration < ActiveRecord::Migration
217
226
  end
218
227
 
219
228
  # rubocop:disable MethodLength
220
- class CreateNamespacedARModelTransitionMigration < ActiveRecord::Migration
229
+ class CreateNamespacedARModelTransitionMigration < MIGRATION_CLASS
221
230
  def change
222
231
  create_table :my_namespace_my_active_record_model_transitions do |t|
223
232
  t.string :to_state
@@ -245,14 +254,14 @@ class CreateNamespacedARModelTransitionMigration < ActiveRecord::Migration
245
254
 
246
255
  if Statesman::Adapters::ActiveRecord.database_supports_partial_indexes?
247
256
  add_index :my_namespace_my_active_record_model_transitions,
248
- [:my_active_record_model_id, :most_recent],
257
+ %i[my_active_record_model_id most_recent],
249
258
  unique: true,
250
259
  where: "most_recent",
251
260
  name: "index_namespace_model_transitions_"\
252
261
  "parent_most_recent"
253
262
  else
254
263
  add_index :my_namespace_my_active_record_model_transitions,
255
- [:my_active_record_model_id, :most_recent],
264
+ %i[my_active_record_model_id most_recent],
256
265
  unique: true,
257
266
  name: "index_namespace_model_transitions_"\
258
267
  "parent_most_recent"
@@ -7,7 +7,7 @@ TMP_GENERATOR_PATH = File.expand_path('../generator-tmp', __FILE__)
7
7
  shared_examples 'a generator' do
8
8
  destination TMP_GENERATOR_PATH
9
9
  before { prepare_destination }
10
- let(:gen) { generator %w(Yummy::Bacon Yummy::BaconTransition) }
10
+ let(:gen) { generator %w[Yummy::Bacon Yummy::BaconTransition] }
11
11
 
12
12
  it 'invokes create_model_file method' do
13
13
  expect(gen).to receive(:create_model_file)
@@ -15,7 +15,7 @@ shared_examples 'a generator' do
15
15
  end
16
16
 
17
17
  describe 'it runs the generator and check things out' do
18
- before { run_generator %w(Yummy::Bacon Yummy::BaconTransition) }
18
+ before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }
19
19
 
20
20
  describe 'it generates a correctly named file' do
21
21
  subject { file(migration_name) }
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'statesman/version'
@@ -6,28 +7,30 @@ require 'statesman/version'
6
7
  Gem::Specification.new do |spec|
7
8
  spec.name = "statesman"
8
9
  spec.version = Statesman::VERSION
9
- spec.authors = ["Harry Marr", "Andy Appleton"]
10
+ spec.authors = ["GoCardless"]
10
11
  spec.email = ["developers@gocardless.com"]
11
- spec.description = %q{A statesmanlike state machine library}
12
+ spec.description = 'A statesman-like state machine library'
12
13
  spec.summary = spec.description
13
14
  spec.homepage = "https://github.com/gocardless/statesman"
14
15
  spec.license = "MIT"
15
16
 
16
- spec.files = `git ls-files`.split($/)
17
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
20
  spec.require_paths = ["lib"]
20
21
 
21
22
  spec.required_ruby_version = '>= 2.2'
22
- spec.add_development_dependency "bundler", "~> 1.3"
23
- spec.add_development_dependency "rake"
24
- spec.add_development_dependency "rspec", "~> 3.1"
25
- spec.add_development_dependency "rspec-rails", "~> 3.1"
26
- spec.add_development_dependency "rspec-its", "~> 1.1"
27
- spec.add_development_dependency "rubocop", "~> 0.30.0"
28
- spec.add_development_dependency "sqlite3", "~> 1.3"
29
- spec.add_development_dependency "rails", ">= 3.2"
30
- spec.add_development_dependency "pg", "~> 0.18"
31
- spec.add_development_dependency "mysql2", "~> 0.4"
32
- spec.add_development_dependency "ammeter", "~> 1.1"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake", "~> 12.0.0"
26
+ spec.add_development_dependency "rspec", "~> 3.1"
27
+ spec.add_development_dependency "rspec-rails", "~> 3.1"
28
+ spec.add_development_dependency "rspec-its", "~> 1.1"
29
+ spec.add_development_dependency "rubocop", "~> 0.49.1"
30
+ spec.add_development_dependency "sqlite3", "~> 1.3"
31
+ spec.add_development_dependency "rails", ">= 3.2"
32
+ spec.add_development_dependency "pg", "~> 0.18"
33
+ spec.add_development_dependency "mysql2", "~> 0.4"
34
+ spec.add_development_dependency "ammeter", "~> 1.1"
35
+ spec.add_development_dependency "rspec_junit_formatter", "~> 0.3.0"
33
36
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statesman
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Harry Marr
8
- - Andy Appleton
7
+ - GoCardless
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2017-07-03 00:00:00.000000000 Z
11
+ date: 2017-09-01 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
@@ -29,16 +28,16 @@ dependencies:
29
28
  name: rake
30
29
  requirement: !ruby/object:Gem::Requirement
31
30
  requirements:
32
- - - ">="
31
+ - - "~>"
33
32
  - !ruby/object:Gem::Version
34
- version: '0'
33
+ version: 12.0.0
35
34
  type: :development
36
35
  prerelease: false
37
36
  version_requirements: !ruby/object:Gem::Requirement
38
37
  requirements:
39
- - - ">="
38
+ - - "~>"
40
39
  - !ruby/object:Gem::Version
41
- version: '0'
40
+ version: 12.0.0
42
41
  - !ruby/object:Gem::Dependency
43
42
  name: rspec
44
43
  requirement: !ruby/object:Gem::Requirement
@@ -87,14 +86,14 @@ dependencies:
87
86
  requirements:
88
87
  - - "~>"
89
88
  - !ruby/object:Gem::Version
90
- version: 0.30.0
89
+ version: 0.49.1
91
90
  type: :development
92
91
  prerelease: false
93
92
  version_requirements: !ruby/object:Gem::Requirement
94
93
  requirements:
95
94
  - - "~>"
96
95
  - !ruby/object:Gem::Version
97
- version: 0.30.0
96
+ version: 0.49.1
98
97
  - !ruby/object:Gem::Dependency
99
98
  name: sqlite3
100
99
  requirement: !ruby/object:Gem::Requirement
@@ -165,16 +164,31 @@ dependencies:
165
164
  - - "~>"
166
165
  - !ruby/object:Gem::Version
167
166
  version: '1.1'
168
- description: A statesmanlike state machine library
167
+ - !ruby/object:Gem::Dependency
168
+ name: rspec_junit_formatter
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.3.0
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.3.0
181
+ description: A statesman-like state machine library
169
182
  email:
170
183
  - developers@gocardless.com
171
184
  executables: []
172
185
  extensions: []
173
186
  extra_rdoc_files: []
174
187
  files:
188
+ - ".circleci/config.yml"
175
189
  - ".gitignore"
176
190
  - ".rubocop.yml"
177
- - ".travis.yml"
191
+ - ".rubocop_todo.yml"
178
192
  - CHANGELOG.md
179
193
  - CONTRIBUTING.md
180
194
  - Gemfile
@@ -205,6 +219,7 @@ files:
205
219
  - lib/statesman/guard.rb
206
220
  - lib/statesman/machine.rb
207
221
  - lib/statesman/railtie.rb
222
+ - lib/statesman/utils.rb
208
223
  - lib/statesman/version.rb
209
224
  - lib/tasks/statesman.rake
210
225
  - spec/fixtures/add_constraints_to_most_recent_for_bacon_transitions_with_partial_index.rb
@@ -225,6 +240,7 @@ files:
225
240
  - spec/statesman/guard_spec.rb
226
241
  - spec/statesman/machine_spec.rb
227
242
  - spec/statesman/transition_spec.rb
243
+ - spec/statesman/utils_spec.rb
228
244
  - spec/support/active_record.rb
229
245
  - spec/support/generators_shared_examples.rb
230
246
  - spec/support/mongoid.rb
@@ -249,10 +265,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
265
  version: '0'
250
266
  requirements: []
251
267
  rubyforge_project:
252
- rubygems_version: 2.6.11
268
+ rubygems_version: 2.6.13
253
269
  signing_key:
254
270
  specification_version: 4
255
- summary: A statesmanlike state machine library
271
+ summary: A statesman-like state machine library
256
272
  test_files:
257
273
  - spec/fixtures/add_constraints_to_most_recent_for_bacon_transitions_with_partial_index.rb
258
274
  - spec/fixtures/add_constraints_to_most_recent_for_bacon_transitions_without_partial_index.rb
@@ -272,6 +288,7 @@ test_files:
272
288
  - spec/statesman/guard_spec.rb
273
289
  - spec/statesman/machine_spec.rb
274
290
  - spec/statesman/transition_spec.rb
291
+ - spec/statesman/utils_spec.rb
275
292
  - spec/support/active_record.rb
276
293
  - spec/support/generators_shared_examples.rb
277
294
  - spec/support/mongoid.rb
@@ -1,37 +0,0 @@
1
- language: ruby
2
-
3
- cache: bundler
4
-
5
- rvm:
6
- - 2.4.1
7
- - 2.3.3
8
- - 2.2.6
9
-
10
- sudo: false
11
-
12
- services: mongodb
13
-
14
- before_install:
15
- - gem update --system
16
- - gem install bundler
17
-
18
- branches:
19
- only:
20
- - "master"
21
-
22
- before_script:
23
- - mysql -e 'CREATE DATABASE statesman_test;'
24
- - psql -c 'CREATE DATABASE statesman_test;' -U postgres
25
-
26
- script:
27
- - bundle exec rubocop
28
- - bundle exec rake
29
-
30
- env:
31
- - "RAILS_VERSION=4.2.8"
32
- - "RAILS_VERSION=4.2.8 DATABASE_URL=mysql2://root@localhost/statesman_test"
33
- - "RAILS_VERSION=4.2.8 DATABASE_URL=postgres://postgres@localhost/statesman_test"
34
- - "RAILS_VERSION=5.0.0 EXCLUDE_MONGOID=true"
35
-
36
- matrix:
37
- exclude: []