statesman 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +7 -1
  3. data/.travis.yml +19 -4
  4. data/CHANGELOG.md +30 -0
  5. data/README.md +11 -35
  6. data/lib/generators/statesman/add_constraints_to_most_recent_generator.rb +28 -0
  7. data/lib/generators/statesman/add_most_recent_generator.rb +25 -0
  8. data/lib/generators/statesman/generator_helpers.rb +6 -2
  9. data/lib/generators/statesman/templates/add_constraints_to_most_recent_migration.rb.erb +13 -0
  10. data/lib/generators/statesman/templates/add_most_recent_migration.rb.erb +9 -0
  11. data/lib/generators/statesman/templates/create_migration.rb.erb +4 -3
  12. data/lib/generators/statesman/templates/update_migration.rb.erb +5 -4
  13. data/lib/statesman.rb +1 -0
  14. data/lib/statesman/adapters/active_record.rb +23 -8
  15. data/lib/statesman/adapters/active_record_queries.rb +62 -15
  16. data/lib/statesman/railtie.rb +9 -0
  17. data/lib/statesman/version.rb +1 -1
  18. data/lib/tasks/statesman.rake +49 -0
  19. data/spec/fixtures/add_constraints_to_most_recent_for_bacon_transitions.rb +13 -0
  20. data/spec/fixtures/add_most_recent_to_bacon_transitions.rb +9 -0
  21. data/spec/generators/statesman/active_record_transition_generator_spec.rb +0 -2
  22. data/spec/generators/statesman/add_constraints_to_most_recent_generator_spec.rb +38 -0
  23. data/spec/generators/statesman/add_most_recent_generator_spec.rb +35 -0
  24. data/spec/generators/statesman/migration_generator_spec.rb +10 -1
  25. data/spec/generators/statesman/mongoid_transition_generator_spec.rb +0 -2
  26. data/spec/spec_helper.rb +22 -7
  27. data/spec/statesman/adapters/active_record_queries_spec.rb +110 -28
  28. data/spec/statesman/adapters/active_record_spec.rb +61 -31
  29. data/spec/statesman/adapters/mongoid_spec.rb +8 -17
  30. data/spec/statesman/adapters/shared_examples.rb +10 -17
  31. data/spec/statesman/callback_spec.rb +2 -6
  32. data/spec/statesman/config_spec.rb +2 -5
  33. data/spec/statesman/guard_spec.rb +3 -9
  34. data/spec/statesman/machine_spec.rb +91 -129
  35. data/spec/support/active_record.rb +35 -4
  36. data/spec/support/generators_shared_examples.rb +1 -4
  37. data/statesman.gemspec +5 -3
  38. metadata +52 -10
@@ -1,8 +1,6 @@
1
1
  require "support/active_record"
2
2
  require "json"
3
3
 
4
- DB = Pathname.new("test.sqlite3")
5
-
6
4
  class MyStateMachine
7
5
  include Statesman::Machine
8
6
 
@@ -16,14 +14,21 @@ end
16
14
 
17
15
  class MyActiveRecordModel < ActiveRecord::Base
18
16
  has_many :my_active_record_model_transitions
17
+ alias_method :transitions, :my_active_record_model_transitions
19
18
 
20
19
  def state_machine
21
20
  @state_machine ||= MyStateMachine.new(
22
21
  self, transition_class: MyActiveRecordModelTransition)
23
22
  end
23
+
24
+ def metadata
25
+ super || {}
26
+ end
24
27
  end
25
28
 
26
29
  class MyActiveRecordModelTransition < ActiveRecord::Base
30
+ include Statesman::Adapters::ActiveRecordTransition
31
+
27
32
  belongs_to :my_active_record_model
28
33
  serialize :metadata, JSON
29
34
  end
@@ -38,16 +43,42 @@ class CreateMyActiveRecordModelMigration < ActiveRecord::Migration
38
43
  end
39
44
 
40
45
  # TODO: make this a module we can extend from the app? Or a generator?
46
+ # rubocop:disable MethodLength
41
47
  class CreateMyActiveRecordModelTransitionMigration < ActiveRecord::Migration
42
48
  def change
43
49
  create_table :my_active_record_model_transitions do |t|
44
50
  t.string :to_state
