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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 56832b6fe9e7b6bdab1c9890fbf6078015b095c85e7b07f38f991012ee471402
|
|
4
|
+
data.tar.gz: 26cd99926e156dba1f805a7077934a0ee6af40b3f699c60ff44d2a533cd3500b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
181
|
-
"different default" warning is printed
|
|
182
|
-
|
|
183
|
-
|
|
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.
|
|
@@ -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
|
-
|
|
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
|