rails_state_machine 2.1.1 → 2.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: f378f574778d4689103d41b8b77a264ae1480ace89753c5cf33015dda494f296
4
- data.tar.gz: 0dec14e494125c3a7282e5629849c6322cc5404fa744ee5a6847256b1c00bc63
3
+ metadata.gz: f07991f6a0bc2dab134bc8518199885d79d0ee49d1865043a5e805e71c04b2b8
4
+ data.tar.gz: b4d3ba7b43eaed7a95522df3b385147f6feefbfac8a7b9dd7c49c6089731d037
5
5
  SHA512:
6
- metadata.gz: 0b6b13a387e5ecebd6a9b6d301652df68b2bd5e95514491729e521e7efbc28ecce1752aacc667395974a211bf2aa2e3f0883126eea8dc1bbcd3b827e5d13e442
7
- data.tar.gz: ed790b2722a425566cc9d02d074cf3b20d296b1524579839af8678519b8e59c2fdb7ddd1546d225258d843b96f0a0dcfa443d09fca0cb400cd0f35c3e644f184
6
+ metadata.gz: 1a5c81c9136fc6181974e7de5cd4a664f481c965098f39af45a3998b5cc3f29c9a46e0e983d5ca2cb724915505143c09643b93ade5a6bf3d47590af6ab716147
7
+ data.tar.gz: af45d14e32f278bc0a4e1d1dc76b9f7863300e178d87b032f7217c7aa4df1987740a5fb895b055058b6fb7fb1ca4e08cc3021c2d6b2bafc62c44ac1f15f23d94
data/CHANGELOG.md CHANGED
@@ -10,6 +10,14 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
10
10
  ### Breaking changes
11
11
 
12
12
 
13
+ ## 2.2.0 2023-12-06
14
+
15
+ ### Compatible changes
16
+
17
+ - Added: State machine can now use the `:prefix` option to avoid name collision if you define multiple state machines
18
+ on the same model, and use state names more than once
19
+ - Fix bug where additional inclusions of `RailsStateMachine::Model` would reset previous defined state machines
20
+
13
21
  ## 2.1.1 2022-03-16
14
22
 
15
23
  ### Compatible changes
data/Gemfile CHANGED
@@ -1 +1 @@
1
- Gemfile.5.1.pg
1
+ ./Gemfile.5.1.pg
data/Gemfile.5.1.pg.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_state_machine (2.1.1)
4
+ rails_state_machine (2.2.0)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -146,4 +146,4 @@ DEPENDENCIES
146
146
  rspec (~> 3.5)
147
147
 
148
148
  BUNDLED WITH
149
- 2.2.3
149
+ 2.3.0
data/Gemfile.5.2.pg.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_state_machine (2.1.1)
4
+ rails_state_machine (2.2.0)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -156,4 +156,4 @@ DEPENDENCIES
156
156
  rspec (~> 3.5)
157
157
 
158
158
  BUNDLED WITH
159
- 2.2.15
159
+ 2.3.0
data/Gemfile.6.0.pg.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_state_machine (2.1.1)
4
+ rails_state_machine (2.2.0)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -172,4 +172,4 @@ DEPENDENCIES
172
172
  rspec (~> 3.5)
173
173
 
174
174
  BUNDLED WITH
175
- 2.1.4
175
+ 2.3.0
data/Gemfile.6.1.pg.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_state_machine (2.1.1)
4
+ rails_state_machine (2.2.0)
5
5
  activerecord
6
6
 
7
7
  GEM
data/Gemfile.7.0.pg.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_state_machine (2.1.1)
4
+ rails_state_machine (2.2.0)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -92,8 +92,12 @@ GEM
92
92
  marcel (1.0.2)
93
93
  method_source (1.0.0)
94
94
  mini_mime (1.1.2)
95
+ mini_portile2 (2.6.1)
95
96
  minitest (5.15.0)
96
97
  nio4r (2.5.8)
98
+ nokogiri (1.12.5)
99
+ mini_portile2 (~> 2.6.1)
100
+ racc (~> 1.4)
97
101
  nokogiri (1.12.5-x86_64-linux)
98
102
  racc (~> 1.4)
99
103
  pg (1.2.3)
@@ -156,6 +160,7 @@ GEM
156
160
  zeitwerk (2.5.1)
157
161
 
158
162
  PLATFORMS
163
+ ruby
159
164
  x86_64-linux
160
165
 
161
166
  DEPENDENCIES
data/Gemfile.lock CHANGED
@@ -1 +1 @@
1
- Gemfile.5.1.pg.lock
1
+ ./Gemfile.5.1.pg.lock
data/README.md CHANGED
@@ -107,7 +107,7 @@ end
107
107
 
108
108
  To use a state attribute other than the default `state`, pass it to the `.state_machine` method:
109
109
 
110
- ```
110
+ ```ruby
111
111
  state_machine :review_state do
112
112
  # ...
113
113
  end
