active_fields 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +0 -1
  3. data/.rubocop.yml +3 -15
  4. data/CHANGELOG.md +19 -0
  5. data/README.md +633 -0
  6. data/Rakefile +7 -0
  7. data/app/models/active_fields/application_record.rb +7 -0
  8. data/app/models/active_fields/field/base.rb +17 -0
  9. data/{lib/active_fields/models → app/models/active_fields}/field/boolean.rb +14 -9
  10. data/{lib/active_fields/models → app/models/active_fields}/field/date.rb +16 -12
  11. data/app/models/active_fields/field/date_array.rb +36 -0
  12. data/app/models/active_fields/field/date_time.rb +77 -0
  13. data/app/models/active_fields/field/date_time_array.rb +61 -0
  14. data/app/models/active_fields/field/decimal.rb +75 -0
  15. data/app/models/active_fields/field/decimal_array.rb +59 -0
  16. data/{lib/active_fields/models → app/models/active_fields}/field/enum.rb +34 -17
  17. data/app/models/active_fields/field/enum_array.rb +64 -0
  18. data/{lib/active_fields/models → app/models/active_fields}/field/integer.rb +16 -12
  19. data/app/models/active_fields/field/integer_array.rb +36 -0
  20. data/{lib/active_fields/models → app/models/active_fields}/field/text.rb +16 -12
  21. data/app/models/active_fields/field/text_array.rb +39 -0
  22. data/app/models/active_fields/value.rb +15 -0
  23. data/app/models/concerns/active_fields/customizable_concern.rb +94 -0
  24. data/app/models/concerns/active_fields/field_array_concern.rb +25 -0
  25. data/app/models/concerns/active_fields/field_concern.rb +111 -0
  26. data/app/models/concerns/active_fields/value_concern.rb +83 -0
  27. data/config/routes.rb +4 -0
  28. data/{lib/generators/active_fields/install/templates/create_active_fields_tables.rb.tt → db/migrate/20240229230000_create_active_fields_tables.rb} +8 -6
  29. data/lib/active_fields/casters/base_caster.rb +8 -2
  30. data/lib/active_fields/casters/boolean_caster.rb +0 -2
  31. data/lib/active_fields/casters/date_array_caster.rb +0 -2
  32. data/lib/active_fields/casters/date_caster.rb +8 -8
  33. data/lib/active_fields/casters/date_time_array_caster.rb +19 -0
  34. data/lib/active_fields/casters/date_time_caster.rb +43 -0
  35. data/lib/active_fields/casters/decimal_array_caster.rb +0 -2
  36. data/lib/active_fields/casters/decimal_caster.rb +10 -4
  37. data/lib/active_fields/casters/enum_array_caster.rb +13 -3
  38. data/lib/active_fields/casters/enum_caster.rb +15 -3
  39. data/lib/active_fields/casters/integer_array_caster.rb +0 -2
  40. data/lib/active_fields/casters/integer_caster.rb +2 -4
  41. data/lib/active_fields/casters/text_array_caster.rb +0 -2
  42. data/lib/active_fields/casters/text_caster.rb +0 -2
  43. data/lib/active_fields/config.rb +61 -0
  44. data/lib/active_fields/customizable_config.rb +24 -0
  45. data/lib/active_fields/engine.rb +13 -0
  46. data/lib/active_fields/has_active_fields.rb +19 -0
  47. data/lib/active_fields/validators/base_validator.rb +7 -3
  48. data/lib/active_fields/validators/boolean_validator.rb +2 -4
  49. data/lib/active_fields/validators/date_array_validator.rb +3 -5
  50. data/lib/active_fields/validators/date_time_array_validator.rb +26 -0
  51. data/lib/active_fields/validators/date_time_validator.rb +19 -0
  52. data/lib/active_fields/validators/date_validator.rb +3 -5
  53. data/lib/active_fields/validators/decimal_array_validator.rb +2 -4
  54. data/lib/active_fields/validators/decimal_validator.rb +2 -4
  55. data/lib/active_fields/validators/enum_array_validator.rb +3 -5
  56. data/lib/active_fields/validators/enum_validator.rb +3 -5
  57. data/lib/active_fields/validators/integer_array_validator.rb +2 -4
  58. data/lib/active_fields/validators/integer_validator.rb +2 -4
  59. data/lib/active_fields/validators/text_array_validator.rb +2 -4
  60. data/lib/active_fields/validators/text_validator.rb +2 -4
  61. data/lib/active_fields/version.rb +1 -1
  62. data/lib/active_fields.rb +47 -7
  63. data/lib/generators/active_fields/install/USAGE +1 -1
  64. data/lib/generators/active_fields/install/install_generator.rb +14 -18
  65. data/lib/tasks/active_fields_tasks.rake +6 -0
  66. metadata +49 -36
  67. data/lib/active_fields/models/concerns/concern.rb +0 -16
  68. data/lib/active_fields/models/concerns/customizable.rb +0 -83
  69. data/lib/active_fields/models/concerns/field_concern.rb +0 -79
  70. data/lib/active_fields/models/concerns/value_concern.rb +0 -50
  71. data/lib/active_fields/models/field/date_array.rb +0 -44
  72. data/lib/active_fields/models/field/decimal.rb +0 -48
  73. data/lib/active_fields/models/field/decimal_array.rb +0 -44
  74. data/lib/active_fields/models/field/enum_array.rb +0 -59
  75. data/lib/active_fields/models/field/integer_array.rb +0 -34
  76. data/lib/active_fields/models/field/text_array.rb +0 -35
  77. data/lib/active_fields/models/field.rb +0 -27
  78. data/lib/active_fields/models/value.rb +0 -30
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Usanov
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-05-19 00:00:00.000000000 Z
12
+ date: 2024-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: activerecord
15
+ name: rails
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - ">="
@@ -26,21 +26,21 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  version: '6.0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: activesupport
29
+ name: factory_bot
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: '6.0'
35
- type: :runtime
34
+ version: '0'
35
+ type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: '6.0'
41
+ version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: database_cleaner-active_record
43
+ name: pg
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
@@ -54,7 +54,7 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
- name: factory_bot
57
+ name: rake
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="
@@ -68,7 +68,7 @@ dependencies:
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  - !ruby/object:Gem::Dependency
71
- name: pg
71
+ name: rspec-rails
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - ">="
@@ -82,7 +82,7 @@ dependencies:
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  - !ruby/object:Gem::Dependency
85
- name: rake
85
+ name: rubocop
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
88
  - - ">="
