statesman-multi_state 0.1.3 → 0.2.0

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: 35f6636a6f4af3bf83393099ff9da9cce30e4cc8063a88e1c7ec24ea568ff2bb
4
+ data.tar.gz: c6064ca504b13bafe10e4c173b557c0fa27b5697603af0a9ed626946de9811d3
5
5
  SHA512:
6
- metadata.gz: 8cdfde1e0826666e57cb82d6be2eff7649675ba8d170520b40c22a139c3fc981030b07b072f454148d446ba7cc71121e09644a03709d2ba618307153bb6558a3
7
- data.tar.gz: 76efdb5cc4e0217f2f6543aef1b5689d0c5375420d34970da6f29b1b995593ef6045b71bb32bb6741bc3157e41870ab56d2006ba228f708d7e3dac6923cc9b21
6
+ metadata.gz: 711439772df22c7cb28c5dafc634fe0e3938aaf4ccaa22c499e0fc214a23e794ab3d17287e4eedad57853cec0e5445160381a93df893192168f2699b48f7d464
7
+ data.tar.gz: c04a23f23852d80a3636dff62b0880d7f9c7da52ed55c7055429ce35b86655c4b57ec79eba84faacccb2ab8421e55276510ff4e10cc5f756891c47a807367c0c
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
@@ -9,13 +9,15 @@ module Statesman
9
9
 
10
10
  class_methods do
11
11
  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")
12
+ transition_name: transition_klass.to_s.underscore.pluralize.to_sym, virtual_attribute_name: "#{field_name}_state_form",
13
+ transition_foreign_key: nil)
13
14
  state_machine_name = "#{field_name}_state_machine"
14
15
 
15
16
  # To handle STI, this needs to be done to get the base klass
16
17
  base_klass = caller_locations.first.label.split(':').last[...-1]
17
18
 
18
- has_many transition_name, class_name: transition_klass.to_s, autosave: false, dependent: :destroy
19
+ association_options = { class_name: transition_klass.to_s, autosave: false, dependent: :destroy }.merge(transition_foreign_key ? { foreign_key: transition_foreign_key.to_s } : {} )
20
+ has_many transition_name, **association_options
19
21
 
20
22
  include Statesman::Adapters::CustomActiveRecordQueries[
21
23
  transition_class: transition_klass.constantize,
@@ -94,7 +96,8 @@ module Statesman
94
96
  field_name,
95
97
  nil,
96
98
  { state_machine_klass: state_machine_klass, transition_klass: transition_klass,
97
- transition_name: transition_name, virtual_attribute_name: virtual_attribute_name },
99
+ transition_name: transition_name, virtual_attribute_name: virtual_attribute_name,
100
+ transition_foreign_key: transition_foreign_key },
98
101
  self
99
102
  )
100
103
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Statesman
4
4
  module MultiState
5
- VERSION = '0.1.3'
5
+ VERSION = '0.2.0'
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.0
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-04 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