stateful_models 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
Binary file
@@ -1,8 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateHasStatesTables < ActiveRecord::Migration[7.1]
1
+ class CreateHasStatesStates < ActiveRecord::Migration[8.0]
4
2
  def change
5
3
  create_table :has_states_states do |t|
4
+ t.string :type, null: false
6
5
  t.string :state_type
7
6
  t.string :status, null: false
8
7
 
@@ -10,10 +9,10 @@ class CreateHasStatesTables < ActiveRecord::Migration[7.1]
10
9
 
11
10
  t.references :stateable, polymorphic: true, null: false
12
11
 
13
- t.datetime :completed_at
14
12
  t.timestamps
15
13
 
14
+ t.index %i[type stateable_id]
16
15
  t.index %i[stateable_type stateable_id]
17
16
  end
18
17
  end
19
- end
18
+ end
@@ -261,4 +261,66 @@ RSpec.describe HasStates::State, type: :model do
261
261
  end.to change { HasStates.configuration.callbacks.size }.from(1).to(0)
262
262
  end
263
263
  end
264
+
265
+ describe 'custom state types' do
266
+ # Create a custom state class for testing
267
+ class KYCState < HasStates::Base
268
+ validates :metadata, presence: true
269
+ validate :required_metadata_fields
270
+
271
+ private
272
+
273
+ def required_metadata_fields
274
+ return if metadata&.key?('document_type')
275
+ errors.add(:metadata, 'must include document_type')
276
+ end
277
+ end
278
+
279
+ let(:user) { create(:user) }
280
+
281
+ it 'allows creation of custom state types' do
282
+ state = user.add_state(
283
+ 'kyc',
284
+ status: 'pending',
285
+ metadata: { document_type: 'passport' },
286
+ state_class: KYCState
287
+ )
288
+
289
+ expect(state).to be_valid
290
+ expect(state).to be_a(KYCState)
291
+ end
292
+
293
+ it 'enforces custom validations' do
294
+ expect do
295
+ user.add_state(
296
+ 'kyc',
297
+ status: 'pending',
298
+ metadata: { other_field: 'value' },
299
+ state_class: KYCState
300
+ )
301
+ end.to raise_error(
302
+ ActiveRecord::RecordInvalid,
303
+ /Metadata must include document_type/
304
+ )
305
+ end
306
+
307
+ it 'defaults to HasStates::State when no state_class specified' do
308
+ state = user.add_state('kyc', status: 'pending')
309
+
310
+ expect(state).to be_a(HasStates::State)
311
+ expect(state).to be_valid
312
+ end
313
+
314
+ it 'maintains STI type across database reads' do
315
+ state = user.add_state(
316
+ 'kyc',
317
+ status: 'pending',
318
+ metadata: { document_type: 'passport' },
319
+ state_class: KYCState
320
+ )
321
+
322
+ reloaded_state = HasStates::Base.find(state.id)
323
+ expect(reloaded_state).to be_a(KYCState)
324
+ end
325
+ end
264
326
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stateful_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Scholl
@@ -26,6 +26,7 @@ files:
26
26
  - lib/generators/has_states/install/templates/create_has_states_states.rb.erb
27
27
  - lib/generators/has_states/install/templates/initializer.rb.erb
28
28
  - lib/has_states.rb
29
+ - lib/has_states/base.rb
29
30
  - lib/has_states/callback.rb
30
31
  - lib/has_states/configuration.rb
31
32
  - lib/has_states/configuration/model_configuration.rb
@@ -62,13 +63,14 @@ files:
62
63
  - spec/dummy/config/environments/test.rb
63
64
  - spec/dummy/config/initializers/cors.rb
64
65
  - spec/dummy/config/initializers/filter_parameter_logging.rb
66
+ - spec/dummy/config/initializers/has_states.rb
65
67
  - spec/dummy/config/initializers/inflections.rb
66
68
  - spec/dummy/config/locales/en.yml
67
69
  - spec/dummy/config/master.key
68
70
  - spec/dummy/config/puma.rb
69
71
  - spec/dummy/config/routes.rb
70
72
  - spec/dummy/db/migrate/20241221171423_create_test_models.rb
71
- - spec/dummy/db/migrate/20241221183116_create_has_states_tables.rb
73
+ - spec/dummy/db/migrate/20241223212128_create_has_states_states.rb
72
74
  - spec/dummy/db/schema.rb
73
75
  - spec/dummy/db/seeds.rb
74
76
  - spec/dummy/log/development.log
@@ -80,7 +82,7 @@ files:
80
82
  - spec/factories/has_states.rb
81
83
  - spec/generators/has_states/install_generator_spec.rb
82
84
  - spec/generators/tmp/config/initializers/has_states.rb
83
- - spec/generators/tmp/db/migrate/20241223020432_create_has_states_states.rb
85
+ - spec/generators/tmp/db/migrate/20241223213845_create_has_states_states.rb
84
86
  - spec/has_states/callback_spec.rb
85
87
  - spec/has_states/configuration_spec.rb
86
88
  - spec/has_states/state_spec.rb