@@ -96,7 +96,7 @@ dependencies:
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  - !ruby/object:Gem::Dependency
99
- name: rspec
99
+ name: rubocop-factory_bot
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
102
  - - ">="
@@ -110,7 +110,7 @@ dependencies:
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  - !ruby/object:Gem::Dependency
113
- name: rubocop
113
+ name: rubocop-performance
114
114
  requirement: !ruby/object:Gem::Requirement
115
115
  requirements:
116
116
  - - ">="
@@ -124,7 +124,7 @@ dependencies:
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
126
  - !ruby/object:Gem::Dependency
127
- name: rubocop-performance
127
+ name: rubocop-rails
128
128
  requirement: !ruby/object:Gem::Requirement
129
129
  requirements:
130
130
  - - ">="
@@ -138,7 +138,7 @@ dependencies:
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0'
140
140
  - !ruby/object:Gem::Dependency
141
- name: rubocop-rails
141
+ name: rubocop-rake
142
142
  requirement: !ruby/object:Gem::Requirement
143
143
  requirements:
144
144
  - - ">="
@@ -152,7 +152,7 @@ dependencies:
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0'
154
154
  - !ruby/object:Gem::Dependency
155
- name: rubocop-rake
155
+ name: rubocop-rspec
156
156
  requirement: !ruby/object:Gem::Requirement
157
157
  requirements:
158
158
  - - ">="
@@ -166,7 +166,7 @@ dependencies:
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0'
168
168
  - !ruby/object:Gem::Dependency
169
- name: rubocop-rspec
169
+ name: rubocop-rspec_rails
170
170
  requirement: !ruby/object:Gem::Requirement
171
171
  requirements:
172
172
  - - ">="
@@ -206,11 +206,35 @@ files:
206
206
  - LICENSE
207
207
  - README.md
208
208
  - Rakefile