@@ -116,13 +116,24 @@ end
116
116
  This also allows you to define multiple state machines on the same model. Note that event
117
117
  and state names still have to be unique for the whole model.
118
118
 
119
+ If you define multiple state machines on the same model, and use state names more than once,
120
+ you must use a prefix to avoid name collisions.
121
+
122
+ A prefix may be specified by passing the `:prefix` option when declaring your state machine:
123
+
124
+ ```ruby
125
+ state_machine :review_state, prefix: 'some_prefix' do
126
+ # ...
127
+ end
128
+ ```
129
+
119
130
 
120
131
  ## Taking multiple transitions
121
132
 
122
133
  You can safely take a second transition inside an after_save callback. All relevant
123
134
  callbacks will be run.
124
135
 
125
- ```
136
+ ```ruby
126
137
  state_machine do
127
138
  state :draft, initial: true
128
139
  state :review_pending
@@ -7,15 +7,15 @@ module RailsStateMachine
7
7
  extend ClassMethods
8
8
 
9
9
  cattr_accessor :state_machines
10
- self.state_machines = {}
10
+ self.state_machines ||= {}
11
11
 
12
12
  delegate :state_machine, to: :class
13
13
  end
14
14
  end
15
15
 
16
16
  module ClassMethods
17
- def state_machine(state_attribute = DEFAULT_STATE_ATTRIBUTE, &block)
18
- state_machine = state_machines[state_attribute] ||= StateMachine.new(self, state_attribute)
17
+ def state_machine(state_attribute = DEFAULT_STATE_ATTRIBUTE, prefix: '', &block)
18
+ state_machine = state_machines[state_attribute] ||= StateMachine.new(self, state_attribute, prefix: prefix)
19
19
  if block
20
20
  include(Callbacks) unless self < Callbacks
21
21
  state_machine.configure(&block)
@@ -1,18 +1,26 @@
1
1
  module RailsStateMachine
2
2
  class StateMachine
3
- attr_reader :model
4
3
 
5
- def initialize(model, state_attribute)
4
+ StateAlreadyDefinedError = Class.new(StandardError)
5
+
6
+ def initialize(model, state_attribute, prefix: '')
6
7
  @model = model
7
8
  @state_attribute = state_attribute
9
+ @prefix = prefix
10
+ @prefix_for_constant_definition = "#{@prefix.upcase}_" if @prefix.present?
11
+ @prefix_for_method_definition = "#{@prefix.downcase}_" if @prefix.present?
8
12
  @states_by_name = {}
9
13
  @events_by_name = {}
10
14
  build_model_module
11
15
  end
12
16
 
17
+ attr_reader :model, :prefix, :prefix_for_constant_definition, :prefix_for_method_definition, :state_attribute
18
+
13
19
  def configure(&block)
14
20
  instance_eval(&block)
15
21
 
22
+ check_if_states_already_defined
23
+
16
24
  define_state_methods
17
25
  define_state_constants
18
26
  register_initial_state
@@ -74,11 +82,26 @@ module RailsStateMachine
74
82
  @model_module.module_eval(&block)
75
83
  end
76
84
 
85
+ def check_if_states_already_defined
86
+ @states_by_name.each do |state_name, _|
87
+ other_state_machines.each do |state_machine|
88
+ if state_machine.has_state?(state_name) && state_machine.prefix == prefix
89
+ raise StateAlreadyDefinedError, "State #{state_name.inspect} has already been defined in the #{state_machine.state_attribute.inspect} state machine. You may use the :prefix option when defining a state machine to avoid that."
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ def other_state_machines
96
+ model.state_machines.except(@state_attribute).values
97
+ end
98
+
77
99
  def define_state_methods
78
100
  state_attribute = @state_attribute
101
+ prefix = prefix_for_method_definition
79
102
  state_names.each do |state_name|
80
103
  model_module_eval do
81
- define_method "#{state_name}?" do
104
+ define_method "#{prefix}#{state_name}?" do
82
105
  state_machine_state_manager(state_attribute).state == state_name.to_s
83
106
  end
84
107
  end
@@ -87,7 +110,7 @@ module RailsStateMachine
87
110
 
88
111
  def define_state_constants
89
112
  state_names.each do |state_name|
90
- model_constant("STATE_#{state_name.upcase}", state_name)
113
+ model_constant("#{@prefix_for_constant_definition}STATE_#{state_name.upcase}", state_name)
91
114
  end
92
115
  end
93
116
 
@@ -1,3 +1,3 @@
1
1
  module RailsStateMachine
2
- VERSION = '2.1.1'
2
+ VERSION = '2.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arne Hartherz
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-03-16 00:00:00.000000000 Z
12
+ date: 2023-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  - !ruby/object:Gem::Version
127
127
  version: '0'
128
128
  requirements: []
129
- rubygems_version: 3.3.9
129
+ rubygems_version: 3.1.6
130
130
  signing_key:
131
131
  specification_version: 4
132
132
  summary: ActiveRecord-bound state machine