statesman-multi_state 0.2.8 → 0.3.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: 4341220f127a7664fa7f0f3457a58d8901284dedf43bec6b8f8fd8b7d18b52fe
|
|
4
|
+
data.tar.gz: e5ac0fe8d9f6de6cacc2bb4c9b89ddc7e9e1800920bc660dc169eb8507cc5ff8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2acbbdb9b3af2837f225a870fe483f16eeadd80a127a902da0180b62f9950e9ff8a641cf02296e2b71e12534485cfc65264ff910a5896c6d232359a2d15667af
|
|
7
|
+
data.tar.gz: 75da67f863766eb592e297a411dcced1cbc08c8f2df4215909ccb21f87e946a68e145b8dea9273b2a8b7113ae39a005a2a70d8821310b34c4a12fbcfcc97ce5c
|
|
@@ -50,14 +50,17 @@ module Statesman
|
|
|
50
50
|
|
|
51
51
|
if #{initial_transition}
|
|
52
52
|
if @#{state_machine_name}.history.empty? && #{state_machine_klass}.initial_state
|
|
53
|
-
@#{state_machine_name}.
|
|
53
|
+
if @#{state_machine_name}.object.persisted? && self.class.current_role != ::ActiveRecord.reading_role
|
|
54
|
+
@#{state_machine_name}.instance_variable_get(:@storage_adapter).create(nil, #{state_machine_klass}.initial_state)
|
|
55
|
+
end
|
|
54
56
|
end
|
|
55
57
|
end
|
|
56
58
|
@#{state_machine_name}
|
|
57
59
|
end
|
|
58
60
|
|
|
59
61
|
def #{virtual_attribute_name}
|
|
60
|
-
|
|
62
|
+
value = read_attribute("#{virtual_attribute_name}")
|
|
63
|
+
value.nil? ? #{field_name}_current_state : value
|
|
61
64
|
end
|
|
62
65
|
|
|
63
66
|
def #{field_name}_current_state_human
|
|
@@ -66,6 +69,26 @@ module Statesman
|
|
|
66
69
|
end
|
|
67
70
|
CODE
|
|
68
71
|
|
|
72
|
+
# Define a public reader so form helpers, serializers, and other
|
|
73
|
+
# callers that use `public_send(field_name)` work. Guard against
|
|
74
|
+
# clobbering an existing column reader or user-defined method with
|
|
75
|
+
# the same name as `field_name`.
|
|
76
|
+
table_name_available = name.present? || (instance_variable_defined?(:@table_name) && @table_name.present?)
|
|
77
|
+
has_column_reader = if respond_to?(:column_names) && table_name_available &&
|
|
78
|
+
respond_to?(:table_exists?) && table_exists?
|
|
79
|
+
column_names.include?(field_name.to_s)
|
|
80
|
+
else
|
|
81
|
+
false
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
unless method_defined?(field_name) || private_method_defined?(field_name) || has_column_reader
|
|
85
|
+
generated_association_methods.class_eval <<~READER, __FILE__, __LINE__ + 1
|
|
86
|
+
def #{field_name}
|
|
87
|
+
#{field_name}_current_state
|
|
88
|
+
end
|
|
89
|
+
READER
|
|
90
|
+
end
|
|
91
|
+
|
|
69
92
|
include(const_set("#{field_name}#{SecureRandom.hex(4)}_mod".classify, Module.new).tap do |mod|
|
|
70
93
|
mod.module_eval do
|
|
71
94
|
extend ActiveSupport::Concern
|
|
@@ -84,8 +107,18 @@ module Statesman
|
|
|
84
107
|
class_eval <<~METHOD, __FILE__, __LINE__ + 1
|
|
85
108
|
def save_with_state(**options)
|
|
86
109
|
@registered_callbacks ||= []
|
|
87
|
-
if #{virtual_attribute_name}
|
|
88
|
-
|
|
110
|
+
if #{virtual_attribute_name}.to_s != #{field_name}_current_state.to_s
|
|
111
|
+
if #{field_name}_can_transition_to?(#{virtual_attribute_name})
|
|
112
|
+
@registered_callbacks << -> { #{field_name}_transition_to(#{virtual_attribute_name}, **options) }
|
|
113
|
+
else
|
|
114
|
+
errors.add(
|
|
115
|
+
:#{field_name},
|
|
116
|
+
:invalid_transition,
|
|
117
|
+
current_state: #{field_name}_current_state_human,
|
|
118
|
+
target_state: I18n.t(#{virtual_attribute_name}, scope: "statesman.#{field_name}_#{base_klass.underscore}", default: #{virtual_attribute_name}.to_s)
|
|
119
|
+
)
|
|
120
|
+
return false
|
|
121
|
+
end
|
|
89
122
|
end
|
|
90
123
|
|
|
91
124
|
if defined?(super)
|
|
@@ -14,6 +14,10 @@ module Statesman
|
|
|
14
14
|
ActiveRecord::Reflection.singleton_class.prepend(Statesman::MultiState::Reflection::ReflectionExtension)
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
|
+
|
|
18
|
+
initializer 'statesman.multi_state.i18n' do |app|
|
|
19
|
+
app.config.i18n.load_path += Dir[File.expand_path('locales/*.yml', __dir__)]
|
|
20
|
+
end
|
|
17
21
|
end
|
|
18
22
|
end
|
|
19
23
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: statesman-multi_state
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Chedli Bourguiba
|
|
@@ -51,6 +51,7 @@ files:
|
|
|
51
51
|
- lib/statesman/adapters/custom_active_record_queries.rb
|
|
52
52
|
- lib/statesman/multi_state.rb
|
|
53
53
|
- lib/statesman/multi_state/active_record_macro.rb
|
|
54
|
+
- lib/statesman/multi_state/locales/en.yml
|
|
54
55
|
- lib/statesman/multi_state/railtie.rb
|
|
55
56
|
- lib/statesman/multi_state/reflection.rb
|
|
56
57
|
- lib/statesman/multi_state/version.rb
|