45
51
  t.integer :my_active_record_model_id
46
52
  t.integer :sort_key
47
- t.text :metadata
53
+ t.boolean :most_recent, default: true, null: false
54
+
55
+ # MySQL doesn't allow default values on text fields
56
+ if ActiveRecord::Base.connection.adapter_name == 'Mysql2'
57
+ t.text :metadata
58
+ else
59
+ t.text :metadata, default: '{}'
60
+ end
61
+
48
62
  t.timestamps(null: false)
49
63
  end
50
64
 
51
- add_index :my_active_record_model_transitions, :sort_key, unique: true
65
+ add_index :my_active_record_model_transitions,
66
+ [:my_active_record_model_id, :sort_key],
67
+ unique: true, name: "sort_key_index"
68
+ add_index :my_active_record_model_transitions,
69
+ [:my_active_record_model_id, :most_recent],
70
+ unique: true, where: "most_recent",
71
+ name: "index_my_active_record_model_transitions_"\
72
+ "parent_most_recent"
73
+ end
74
+ end
75
+ # rubocop:enable MethodLength
76
+
77
+ class DropMostRecentColumn < ActiveRecord::Migration
78
+ def change
79
+ remove_index :my_active_record_model_transitions,
80
+ name: "index_my_active_record_model_transitions_"\
81
+ "parent_most_recent"
82
+ remove_column :my_active_record_model_transitions, :most_recent
52
83
  end
53
84
  end
@@ -10,20 +10,17 @@ shared_examples 'a generator' do
10
10
 
11
11
  it 'invokes create_model_file method' do
12
12
  expect(gen).to receive(:create_model_file)
13
- capture(:stdout) { gen.invoke_all }
13
+ gen.invoke_all
14
14
  end
15
15
 
16
16
  describe 'it runs the generator and check things out' do
17
-
18
17
  before { run_generator %w(Yummy::Bacon Yummy::BaconTransition) }
19
18
 
20
19
  describe 'it generates a correctly named file' do
21
20
  subject { file(migration_name) }
22
21
  it { is_expected.to be_a_migration }
23
22
  end
24
-
25
23
  end
26
-
27
24
  end
28
25
 
29
26
  RSpec.configure do |config|
data/statesman.gemspec CHANGED
@@ -23,10 +23,12 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec", "~> 3.1"
24
24
  spec.add_development_dependency "rspec-its", "~> 1.1"
25
25
  spec.add_development_dependency "guard-rspec", "~> 4.3"
26
- spec.add_development_dependency "rubocop", "~> 0.27"
26
+ spec.add_development_dependency "rubocop", "~> 0.29.1"
27
27
  spec.add_development_dependency "guard-rubocop", "~> 1.2"
28
28
  spec.add_development_dependency "sqlite3", "~> 1.3"
29
- spec.add_development_dependency "mongoid", "~> 4.0"
30
- spec.add_development_dependency "activerecord", "~> 4.1"
29
+ spec.add_development_dependency "mongoid", ">= 3.1"
30
+ spec.add_development_dependency "activerecord", ">= 3.2"
31
+ spec.add_development_dependency "pg", "~> 0.18"
32
+ spec.add_development_dependency "mysql2", "~> 0.3"
31
33
  spec.add_development_dependency "ammeter", "~> 1.1"
32
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statesman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Marr
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-09 00:00:00.000000000 Z
12
+ date: 2015-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -87,14 +87,14 @@ dependencies:
87
87
  requirements:
88
88
  - - ~>
89
89
  - !ruby/object:Gem::Version
90
- version: '0.27'
90
+ version: 0.29.1
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ~>
96
96
  - !ruby/object:Gem::Version
97
- version: '0.27'
97
+ version: 0.29.1
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: guard-rubocop
100
100
  requirement: !ruby/object:Gem::Requirement
@@ -125,32 +125,60 @@ dependencies:
125
125
  version: '1.3'
126
126
  - !ruby/object:Gem::Dependency
127
127
  name: mongoid
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '3.1'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '3.1'
140
+ - !ruby/object:Gem::Dependency
141
+ name: activerecord
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '3.2'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '3.2'
154
+ - !ruby/object:Gem::Dependency
155
+ name: pg
128
156
  requirement: !ruby/object:Gem::Requirement