209
+ - app/models/active_fields/application_record.rb
210
+ - app/models/active_fields/field/base.rb
211
+ - app/models/active_fields/field/boolean.rb
212
+ - app/models/active_fields/field/date.rb
213
+ - app/models/active_fields/field/date_array.rb
214
+ - app/models/active_fields/field/date_time.rb
215
+ - app/models/active_fields/field/date_time_array.rb
216
+ - app/models/active_fields/field/decimal.rb
217
+ - app/models/active_fields/field/decimal_array.rb
218
+ - app/models/active_fields/field/enum.rb
219
+ - app/models/active_fields/field/enum_array.rb
220
+ - app/models/active_fields/field/integer.rb
221
+ - app/models/active_fields/field/integer_array.rb
222
+ - app/models/active_fields/field/text.rb
223
+ - app/models/active_fields/field/text_array.rb
224
+ - app/models/active_fields/value.rb
225
+ - app/models/concerns/active_fields/customizable_concern.rb
226
+ - app/models/concerns/active_fields/field_array_concern.rb
227
+ - app/models/concerns/active_fields/field_concern.rb
228
+ - app/models/concerns/active_fields/value_concern.rb
229
+ - config/routes.rb
230
+ - db/migrate/20240229230000_create_active_fields_tables.rb
209
231
  - lib/active_fields.rb
210
232
  - lib/active_fields/casters/base_caster.rb
211
233
  - lib/active_fields/casters/boolean_caster.rb
212
234
  - lib/active_fields/casters/date_array_caster.rb
213
235
  - lib/active_fields/casters/date_caster.rb
236
+ - lib/active_fields/casters/date_time_array_caster.rb
237
+ - lib/active_fields/casters/date_time_caster.rb
214
238
  - lib/active_fields/casters/decimal_array_caster.rb
215
239
  - lib/active_fields/casters/decimal_caster.rb
216
240
  - lib/active_fields/casters/enum_array_caster.rb
@@ -219,26 +243,15 @@ files:
219
243
  - lib/active_fields/casters/integer_caster.rb
220
244
  - lib/active_fields/casters/text_array_caster.rb
221
245
  - lib/active_fields/casters/text_caster.rb
222
- - lib/active_fields/models/concerns/concern.rb
223
- - lib/active_fields/models/concerns/customizable.rb
224
- - lib/active_fields/models/concerns/field_concern.rb
225
- - lib/active_fields/models/concerns/value_concern.rb
226
- - lib/active_fields/models/field.rb
227
- - lib/active_fields/models/field/boolean.rb
228
- - lib/active_fields/models/field/date.rb
229
- - lib/active_fields/models/field/date_array.rb
230
- - lib/active_fields/models/field/decimal.rb
231
- - lib/active_fields/models/field/decimal_array.rb
232
- - lib/active_fields/models/field/enum.rb
233
- - lib/active_fields/models/field/enum_array.rb
234
- - lib/active_fields/models/field/integer.rb
235
- - lib/active_fields/models/field/integer_array.rb
236
- - lib/active_fields/models/field/text.rb
237
- - lib/active_fields/models/field/text_array.rb
238
- - lib/active_fields/models/value.rb
246
+ - lib/active_fields/config.rb
247
+ - lib/active_fields/customizable_config.rb
248
+ - lib/active_fields/engine.rb
249
+ - lib/active_fields/has_active_fields.rb
239
250
  - lib/active_fields/validators/base_validator.rb
240
251
  - lib/active_fields/validators/boolean_validator.rb
241
252
  - lib/active_fields/validators/date_array_validator.rb
253
+ - lib/active_fields/validators/date_time_array_validator.rb
254
+ - lib/active_fields/validators/date_time_validator.rb
242
255
  - lib/active_fields/validators/date_validator.rb
243
256
  - lib/active_fields/validators/decimal_array_validator.rb
244
257
  - lib/active_fields/validators/decimal_validator.rb
@@ -251,7 +264,7 @@ files:
251
264
  - lib/active_fields/version.rb
252
265
  - lib/generators/active_fields/install/USAGE
253
266
  - lib/generators/active_fields/install/install_generator.rb
254
- - lib/generators/active_fields/install/templates/create_active_fields_tables.rb.tt
267
+ - lib/tasks/active_fields_tasks.rake
255
268
  - sig/active_fields.rbs
256
269
  homepage: https://github.com/lassoid/active_fields
257
270
  licenses:
@@ -275,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
275
288
  - !ruby/object:Gem::Version
276
289
  version: '0'
277
290
  requirements: []
278
- rubygems_version: 3.5.9
291
+ rubygems_version: 3.5.11
279
292
  signing_key:
280
293
  specification_version: 4
