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.
- checksums.yaml +4 -4
- data/.rspec +0 -1
- data/.rubocop.yml +3 -15
- data/CHANGELOG.md +19 -0
- data/README.md +633 -0
- data/Rakefile +7 -0
- data/app/models/active_fields/application_record.rb +7 -0
- data/app/models/active_fields/field/base.rb +17 -0
- data/{lib/active_fields/models → app/models/active_fields}/field/boolean.rb +14 -9
- data/{lib/active_fields/models → app/models/active_fields}/field/date.rb +16 -12
- data/app/models/active_fields/field/date_array.rb +36 -0
- data/app/models/active_fields/field/date_time.rb +77 -0
- data/app/models/active_fields/field/date_time_array.rb +61 -0
- data/app/models/active_fields/field/decimal.rb +75 -0
- data/app/models/active_fields/field/decimal_array.rb +59 -0
- data/{lib/active_fields/models → app/models/active_fields}/field/enum.rb +34 -17
- data/app/models/active_fields/field/enum_array.rb +64 -0
- data/{lib/active_fields/models → app/models/active_fields}/field/integer.rb +16 -12
- data/app/models/active_fields/field/integer_array.rb +36 -0
- data/{lib/active_fields/models → app/models/active_fields}/field/text.rb +16 -12
- data/app/models/active_fields/field/text_array.rb +39 -0
- data/app/models/active_fields/value.rb +15 -0
- data/app/models/concerns/active_fields/customizable_concern.rb +94 -0
- data/app/models/concerns/active_fields/field_array_concern.rb +25 -0
- data/app/models/concerns/active_fields/field_concern.rb +111 -0
- data/app/models/concerns/active_fields/value_concern.rb +83 -0
- data/config/routes.rb +4 -0
- data/{lib/generators/active_fields/install/templates/create_active_fields_tables.rb.tt → db/migrate/20240229230000_create_active_fields_tables.rb} +8 -6
- data/lib/active_fields/casters/base_caster.rb +8 -2
- data/lib/active_fields/casters/boolean_caster.rb +0 -2
- data/lib/active_fields/casters/date_array_caster.rb +0 -2
- data/lib/active_fields/casters/date_caster.rb +8 -8
- data/lib/active_fields/casters/date_time_array_caster.rb +19 -0
- data/lib/active_fields/casters/date_time_caster.rb +43 -0
- data/lib/active_fields/casters/decimal_array_caster.rb +0 -2
- data/lib/active_fields/casters/decimal_caster.rb +10 -4
- data/lib/active_fields/casters/enum_array_caster.rb +13 -3
- data/lib/active_fields/casters/enum_caster.rb +15 -3
- data/lib/active_fields/casters/integer_array_caster.rb +0 -2
- data/lib/active_fields/casters/integer_caster.rb +2 -4
- data/lib/active_fields/casters/text_array_caster.rb +0 -2
- data/lib/active_fields/casters/text_caster.rb +0 -2
- data/lib/active_fields/config.rb +61 -0
- data/lib/active_fields/customizable_config.rb +24 -0
- data/lib/active_fields/engine.rb +13 -0
- data/lib/active_fields/has_active_fields.rb +19 -0
- data/lib/active_fields/validators/base_validator.rb +7 -3
- data/lib/active_fields/validators/boolean_validator.rb +2 -4
- data/lib/active_fields/validators/date_array_validator.rb +3 -5
- data/lib/active_fields/validators/date_time_array_validator.rb +26 -0
- data/lib/active_fields/validators/date_time_validator.rb +19 -0
- data/lib/active_fields/validators/date_validator.rb +3 -5
- data/lib/active_fields/validators/decimal_array_validator.rb +2 -4
- data/lib/active_fields/validators/decimal_validator.rb +2 -4
- data/lib/active_fields/validators/enum_array_validator.rb +3 -5
- data/lib/active_fields/validators/enum_validator.rb +3 -5
- data/lib/active_fields/validators/integer_array_validator.rb +2 -4
- data/lib/active_fields/validators/integer_validator.rb +2 -4
- data/lib/active_fields/validators/text_array_validator.rb +2 -4
- data/lib/active_fields/validators/text_validator.rb +2 -4
- data/lib/active_fields/version.rb +1 -1
- data/lib/active_fields.rb +47 -7
- data/lib/generators/active_fields/install/USAGE +1 -1
- data/lib/generators/active_fields/install/install_generator.rb +14 -18
- data/lib/tasks/active_fields_tasks.rake +6 -0
- metadata +49 -36
- data/lib/active_fields/models/concerns/concern.rb +0 -16
- data/lib/active_fields/models/concerns/customizable.rb +0 -83
- data/lib/active_fields/models/concerns/field_concern.rb +0 -79
- data/lib/active_fields/models/concerns/value_concern.rb +0 -50
- data/lib/active_fields/models/field/date_array.rb +0 -44
- data/lib/active_fields/models/field/decimal.rb +0 -48
- data/lib/active_fields/models/field/decimal_array.rb +0 -44
- data/lib/active_fields/models/field/enum_array.rb +0 -59
- data/lib/active_fields/models/field/integer_array.rb +0 -34
- data/lib/active_fields/models/field/text_array.rb +0 -35
- data/lib/active_fields/models/field.rb +0 -27
- data/lib/active_fields/models/value.rb +0 -30
@@ -1,21 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "../field"
|
4
|
-
|
5
3
|
module ActiveFields
|
6
|
-
|
7
|
-
class Boolean < ActiveFields
|
8
|
-
|
4
|
+
module Field
|
5
|
+
class Boolean < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
validator: {
|
8
|
+
class_name: "ActiveFields::Validators::BooleanValidator",
|
9
|
+
options: -> { { required: required?, nullable: nullable? } },
|
10
|
+
},
|
11
|
+
caster: {
|
12
|
+
class_name: "ActiveFields::Casters::BooleanCaster",
|
13
|
+
},
|
14
|
+
)
|
9
15
|
|
10
|
-
|
11
|
-
# attribute :nullable, :boolean, default: false
|
16
|
+
store_accessor :options, :required, :nullable
|
12
17
|
|
13
18
|
validates :required, exclusion: [nil]
|
14
19
|
validates :nullable, exclusion: [nil]
|
15
20
|
|
16
21
|
%i[required nullable].each do |column|
|
17
22
|
define_method(column) do
|
18
|
-
|
23
|
+
Casters::BooleanCaster.new.deserialize(super())
|
19
24
|
end
|
20
25
|
|
21
26
|
define_method(:"#{column}?") do
|
@@ -23,7 +28,7 @@ module ActiveFields
|
|
23
28
|
end
|
24
29
|
|
25
30
|
define_method(:"#{column}=") do |other|
|
26
|
-
super(
|
31
|
+
super(Casters::BooleanCaster.new.serialize(other))
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
@@ -1,22 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "../field"
|
4
|
-
|
5
3
|
module ActiveFields
|
6
|
-
|
7
|
-
class Date < ActiveFields
|
8
|
-
|
4
|
+
module Field
|
5
|
+
class Date < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
validator: {
|
8
|
+
class_name: "ActiveFields::Validators::DateValidator",
|
9
|
+
options: -> { { required: required?, min: min, max: max } },
|
10
|
+
},
|
11
|
+
caster: {
|
12
|
+
class_name: "ActiveFields::Casters::DateCaster",
|
13
|
+
},
|
14
|
+
)
|
9
15
|
|
10
|
-
|
11
|
-
# attribute :min, :date
|
12
|
-
# attribute :max, :date
|
16
|
+
store_accessor :options, :required, :min, :max
|
13
17
|
|
14
18
|
validates :required, exclusion: [nil]
|
15
19
|
validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
|
16
20
|
|
17
21
|
%i[required].each do |column|
|
18
22
|
define_method(column) do
|
19
|
-
|
23
|
+
Casters::BooleanCaster.new.deserialize(super())
|
20
24
|
end
|
21
25
|
|
22
26
|
define_method(:"#{column}?") do
|
@@ -24,17 +28,17 @@ module ActiveFields
|
|
24
28
|
end
|
25
29
|
|
26
30
|
define_method(:"#{column}=") do |other|
|
27
|
-
super(
|
31
|
+
super(Casters::BooleanCaster.new.serialize(other))
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
31
35
|
%i[min max].each do |column|
|
32
36
|
define_method(column) do
|
33
|
-
|
37
|
+
Casters::DateCaster.new.deserialize(super())
|
34
38
|
end
|
35
39
|
|
36
40
|
define_method(:"#{column}=") do |other|
|
37
|
-
super(
|
41
|
+
super(Casters::DateCaster.new.serialize(other))
|
38
42
|
end
|
39
43
|
end
|
40
44
|
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveFields
|
4
|
+
module Field
|
5
|
+
class DateArray < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
array: true,
|
8
|
+
validator: {
|
9
|
+
class_name: "ActiveFields::Validators::DateArrayValidator",
|
10
|
+
options: -> { { min: min, max: max, min_size: min_size, max_size: max_size } },
|
11
|
+
},
|
12
|
+
caster: {
|
13
|
+
class_name: "ActiveFields::Casters::DateArrayCaster",
|
14
|
+
},
|
15
|
+
)
|
16
|
+
|
17
|
+
store_accessor :options, :min, :max
|
18
|
+
|
19
|
+
validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
|
20
|
+
|
21
|
+
%i[min max].each do |column|
|
22
|
+
define_method(column) do
|
23
|
+
Casters::DateCaster.new.deserialize(super())
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method(:"#{column}=") do |other|
|
27
|
+
super(Casters::DateCaster.new.serialize(other))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def set_defaults; end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveFields
|
4
|
+
module Field
|
5
|
+
class DateTime < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
validator: {
|
8
|
+
class_name: "ActiveFields::Validators::DateTimeValidator",
|
9
|
+
options: -> { { required: required?, min: min, max: max } },
|
10
|
+
},
|
11
|
+
caster: {
|
12
|
+
class_name: "ActiveFields::Casters::DateTimeCaster",
|
13
|
+
options: -> { { precision: precision } },
|
14
|
+
},
|
15
|
+
)
|
16
|
+
|
17
|
+
store_accessor :options, :required, :min, :max, :precision
|
18
|
+
|
19
|
+
validates :required, exclusion: [nil]
|
20
|
+
validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
|
21
|
+
validates :precision,
|
22
|
+
comparison: { greater_than_or_equal_to: 0, less_than_or_equal_to: Casters::DateTimeCaster::MAX_PRECISION },
|
23
|
+
allow_nil: true
|
24
|
+
|
25
|
+
# If precision is set after attributes that depend on it, deserialization will work correctly,
|
26
|
+
# but an incorrect internal value may be saved in the DB.
|
27
|
+
# This callback reassigns the internal values of the attributes to ensure accuracy.
|
28
|
+
before_save :reapply_precision
|
29
|
+
|
30
|
+
%i[required].each do |column|
|
31
|
+
define_method(column) do
|
32
|
+
Casters::BooleanCaster.new.deserialize(super())
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method(:"#{column}?") do
|
36
|
+
!!public_send(column)
|
37
|
+
end
|
38
|
+
|
39
|
+
define_method(:"#{column}=") do |other|
|
40
|
+
super(Casters::BooleanCaster.new.serialize(other))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
%i[min max].each do |column|
|
45
|
+
define_method(column) do
|
46
|
+
Casters::DateTimeCaster.new(precision: precision).deserialize(super())
|
47
|
+
end
|
48
|
+
|
49
|
+
define_method(:"#{column}=") do |other|
|
50
|
+
super(Casters::DateTimeCaster.new(precision: precision).serialize(other))
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
%i[precision].each do |column|
|
55
|
+
define_method(column) do
|
56
|
+
Casters::IntegerCaster.new.deserialize(super())
|
57
|
+
end
|
58
|
+
|
59
|
+
define_method(:"#{column}=") do |other|
|
60
|
+
super(Casters::IntegerCaster.new.serialize(other))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def set_defaults
|
67
|
+
self.required ||= false
|
68
|
+
end
|
69
|
+
|
70
|
+
def reapply_precision
|
71
|
+
self.min = min
|
72
|
+
self.max = max
|
73
|
+
self.default_value = default_value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveFields
|
4
|
+
module Field
|
5
|
+
class DateTimeArray < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
array: true,
|
8
|
+
validator: {
|
9
|
+
class_name: "ActiveFields::Validators::DateTimeArrayValidator",
|
10
|
+
options: -> { { min: min, max: max, min_size: min_size, max_size: max_size } },
|
11
|
+
},
|
12
|
+
caster: {
|
13
|
+
class_name: "ActiveFields::Casters::DateTimeArrayCaster",
|
14
|
+
options: -> { { precision: precision } },
|
15
|
+
},
|
16
|
+
)
|
17
|
+
|
18
|
+
store_accessor :options, :min, :max, :precision
|
19
|
+
|
20
|
+
validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
|
21
|
+
validates :precision,
|
22
|
+
comparison: { greater_than_or_equal_to: 0, less_than_or_equal_to: Casters::DateTimeCaster::MAX_PRECISION },
|
23
|
+
allow_nil: true
|
24
|
+
|
25
|
+
# If precision is set after attributes that depend on it, deserialization will work correctly,
|
26
|
+
# but an incorrect internal value may be saved in the DB.
|
27
|
+
# This callback reassigns the internal values of the attributes to ensure accuracy.
|
28
|
+
before_save :reapply_precision
|
29
|
+
|
30
|
+
%i[precision].each do |column|
|
31
|
+
define_method(column) do
|
32
|
+
Casters::IntegerCaster.new.deserialize(super())
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method(:"#{column}=") do |other|
|
36
|
+
super(Casters::IntegerCaster.new.serialize(other))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
%i[min max].each do |column|
|
41
|
+
define_method(column) do
|
42
|
+
Casters::DateTimeCaster.new(precision: precision).deserialize(super())
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method(:"#{column}=") do |other|
|
46
|
+
super(Casters::DateTimeCaster.new(precision: precision).serialize(other))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def set_defaults; end
|
53
|
+
|
54
|
+
def reapply_precision
|
55
|
+
self.min = min
|
56
|
+
self.max = max
|
57
|
+
self.default_value = default_value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveFields
|
4
|
+
module Field
|
5
|
+
class Decimal < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
validator: {
|
8
|
+
class_name: "ActiveFields::Validators::DecimalValidator",
|
9
|
+
options: -> { { required: required?, min: min, max: max } },
|
10
|
+
},
|
11
|
+
caster: {
|
12
|
+
class_name: "ActiveFields::Casters::DecimalCaster",
|
13
|
+
options: -> { { precision: precision } },
|
14
|
+
},
|
15
|
+
)
|
16
|
+
|
17
|
+
store_accessor :options, :required, :min, :max, :precision
|
18
|
+
|
19
|
+
validates :required, exclusion: [nil]
|
20
|
+
validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
|
21
|
+
validates :precision, comparison: { greater_than_or_equal_to: 0 }, allow_nil: true
|
22
|
+
|
23
|
+
# If precision is set after attributes that depend on it, deserialization will work correctly,
|
24
|
+
# but an incorrect internal value may be saved in the DB.
|
25
|
+
# This callback reassigns the internal values of the attributes to ensure accuracy.
|
26
|
+
before_save :reapply_precision
|
27
|
+
|
28
|
+
%i[required].each do |column|
|
29
|
+
define_method(column) do
|
30
|
+
Casters::BooleanCaster.new.deserialize(super())
|
31
|
+
end
|
32
|
+
|
33
|
+
define_method(:"#{column}?") do
|
34
|
+
!!public_send(column)
|
35
|
+
end
|
36
|
+
|
37
|
+
define_method(:"#{column}=") do |other|
|
38
|
+
super(Casters::BooleanCaster.new.serialize(other))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
%i[min max].each do |column|
|
43
|
+
define_method(column) do
|
44
|
+
Casters::DecimalCaster.new(precision: precision).deserialize(super())
|
45
|
+
end
|
46
|
+
|
47
|
+
define_method(:"#{column}=") do |other|
|
48
|
+
super(Casters::DecimalCaster.new(precision: precision).serialize(other))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
%i[precision].each do |column|
|
53
|
+
define_method(column) do
|
54
|
+
Casters::IntegerCaster.new.deserialize(super())
|
55
|
+
end
|
56
|
+
|
57
|
+
define_method(:"#{column}=") do |other|
|
58
|
+
super(Casters::IntegerCaster.new.serialize(other))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def set_defaults
|
65
|
+
self.required ||= false
|
66
|
+
end
|
67
|
+
|
68
|
+
def reapply_precision
|
69
|
+
self.min = min
|
70
|
+
self.max = max
|
71
|
+
self.default_value = default_value
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveFields
|
4
|
+
module Field
|
5
|
+
class DecimalArray < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
array: true,
|
8
|
+
validator: {
|
9
|
+
class_name: "ActiveFields::Validators::DecimalArrayValidator",
|
10
|
+
options: -> { { min: min, max: max, min_size: min_size, max_size: max_size } },
|
11
|
+
},
|
12
|
+
caster: {
|
13
|
+
class_name: "ActiveFields::Casters::DecimalArrayCaster",
|
14
|
+
options: -> { { precision: precision } },
|
15
|
+
},
|
16
|
+
)
|
17
|
+
|
18
|
+
store_accessor :options, :min, :max, :precision
|
19
|
+
|
20
|
+
validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
|
21
|
+
validates :precision, comparison: { greater_than_or_equal_to: 0 }, allow_nil: true
|
22
|
+
|
23
|
+
# If precision is set after attributes that depend on it, deserialization will work correctly,
|
24
|
+
# but an incorrect internal value may be saved in the DB.
|
25
|
+
# This callback reassigns the internal values of the attributes to ensure accuracy.
|
26
|
+
before_save :reapply_precision
|
27
|
+
|
28
|
+
%i[precision].each do |column|
|
29
|
+
define_method(column) do
|
30
|
+
Casters::IntegerCaster.new.deserialize(super())
|
31
|
+
end
|
32
|
+
|
33
|
+
define_method(:"#{column}=") do |other|
|
34
|
+
super(Casters::IntegerCaster.new.serialize(other))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
%i[min max].each do |column|
|
39
|
+
define_method(column) do
|
40
|
+
Casters::DecimalCaster.new(precision: precision).deserialize(super())
|
41
|
+
end
|
42
|
+
|
43
|
+
define_method(:"#{column}=") do |other|
|
44
|
+
super(Casters::DecimalCaster.new(precision: precision).serialize(other))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def set_defaults; end
|
51
|
+
|
52
|
+
def reapply_precision
|
53
|
+
self.min = min
|
54
|
+
self.max = max
|
55
|
+
self.default_value = default_value
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -1,21 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "../field"
|
4
|
-
|
5
3
|
module ActiveFields
|
6
|
-
|
7
|
-
class Enum < ActiveFields
|
8
|
-
|
4
|
+
module Field
|
5
|
+
class Enum < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
validator: {
|
8
|
+
class_name: "ActiveFields::Validators::EnumValidator",
|
9
|
+
options: -> { { required: required?, allowed_values: allowed_values } },
|
10
|
+
},
|
11
|
+
caster: {
|
12
|
+
class_name: "ActiveFields::Casters::EnumCaster",
|
13
|
+
},
|
14
|
+
)
|
9
15
|
|
10
|
-
|
11
|
-
# attribute :allowed_values, :string, array: true, default: []
|
16
|
+
store_accessor :options, :required, :allowed_values
|
12
17
|
|
13
18
|
validates :required, exclusion: [nil]
|
14
19
|
validate :validate_allowed_values
|
15
20
|
|
16
21
|
%i[required].each do |column|
|
17
22
|
define_method(column) do
|
18
|
-
|
23
|
+
Casters::BooleanCaster.new.deserialize(super())
|
19
24
|
end
|
20
25
|
|
21
26
|
define_method(:"#{column}?") do
|
@@ -23,17 +28,17 @@ module ActiveFields
|
|
23
28
|
end
|
24
29
|
|
25
30
|
define_method(:"#{column}=") do |other|
|
26
|
-
super(
|
31
|
+
super(Casters::BooleanCaster.new.serialize(other))
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
30
35
|
%i[allowed_values].each do |column|
|
31
36
|
define_method(column) do
|
32
|
-
|
37
|
+
Casters::TextArrayCaster.new.deserialize(super())
|
33
38
|
end
|
34
39
|
|
35
40
|
define_method(:"#{column}=") do |other|
|
36
|
-
super(
|
41
|
+
super(Casters::TextArrayCaster.new.serialize(other))
|
37
42
|
end
|
38
43
|
end
|
39
44
|
|
@@ -42,15 +47,27 @@ module ActiveFields
|
|
42
47
|
def validate_allowed_values
|
43
48
|
if allowed_values.nil?
|
44
49
|
errors.add(:allowed_values, :blank)
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
50
|
+
return false
|
51
|
+
end
|
52
|
+
|
53
|
+
unless allowed_values.is_a?(Array)
|
54
|
+
errors.add(:allowed_values, :invalid)
|
55
|
+
return false
|
56
|
+
end
|
57
|
+
|
58
|
+
if allowed_values.empty?
|
59
|
+
errors.add(:allowed_values, :blank)
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
|
63
|
+
allowed_values.each do |value_element|
|
64
|
+
if value_element.blank?
|
49
65
|
errors.add(:allowed_values, :invalid)
|
66
|
+
return false
|
50
67
|
end
|
51
|
-
else
|
52
|
-
errors.add(:allowed_values, :invalid)
|
53
68
|
end
|
69
|
+
|
70
|
+
true
|
54
71
|
end
|
55
72
|
|
56
73
|
def set_defaults
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveFields
|
4
|
+
module Field
|
5
|
+
class EnumArray < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
array: true,
|
8
|
+
validator: {
|
9
|
+
class_name: "ActiveFields::Validators::EnumArrayValidator",
|
10
|
+
options: -> { { allowed_values: allowed_values, min_size: min_size, max_size: max_size } },
|
11
|
+
},
|
12
|
+
caster: {
|
13
|
+
class_name: "ActiveFields::Casters::EnumArrayCaster",
|
14
|
+
},
|
15
|
+
)
|
16
|
+
|
17
|
+
store_accessor :options, :allowed_values
|
18
|
+
|
19
|
+
validate :validate_allowed_values
|
20
|
+
|
21
|
+
%i[allowed_values].each do |column|
|
22
|
+
define_method(column) do
|
23
|
+
Casters::TextArrayCaster.new.deserialize(super())
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method(:"#{column}=") do |other|
|
27
|
+
super(Casters::TextArrayCaster.new.serialize(other))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def validate_allowed_values
|
34
|
+
if allowed_values.nil?
|
35
|
+
errors.add(:allowed_values, :blank)
|
36
|
+
return false
|
37
|
+
end
|
38
|
+
|
39
|
+
unless allowed_values.is_a?(Array)
|
40
|
+
errors.add(:allowed_values, :invalid)
|
41
|
+
return false
|
42
|
+
end
|
43
|
+
|
44
|
+
if allowed_values.empty?
|
45
|
+
errors.add(:allowed_values, :blank)
|
46
|
+
return false
|
47
|
+
end
|
48
|
+
|
49
|
+
allowed_values.each do |value_element|
|
50
|
+
if value_element.blank?
|
51
|
+
errors.add(:allowed_values, :invalid)
|
52
|
+
return false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_defaults
|
60
|
+
self.allowed_values ||= []
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -1,22 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "../field"
|
4
|
-
|
5
3
|
module ActiveFields
|
6
|
-
|
7
|
-
class Integer < ActiveFields
|
8
|
-
|
4
|
+
module Field
|
5
|
+
class Integer < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
validator: {
|
8
|
+
class_name: "ActiveFields::Validators::IntegerValidator",
|
9
|
+
options: -> { { required: required?, min: min, max: max } },
|
10
|
+
},
|
11
|
+
caster: {
|
12
|
+
class_name: "ActiveFields::Casters::IntegerCaster",
|
13
|
+
},
|
14
|
+
)
|
9
15
|
|
10
|
-
|
11
|
-
# attribute :min, :integer
|
12
|
-
# attribute :max, :integer
|
16
|
+
store_accessor :options, :required, :min, :max
|
13
17
|
|
14
18
|
validates :required, exclusion: [nil]
|
15
19
|
validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
|
16
20
|
|
17
21
|
%i[required].each do |column|
|
18
22
|
define_method(column) do
|
19
|
-
|
23
|
+
Casters::BooleanCaster.new.deserialize(super())
|
20
24
|
end
|
21
25
|
|
22
26
|
define_method(:"#{column}?") do
|
@@ -24,17 +28,17 @@ module ActiveFields
|
|
24
28
|
end
|
25
29
|
|
26
30
|
define_method(:"#{column}=") do |other|
|
27
|
-
super(
|
31
|
+
super(Casters::BooleanCaster.new.serialize(other))
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
31
35
|
%i[min max].each do |column|
|
32
36
|
define_method(column) do
|
33
|
-
|
37
|
+
Casters::IntegerCaster.new.deserialize(super())
|
34
38
|
end
|
35
39
|
|
36
40
|
define_method(:"#{column}=") do |other|
|
37
|
-
super(
|
41
|
+
super(Casters::IntegerCaster.new.serialize(other))
|
38
42
|
end
|
39
43
|
end
|
40
44
|
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveFields
|
4
|
+
module Field
|
5
|
+
class IntegerArray < ActiveFields.config.field_base_class
|
6
|
+
acts_as_active_field(
|
7
|
+
array: true,
|
8
|
+
validator: {
|
9
|
+
class_name: "ActiveFields::Validators::IntegerArrayValidator",
|
10
|
+
options: -> { { min: min, max: max, min_size: min_size, max_size: max_size } },
|
11
|
+
},
|
12
|
+
caster: {
|
13
|
+
class_name: "ActiveFields::Casters::IntegerArrayCaster",
|
14
|
+
},
|
15
|
+
)
|
16
|
+
|
17
|
+
store_accessor :options, :min, :max
|
18
|
+
|
19
|
+
validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min
|
20
|
+
|
21
|
+
%i[min max].each do |column|
|
22
|
+
define_method(column) do
|
23
|
+
Casters::IntegerCaster.new.deserialize(super())
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method(:"#{column}=") do |other|
|
27
|
+
super(Casters::IntegerCaster.new.serialize(other))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def set_defaults; end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|