129
157
  requirements:
130
158
  - - ~>
131
159
  - !ruby/object:Gem::Version
132
- version: '4.0'
160
+ version: '0.18'
133
161
  type: :development
134
162
  prerelease: false
135
163
  version_requirements: !ruby/object:Gem::Requirement
136
164
  requirements:
137
165
  - - ~>
138
166
  - !ruby/object:Gem::Version
139
- version: '4.0'
167
+ version: '0.18'
140
168
  - !ruby/object:Gem::Dependency
141
- name: activerecord
169
+ name: mysql2
142
170
  requirement: !ruby/object:Gem::Requirement
143
171
  requirements:
144
172
  - - ~>
145
173
  - !ruby/object:Gem::Version
146
- version: '4.1'
174
+ version: '0.3'
147
175
  type: :development
148
176
  prerelease: false
149
177
  version_requirements: !ruby/object:Gem::Requirement
150
178
  requirements:
151
179
  - - ~>
152
180
  - !ruby/object:Gem::Version
153
- version: '4.1'
181
+ version: '0.3'
154
182
  - !ruby/object:Gem::Dependency
155
183
  name: ammeter
156
184
  requirement: !ruby/object:Gem::Requirement
@@ -184,10 +212,14 @@ files:
184
212
  - Rakefile
185
213
  - circle.yml
186
214
  - lib/generators/statesman/active_record_transition_generator.rb
215
+ - lib/generators/statesman/add_constraints_to_most_recent_generator.rb
216
+ - lib/generators/statesman/add_most_recent_generator.rb
187
217
  - lib/generators/statesman/generator_helpers.rb
188
218
  - lib/generators/statesman/migration_generator.rb
189
219
  - lib/generators/statesman/mongoid_transition_generator.rb
190
220
  - lib/generators/statesman/templates/active_record_transition_model.rb.erb
221
+ - lib/generators/statesman/templates/add_constraints_to_most_recent_migration.rb.erb
222
+ - lib/generators/statesman/templates/add_most_recent_migration.rb.erb
191
223
  - lib/generators/statesman/templates/create_migration.rb.erb
192
224
  - lib/generators/statesman/templates/mongoid_transition_model.rb.erb
193
225
  - lib/generators/statesman/templates/update_migration.rb.erb
@@ -205,8 +237,14 @@ files:
205
237
  - lib/statesman/exceptions.rb
206
238
  - lib/statesman/guard.rb
207
239
  - lib/statesman/machine.rb
240
+ - lib/statesman/railtie.rb
208
241
  - lib/statesman/version.rb
242
+ - lib/tasks/statesman.rake
243
+ - spec/fixtures/add_constraints_to_most_recent_for_bacon_transitions.rb
244
+ - spec/fixtures/add_most_recent_to_bacon_transitions.rb
209
245
  - spec/generators/statesman/active_record_transition_generator_spec.rb
246
+ - spec/generators/statesman/add_constraints_to_most_recent_generator_spec.rb
247
+ - spec/generators/statesman/add_most_recent_generator_spec.rb
210
248
  - spec/generators/statesman/migration_generator_spec.rb
211
249
  - spec/generators/statesman/mongoid_transition_generator_spec.rb
212
250
  - spec/spec_helper.rb
@@ -245,12 +283,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
245
283
  version: '0'
246
284
  requirements: []
247
285
  rubyforge_project:
248
- rubygems_version: 2.4.1
286
+ rubygems_version: 2.4.2
249
287
  signing_key:
250
288
  specification_version: 4
251
289
  summary: A statesmanlike state machine library
252
290
  test_files:
291
+ - spec/fixtures/add_constraints_to_most_recent_for_bacon_transitions.rb
292
+ - spec/fixtures/add_most_recent_to_bacon_transitions.rb
253
293
  - spec/generators/statesman/active_record_transition_generator_spec.rb
294
+ - spec/generators/statesman/add_constraints_to_most_recent_generator_spec.rb
295
+ - spec/generators/statesman/add_most_recent_generator_spec.rb
254
296
  - spec/generators/statesman/migration_generator_spec.rb
255
297
  - spec/generators/statesman/mongoid_transition_generator_spec.rb
256
298
  - spec/spec_helper.rb