enum_fields 0.0.5 → 0.1.1

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: 4d2ba45a5964fee807097e941cd817f7e1df72ace9094c8bf2620c7462bd06d2
4
- data.tar.gz: 4b9c320af51de403c10631881531efe3455d2ac5946df3b5798d03e825e06ce3
3
+ metadata.gz: 6f02f9ddd27cf9f20da714f32817b9d5da178834ff87d41ac465f0250b0fd024
4
+ data.tar.gz: 5c8e63ac0d57bea298c7dce4298596cf60d3e2d66791e2d336ef4730ce40a188
5
5
  SHA512:
6
- metadata.gz: 6d16f74b5eb526d1cd7ea287a75b5a261679d432be001736b13e0ce52f442bc5806176e8e5020ae605b475eea608f83af25cd487eed1d181d50af30ad1a24dbf
7
- data.tar.gz: e5ea072511fa65e0ea6f5221f9c39406af917533aecca0a6e962be8bf743fc17740033b32064d59579fac25f5cebfd337e2eb002b96546e14bd830275d8bc34e
6
+ metadata.gz: 48a6e303906852608449e28ac134dcab3fa5c1e0bc39b39d54075c8311800ef427ce771e3f5cecf18391d079e383616f2ec281331d3e5a6eeb86cc392d1e516a
7
+ data.tar.gz: 618ad4bc993ec5adf45aa9d7df42b514750b34787b07fa776331673729f27e6068da1fa51acd0dbb94ebf589b5a62d9cec6f2cc89d39971481aee8a465eae206
@@ -11,6 +11,7 @@ module EnumFields
11
11
  @accessor = accessor.to_sym
12
12
  @column_name = options.fetch(:column, @accessor).to_sym
13
13
  @definition = Definition.new(definition)
14
+ @options = options
14
15
  end
15
16
 
16
17
  def define!
@@ -100,7 +101,7 @@ module EnumFields
100
101
  definitions = self.class.enum_field_for(accessor)
101
102
  return nil if definitions.blank?
102
103
 
103
- definitions[column_value]
104
+ definitions[column_value] || definitions.values.find { |d| d[:value] == column_value }
104
105
  end
105
106
  end
106
107
 
@@ -116,7 +117,8 @@ module EnumFields
116
117
  definitions = self.class.enum_field_for(accessor)
117
118
  return nil if definitions.blank?
118
119
 
119
- definitions.dig(column_value, property)
120
+ metadata = definitions[column_value] || definitions.values.find { |d| d[:value] == column_value }
121
+ metadata&.dig(property)
120
122
  end
121
123
  end
122
124
  end
@@ -143,13 +145,56 @@ module EnumFields
143
145
  end
144
146
 
145
147
  def define_validation!
146
- return if @definition.blank?
148
+ return unless column_validatable?
147
149
 
150
+ if column_polymorphic_association_name.present?
151
+ define_polymorphic_validation!
152
+ else
153
+ define_standard_validation!
154
+ end
155
+ end
156
+
157
+ def define_standard_validation!
148
158
  valid_values = @definition.values.pluck(:value)
159
+
149
160
  @model_class.validates(@column_name, inclusion: {
150
161
  in: valid_values,
151
162
  allow_nil: true,
152
163
  })
153
164
  end
165
+
166
+ def define_polymorphic_validation!
167
+ association_name = column_polymorphic_association_name
168
+ column_name = @column_name
169
+ accessor = @accessor
170
+
171
+ @model_class.validate do
172
+ association_obj = respond_to?(association_name) ? public_send(association_name) : nil
173
+ valid_values = self.class.public_send("#{accessor}_values")
174
+
175
+ if association_obj
176
+ errors.add(association_name, "must be one of: #{valid_values.join(', ')}") unless valid_values.include?(association_obj.class.name)
177
+ else
178
+ column_value = attributes[column_name.to_s]
179
+ next if column_value.nil?
180
+
181
+ errors.add(column_name, "must be one of: #{valid_values.join(', ')}") unless valid_values.include?(column_value)
182
+ end
183
+ end
184
+ end
185
+
186
+ def column_validatable?
187
+ @definition.present? && @options.fetch(:validate, true)
188
+ end
189
+
190
+ def column_polymorphic_association_name
191
+ return nil unless @model_class.respond_to?(:reflect_on_all_associations)
192
+
193
+ reflection = @model_class.reflect_on_all_associations(:belongs_to).find do |r|
194
+ r.options[:polymorphic] && "#{r.name}_type" == @column_name.to_s
195
+ end
196
+
197
+ reflection&.name
198
+ end
154
199
  end
155
200
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EnumFields
4
- VERSION = '0.0.5'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kinnell Shah