281
294
  summary: Add custom fields to ActiveRecord models at runtime.
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Mix-in that provides ActiveRecord models with active fields functionality
4
- module ActiveFields
5
- module Concern
6
- extend ActiveSupport::Concern
7
-
8
- class_methods do
9
- def has_active_fields(options = {})
10
- include ActiveFields::Customizable
11
-
12
- @__active_fields_options__ = options.dup
13
- end
14
- end
15
- end
16
- end
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Mix-in that adds the active fields functionality to the ActiveRecord model
4
- module ActiveFields
5
- module Customizable
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- # active_values association for the owner record.
10
- # We skip built-in autosave because it doesn't work if the owner record isn't changed.
11
- # Instead, we implement our own autosave callback: `save_changed_active_values`.
12
- has_many :active_values,
13
- class_name: "ActiveFields::Value",
14
- as: :customizable,
15
- inverse_of: :customizable,
16
- autosave: false,
17
- dependent: :destroy
18
-
19
- # Firstly, we build active_values that hasn't been already created.
20
- # Than, we set values for active_values whose values should be changed
21
- # according to `active_values_attributes`.
22
- before_validation :initialize_active_values
23
-
24
- # Save all changed active_values on the owner record save.
25
- after_save :save_changed_active_values
26
-
27
- # We always validate active_values on the owner record change,
28
- # as if they are just an ordinary record columns.
29
- validates_associated :active_values
30
-
31
- # This virtual attribute is used for setting active_values values.
32
- # Keys are active_fields names,
33
- # values are values for corresponding active_values of the record.
34
- attr_reader :active_values_attributes
35
- end
36
-
37
- def active_fields
38
- ActiveFields::Field.for(model_name.name)
39
- end
40
-
41
- # Convert hash keys to strings for easier access by fields names.
42
- def active_values_attributes=(value)
43
- @active_values_attributes =
44
- if value.respond_to?(:to_h)
45
- value.to_h.transform_keys(&:to_s)
46
- else
47
- value
48
- end
49
- end
50
-
51
- private
52
-
53
- def initialize_active_values
54
- active_fields.each do |active_field|
55
- active_value =
56
- find_active_value_by_field(active_field) ||
57
- active_values.new(active_field: active_field, value: active_field.default_value)
58
-
59
- next unless active_values_attributes.is_a?(Hash)
60
- next unless active_values_attributes.key?(active_field.name)
61
-
62
- active_value.value = active_values_attributes[active_field.name]
63
- end
64
- end
65
-
66
- def save_changed_active_values
67
- active_values.each do |active_value|
68
- next unless active_value.new_record? || active_value.changed?
69
-
70
- # For new records association id isn't set right, so we force reassignment of the customizable
71
- active_value.customizable = self
72
-
73
- # Do not validate active values twice,
74
- # because they have already been validated by `validates_associated`.
75
- active_value.save!(validate: false)
76
- end
77
- end
78
-
79
- def find_active_value_by_field(active_field)
80
- active_values.find { |active_value| active_value.active_field_id == active_field.id }
81
- end
82
- end
83
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Mix-in with a base logic for the active fields model
4
- module ActiveFields
5
- module FieldConcern
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- has_many :active_values,
10
- class_name: "ActiveFields::Value",
11
- foreign_key: :active_field_id,
12
- inverse_of: :active_field,
13
- dependent: :destroy
14
-
15
- scope :for, ->(customizable_type) { where(customizable_type: customizable_type) }
16
-
17
- validates :type, presence: true
18
- validates :name, presence: true, uniqueness: { scope: :customizable_type }
19
- validates :name, format: { with: /\A[a-z0-9_]+\z/ }, allow_blank: true
20
- validate :validate_default_value
21
-
22
- after_create :add_field_to_records
23
- after_initialize :set_defaults
24
- end
25
-
26
- def value_validator_class
27
- @value_validator_class ||= "ActiveFields::Validators::#{model_name.name.demodulize}Validator".constantize
28
- end
29
-
30
- def value_validator
31
- value_validator_class.new(self)
32
- end
33
-
34
- def value_caster_class
35
- @value_caster_class ||= "ActiveFields::Casters::#{model_name.name.demodulize}Caster".constantize
36
- end
37
-
38
- def value_caster
39
- value_caster_class.new
40
- end
41
-
42
- def customizable_model
43
- customizable_type.constantize
44
- end
45
-
46
- def default_value=(v)
47
- super(value_caster.serialize(v))
48
- end
49
-
50
- def default_value
51
- value_caster.deserialize(super)
52
- end
53
-
54
- private
55
-
56
- def validate_default_value
57
- validator = value_validator
58
- return if validator.validate(default_value)
59
-
60
- validator.errors.each do |error|
61
- if error.is_a?(Array) && error.size == 2 && error.first.is_a?(Symbol) && error.last.is_a?(Hash)
62
- errors.add(:default_value, error.first, **error.last)
63
- elsif error.is_a?(Symbol)
64
- errors.add(:default_value, *error)
65
- else
66
- raise ArgumentError
67
- end
68
- end
69
- end
70
-
71
- def add_field_to_records
72
- customizable_model.find_each do |record|
73
- ActiveFields::Value.create!(active_field: self, customizable: record, value: default_value)
74
- end
75
- end
76
-
77
- def set_defaults; end
78
- end
79
- end
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Mix-in with a base logic for the active fields value model
4
- module ActiveFields
5
- module ValueConcern
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- belongs_to :customizable, polymorphic: true, optional: false, inverse_of: :active_values
10
- belongs_to :active_field, class_name: "ActiveFields::Field", optional: false, inverse_of: :active_values
11
-
12
- validates :active_field_id, uniqueness: { scope: %i[customizable_id customizable_type] }
13
- validate :validate_value
14
- validate :validate_customizable_allowed
15
- end
16
-
17
- def value=(v)
18
- super(active_field&.value_caster&.serialize(v))
19
- end
20
-
21
- def value
22
- active_field&.value_caster&.deserialize(super)
23
- end
24
-
25
- private
26
-
27
- def validate_value
28
- return if (validator = active_field&.value_validator).nil?
29
- return if validator.validate(value)
30
-
31
- validator.errors.each do |error|
32
- if error.is_a?(Array) && error.size == 2 && error.first.is_a?(Symbol) && error.last.is_a?(Hash)
33
- errors.add(:value, error.first, **error.last)
34
- elsif error.is_a?(Symbol)
35
- errors.add(:value, *error)
36
- else
37
- raise ArgumentError
38
- end
39
- end
40
- end
41
-
42
- def validate_customizable_allowed
43
- return true if active_field.nil?
44
- return true if customizable_type == active_field.customizable_type
45
-
46
- errors.add(:customizable, :invalid)
47
- false
48
- end
49
- end
50
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../field"
4
-
5
- module ActiveFields
6
- class Field
7
- class DateArray < ActiveFields::Field
8
- store_accessor :options, :min_size, :max_size, :min, :max
9
-
10
- # attribute :min_size, :integer
11
- # attribute :max_size, :integer
12
- # attribute :min, :date
13
- # attribute :max, :date
14
-
15
- validates :min_size, comparison: { greater_than_or_equal_to: 0 }, allow_nil: true
16
- validates :max_size, comparison: { greater_than_or_equal_to: ->(r) { r.min_size || 0 } }, allow_nil: true
17
- validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
18
-
19
- %i[min_size max_size].each do |column|
20
- define_method(column) do
21
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
22
- end
23
-
24
- define_method(:"#{column}=") do |other|
25
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
26
- end
27
- end
28
-
29
- %i[min max].each do |column|
30
- define_method(column) do
31
- ActiveFields::Casters::DateCaster.new.deserialize(super())
32
- end
33
-
34
- define_method(:"#{column}=") do |other|
35
- super(ActiveFields::Casters::DateCaster.new.serialize(other))
36
- end
37
- end
38
-
39
- private
40
-
41
- def set_defaults; end
42
- end
43
- end
44
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../field"
4
-
5
- module ActiveFields
6
- class Field
7
- class Decimal < ActiveFields::Field
8
- store_accessor :options, :required, :min, :max
9
-
10
- # attribute :required, :boolean, default: false
11
- # attribute :min, :decimal
12
- # attribute :max, :decimal
13
-
14
- validates :required, exclusion: [nil]
15
- validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
16
-
17
- %i[required].each do |column|
18
- define_method(column) do
19
- ActiveFields::Casters::BooleanCaster.new.deserialize(super())
20
- end
21
-
22
- define_method(:"#{column}?") do
23
- !!public_send(column)
24
- end
25
-
26
- define_method(:"#{column}=") do |other|
27
- super(ActiveFields::Casters::BooleanCaster.new.serialize(other))
28
- end
29
- end
30
-
31
- %i[min max].each do |column|
32
- define_method(column) do
33
- ActiveFields::Casters::DecimalCaster.new.deserialize(super())
34
- end
35
-
36
- define_method(:"#{column}=") do |other|
37
- super(ActiveFields::Casters::DecimalCaster.new.serialize(other))
38
- end
39
- end
40
-
41
- private
42
-
43
- def set_defaults
44
- self.required ||= false
45
- end
46
- end
47
- end
48
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../field"
4
-
5
- module ActiveFields
6
- class Field
7
- class DecimalArray < ActiveFields::Field
8
- store_accessor :options, :min_size, :max_size, :min, :max
9
-
10
- # attribute :min_size, :integer
11
- # attribute :max_size, :integer
12
- # attribute :min, :decimal
13
- # attribute :max, :decimal
14
-
15
- validates :min_size, comparison: { greater_than_or_equal_to: 0 }, allow_nil: true
16
- validates :max_size, comparison: { greater_than_or_equal_to: ->(r) { r.min_size || 0 } }, allow_nil: true
17
- validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
18
-
19
- %i[min_size max_size].each do |column|
20
- define_method(column) do
21
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
22
- end
23
-
24
- define_method(:"#{column}=") do |other|
25
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
26
- end
27
- end
28
-
29
- %i[min max].each do |column|
30
- define_method(column) do
31
- ActiveFields::Casters::DecimalCaster.new.deserialize(super())
32
- end
33
-
34
- define_method(:"#{column}=") do |other|
35
- super(ActiveFields::Casters::DecimalCaster.new.serialize(other))
36
- end
37
- end
38
-
39
- private
40
-
41
- def set_defaults; end
42
- end
43
- end
44
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../field"
4
-
5
- module ActiveFields
6
- class Field
7
- class EnumArray < ActiveFields::Field
8
- store_accessor :options, :min_size, :max_size, :allowed_values
9
-
10
- # attribute :min_size, :integer
11
- # attribute :max_size, :integer
12
- # attribute :allowed_values, :string, array: true, default: []
13
-
14
- validates :min_size, comparison: { greater_than_or_equal_to: 0 }, allow_nil: true
15
- validates :max_size, comparison: { greater_than_or_equal_to: ->(r) { r.min_size || 0 } }, allow_nil: true
16
- validate :validate_allowed_values
17
-
18
- %i[min_size max_size].each do |column|
19
- define_method(column) do
20
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
21
- end
22
-
23
- define_method(:"#{column}=") do |other|
24
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
25
- end
26
- end
27
-
28
- %i[allowed_values].each do |column|
29
- define_method(column) do
30
- ActiveFields::Casters::TextArrayCaster.new.deserialize(super())
31
- end
32
-
33
- define_method(:"#{column}=") do |other|
34
- super(ActiveFields::Casters::TextArrayCaster.new.serialize(other))
35
- end
36
- end
37
-
38
- private
39
-
40
- def validate_allowed_values
41
- if allowed_values.nil?
42
- errors.add(:allowed_values, :blank)
43
- elsif allowed_values.is_a?(Array)
44
- if allowed_values.empty?
45
- errors.add(:allowed_values, :blank)
46
- elsif allowed_values.any? { !_1.is_a?(String) }
47
- errors.add(:allowed_values, :invalid)
48
- end
49
- else
50
- errors.add(:allowed_values, :invalid)
51
- end
52
- end
53
-
54
- def set_defaults
55
- self.allowed_values ||= []
56
- end
57
- end
58
- end
59
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../field"
4
-
5
- module ActiveFields
6
- class Field
7
- class IntegerArray < ActiveFields::Field
8
- store_accessor :options, :min_size, :max_size, :min, :max
9
-
10
- # attribute :min_size, :integer
11
- # attribute :max_size, :integer
12
- # attribute :min, :integer
13
- # attribute :max, :integer
14
-
15
- validates :min_size, comparison: { greater_than_or_equal_to: 0 }, allow_nil: true
16
- validates :max_size, comparison: { greater_than_or_equal_to: ->(r) { r.min_size || 0 } }, allow_nil: true
17
- validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
18
-
19
- %i[min_size max_size min max].each do |column|
20
- define_method(column) do
21
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
22
- end
23
-
24
- define_method(:"#{column}=") do |other|
25
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
26
- end
27
- end
28
-
29
- private
30
-
31
- def set_defaults; end
32
- end
33
- end
34
- end