statesman-multi_state 0.1.3 → 0.2.1

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
  SHA256:
3
- metadata.gz: e46de0c69e16445bf06a31f55dd04be09853dcb41773cc61252e45f17cc84fdb
4
- data.tar.gz: 05b43b0794b37be03bd9d97b45b5660a3ffa8e095b0c903754d7f478d0d48dd1
3
+ metadata.gz: a4b8c9c47ecda9c736feaf3a97086b56d2f019a5b979b8d9b636d6d3732159d0
4
+ data.tar.gz: 43ba1e393245d8c87fab6efdd457f0b335bd71d3658dab6f74af9713205bc9b9
5
5
  SHA512:
6
- metadata.gz: 8cdfde1e0826666e57cb82d6be2eff7649675ba8d170520b40c22a139c3fc981030b07b072f454148d446ba7cc71121e09644a03709d2ba618307153bb6558a3
7
- data.tar.gz: 76efdb5cc4e0217f2f6543aef1b5689d0c5375420d34970da6f29b1b995593ef6045b71bb32bb6741bc3157e41870ab56d2006ba228f708d7e3dac6923cc9b21
6
+ metadata.gz: 33435bad1dde992be4d977ddba9cc7eec2b99c75e79bc00c1fdfebc27973c2f7ee93003fe1d3ac6fec0f65d87fcd288325eac5019cea75eba4e4e06995d24256
7
+ data.tar.gz: 3eaac52379397cd9fc2f97f5d623d2640bd490674a5932a051e55a792abb2f65c2ddc054c86495ed2fafd97a9348b5a375c1ef935b5b98f11999e9e3f2ae0a74
data/README.md CHANGED
@@ -27,6 +27,7 @@ gem "statesman-multi_state"
27
27
  class Order < ActiveRecord::Base
28
28
  has_one_state_machine :business_status, state_machine_klass: 'OrderBusinessStatusStateMachine', transition_klass: 'OrderBusinessStatus'
29
29
  has_one_state_machine :admin_status, state_machine_klass: 'StateMachineKlass', transition_klass: 'MyTransitionKlass'
30
+ has_one_state_machine :custom_status, state_machine_klass: 'StateMachineKlass', transition_klass: 'MyTransitionKlass', transition_foreign_key: 'my_custom_fk_id'
30
31
  end
31
32
  ```
32
33
 
@@ -34,7 +35,11 @@ Calling `has_one_state_machine` on `business_status` adds the following:
34
35
  - A has_many association with the transition table
35
36
  ```ruby
36
37
  has_many :order_business_status_transitions, dependent: :destroy
38
+
39
+ # with custom foreign key if `transition_foreign_key` is set on `has_one_state_machine`
40
+ has_many :order_business_status_transitions, dependent: :destroy, foreign_key: 'my_custom_fk_id'
37
41
  ```
42
+
38
43
  - a virtual attribute `#business_status_state_form` (using the `ActiveRecord::Attributes` API ) to represent the actual current value of the state machine on the transition table.
39
44
  This also allows to be used in forms directly without the need for nested attributes. Using this API instead of a regular `attr_accessor` allows us to get all the benefits of dirty tracking, so we can track when changes were made, and perform the appropriate transitions.
40
45
  It default to `business_status` current state on the state machine
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'statesman'
3
4
  require 'statesman/adapters/custom_active_record_queries'
4
5
 
5
6
  module Statesman
@@ -9,13 +10,15 @@ module Statesman
9
10
 
10
11
  class_methods do
11
12
  def has_one_state_machine(field_name, state_machine_klass:, transition_klass:,
12
- transition_name: transition_klass.to_s.underscore.pluralize.to_sym, virtual_attribute_name: "#{field_name}_state_form")
13
+ transition_name: transition_klass.to_s.underscore.demodulize.pluralize.to_sym, virtual_attribute_name: "#{field_name}_state_form",
14
+ transition_foreign_key: nil)
13
15
  state_machine_name = "#{field_name}_state_machine"
14
16
 
15
17
  # To handle STI, this needs to be done to get the base klass
16
18
  base_klass = caller_locations.first.label.split(':').last[...-1]
17
19
 
18
- has_many transition_name, class_name: transition_klass.to_s, autosave: false, dependent: :destroy
20
+ association_options = { class_name: transition_klass.to_s, autosave: false, dependent: :destroy }.merge(transition_foreign_key ? { foreign_key: transition_foreign_key.to_s } : {} )
21
+ has_many transition_name, **association_options
19
22
 
20
23
  include Statesman::Adapters::CustomActiveRecordQueries[
21
24
  transition_class: transition_klass.constantize,
@@ -94,7 +97,8 @@ module Statesman
94
97
  field_name,
95
98
  nil,
96
99
  { state_machine_klass: state_machine_klass, transition_klass: transition_klass,
97
- transition_name: transition_name, virtual_attribute_name: virtual_attribute_name },
100
+ transition_name: transition_name, virtual_attribute_name: virtual_attribute_name,
101
+ transition_foreign_key: transition_foreign_key },
98
102
  self
99
103
  )
100
104
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Statesman
4
4
  module MultiState
5
- VERSION = '0.1.3'
5
+ VERSION = '0.2.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statesman-multi_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chedli Bourguiba
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-23 00:00:00.000000000 Z
11
+ date: 2023-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -63,7 +63,7 @@ metadata:
63
63
  homepage_uri: https://github.com/chaadow/statesman-multi_state
64
64
  source_code_uri: https://github.com/chaadow/statesman-multi_state
65
65
  changelog_uri: https://github.com/chaadow/statesman-multi_state/blob/master/CHANGELOG.md
66
- post_install_message:
66
+ post_install_message:
67
67
  rdoc_options: []
68
68
  require_paths:
69
69
  - lib
@@ -78,8 +78,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  - !ruby/object:Gem::Version
79
79
  version: '0'
80
80
  requirements: []
81
- rubygems_version: 3.1.6
82
- signing_key:
81
+ rubygems_version: 3.4.10
82
+ signing_key:
83
83
  specification_version: 4
84
84
  summary: Rails/Statesman plugin that handle multiple state machines on the same model
85
85
  through `has_one_state_machine` ActiveRecord macro