statesman-multi_state 0.2.9 → 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: 4b3c268ec08674602979078f78430d66eb824ab50a039e9e504336c6fe6ff81f
4
- data.tar.gz: edbda12f05e4ca3ac0244135d4d6ac503ff62a1579d49ae3141b7c1081b9faa6
3
+ metadata.gz: 4341220f127a7664fa7f0f3457a58d8901284dedf43bec6b8f8fd8b7d18b52fe
4
+ data.tar.gz: e5ac0fe8d9f6de6cacc2bb4c9b89ddc7e9e1800920bc660dc169eb8507cc5ff8
5
5
  SHA512:
6
- metadata.gz: c7425a27748bf3be1353edac40e65688e7688040f2378f283d768da40ef640b34261c946470bd6503b09cb47ed8ff2185437f077935a53263add3ea003acc0d1
7
- data.tar.gz: 3811acac1ad6767f49dfe1c4c0a6176271a9ef4d3d77ffcce5854d30a0877d4efb690983d2cf04c5f098e6150e7ed835653c694f0052a181b03f816a03cb9c59
6
+ metadata.gz: 2acbbdb9b3af2837f225a870fe483f16eeadd80a127a902da0180b62f9950e9ff8a641cf02296e2b71e12534485cfc65264ff910a5896c6d232359a2d15667af
7
+ data.tar.gz: 75da67f863766eb592e297a411dcced1cbc08c8f2df4215909ccb21f87e946a68e145b8dea9273b2a8b7113ae39a005a2a70d8821310b34c4a12fbcfcc97ce5c
@@ -59,7 +59,8 @@ module Statesman
59
59
  end
60
60
 
61
61
  def #{virtual_attribute_name}
62
- super() || #{field_name}_current_state
62
+ value = read_attribute("#{virtual_attribute_name}")
63
+ value.nil? ? #{field_name}_current_state : value
63
64
  end
64
65
 
65
66
  def #{field_name}_current_state_human
@@ -68,6 +69,26 @@ module Statesman
68
69
  end
69
70
  CODE
70
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
+
71
92
  include(const_set("#{field_name}#{SecureRandom.hex(4)}_mod".classify, Module.new).tap do |mod|
72
93
  mod.module_eval do
73
94
  extend ActiveSupport::Concern
@@ -86,8 +107,18 @@ module Statesman
86
107
  class_eval <<~METHOD, __FILE__, __LINE__ + 1
87
108
  def save_with_state(**options)
88
109
  @registered_callbacks ||= []
89
- if #{virtual_attribute_name}_changed? && #{field_name}_can_transition_to?(#{virtual_attribute_name})
90
- @registered_callbacks << -> { #{field_name}_transition_to(#{virtual_attribute_name}, **options) }
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
91
122
  end
92
123
 
93
124
  if defined?(super)
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ invalid_transition: "cannot transition from %{current_state} to %{target_state}"
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Statesman
4
4
  module MultiState
5
- VERSION = '0.2.9'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  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.2.9
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