state_machines-activerecord 0.301.0 → 0.302.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a978e35d63388e62357e60d6a6e02776b65aa895f0dff425d00fc8da056f48f
4
- data.tar.gz: 03bb42e39587a982d6579bc0bdba365ef0e21b2dbea825116bc217677da067a5
3
+ metadata.gz: 56832b6fe9e7b6bdab1c9890fbf6078015b095c85e7b07f38f991012ee471402
4
+ data.tar.gz: 26cd99926e156dba1f805a7077934a0ee6af40b3f699c60ff44d2a533cd3500b
5
5
  SHA512:
6
- metadata.gz: 7838106f843f19188b991d5a4ce9c62a9fd8420dd50cb6dbf651d81310dd2cbd70b986b48b857c614b20ffc082988c8422343133a23559fd95c2b5c3b755c469
7
- data.tar.gz: 7550030858f46cfa1221c550b2a33ae3fae7b0014e90836cfa538a96472c77010642339593ed91b3497afac62b3fb9561c31a6e29b7f802a359f298102137136
6
+ metadata.gz: 1af35637c8e020ccdf292d27e9ef1ed40f3dd50d3dc17a027bf35a4f8f0034b80b1bb1c34eec7ffb66998678f33dd84787d57dbce06b1529c27c3fa1c93f7679
7
+ data.tar.gz: fe3d5f5bc88807999c5fbf262eb3c56833a74a06096acbe5a8c79094a94bbacffc526fca33b2b07beacea9d19306a72ee52f30670f699bfd56614bd1a069cb56
data/README.md CHANGED
@@ -177,10 +177,10 @@ Account.new.state # => "active"
177
177
  Account.new.suspend # => true
178
178
  ```
179
179
 
180
- - An explicit static `initial:` always wins over the schema default (and the
181
- "different default" warning is printed, as before).
182
- - A dynamic `initial: ->(record) { ... }` only fills a blank attribute, so a
183
- schema default keeps precedence over it; the warning is printed.
180
+ - An explicit `initial:`, static or dynamic, always wins over the schema
181
+ default (and the "different default" warning is printed). A dynamic one is
182
+ evaluated after the attributes given to `new` are assigned, so it can depend
183
+ on them; a state given to `new` is kept.
184
184
  - The default is read when a record is built, not when the class is loaded,
185
185
  so defining the machine before the table exists (e.g. while migrating) is
186
186
  fine.
@@ -3,7 +3,7 @@
3
3
  module StateMachines
4
4
  module Integrations
5
5
  module ActiveRecord
6
- VERSION = '0.301.0'
6
+ VERSION = '0.302.0'
7
7
  end
8
8
  end
9
9
  end
@@ -1028,6 +1028,30 @@ module StateMachines
1028
1028
  static, dynamic = machines.partition { |machine| !machine.dynamic_initial_state? }
1029
1029
  (static + dynamic).each { |machine| machine.initialize_state(record) }
1030
1030
  end
1031
+
1032
+ # Initializes the states of a new record. A dynamic initial state is
1033
+ # applied even over a column default, like a static one: the column
1034
+ # default is already in the attribute, so core's dynamic pass (which
1035
+ # only fills a blank attribute) would keep it. A state the caller
1036
+ # passed is left in place for {StateGuard} to check. Dynamic states
1037
+ # are evaluated after static ones, which they may depend on; the
1038
+ # caller's +new+ block runs afterwards, so it cannot be overwritten.
1039
+ #
1040
+ # @param record [ActiveRecord::Base] the record being built
1041
+ # @param attributes [Hash] the attributes given to +new+, aliases
1042
+ # resolved, plus the scope's attributes
1043
+ # @return [void]
1044
+ def initialize_new_record_states(record, attributes)
1045
+ machines = record.class.state_machines
1046
+ machines.initialize_states(record, { dynamic: false }, attributes)
1047
+
1048
+ given = attributes.keys.map(&:to_s)
1049
+ machines.each_value do |machine|
1050
+ next unless machine.dynamic_initial_state?
1051
+
1052
+ machine.initialize_state(record, force: !given.include?(StateGuard.guarded_name(machine)))
1053
+ end
1054
+ end
1031
1055
  end
1032
1056
 
1033
1057
  protected
@@ -1077,7 +1101,7 @@ module StateMachines
1077
1101
  attributes = (attributes || {}).transform_keys { |key| self.class.attribute_aliases[key.to_s] || key }
1078
1102
  scoped_attributes = attributes.merge(self.class.scope_attributes)
1079
1103
 
1080
- self.class.state_machines.initialize_states(self, {}, scoped_attributes)
1104
+ StateMachines::Integrations::ActiveRecord.initialize_new_record_states(self, scoped_attributes)
1081
1105
  StateMachines::Integrations::ActiveRecord::StateGuard.finish_construction(self)
1082
1106
  yield(*args) if block_given?
1083
1107
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machines-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.301.0
4
+ version: 0.302.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih