active_fields 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +0 -1
  3. data/.rubocop.yml +3 -15
  4. data/CHANGELOG.md +7 -0
  5. data/Rakefile +7 -0
  6. data/app/models/active_fields/application_record.rb +7 -0
  7. data/app/models/active_fields/field/base.rb +17 -0
  8. data/{lib/active_fields/models → app/models/active_fields}/field/boolean.rb +4 -6
  9. data/{lib/active_fields/models → app/models/active_fields}/field/date.rb +6 -8
  10. data/{lib/active_fields/models → app/models/active_fields}/field/date_array.rb +6 -8
  11. data/{lib/active_fields/models → app/models/active_fields}/field/decimal.rb +6 -8
  12. data/{lib/active_fields/models → app/models/active_fields}/field/decimal_array.rb +6 -8
  13. data/{lib/active_fields/models → app/models/active_fields}/field/enum.rb +6 -8
  14. data/{lib/active_fields/models → app/models/active_fields}/field/enum_array.rb +6 -8
  15. data/{lib/active_fields/models → app/models/active_fields}/field/integer.rb +6 -8
  16. data/{lib/active_fields/models → app/models/active_fields}/field/integer_array.rb +4 -6
  17. data/{lib/active_fields/models → app/models/active_fields}/field/text.rb +6 -8
  18. data/{lib/active_fields/models → app/models/active_fields}/field/text_array.rb +4 -6
  19. data/app/models/active_fields/value.rb +15 -0
  20. data/{lib/active_fields/models/concerns/customizable.rb → app/models/concerns/active_fields/customizable_concern.rb} +6 -4
  21. data/{lib/active_fields/models/concerns → app/models/concerns/active_fields}/field_concern.rb +14 -3
  22. data/{lib/active_fields/models/concerns → app/models/concerns/active_fields}/value_concern.rb +7 -2
  23. data/config/routes.rb +4 -0
  24. data/{lib/generators/active_fields/install/templates/create_active_fields_tables.rb.tt → db/migrate/20240229230000_create_active_fields_tables.rb} +6 -4
  25. data/lib/active_fields/casters/boolean_caster.rb +0 -2
  26. data/lib/active_fields/casters/date_array_caster.rb +0 -2
  27. data/lib/active_fields/casters/date_caster.rb +0 -2
  28. data/lib/active_fields/casters/decimal_array_caster.rb +0 -2
  29. data/lib/active_fields/casters/decimal_caster.rb +0 -2
  30. data/lib/active_fields/casters/enum_array_caster.rb +0 -2
  31. data/lib/active_fields/casters/enum_caster.rb +0 -2
  32. data/lib/active_fields/casters/integer_array_caster.rb +0 -2
  33. data/lib/active_fields/casters/integer_caster.rb +1 -3
  34. data/lib/active_fields/casters/text_array_caster.rb +0 -2
  35. data/lib/active_fields/casters/text_caster.rb +0 -2
  36. data/lib/active_fields/config.rb +50 -0
  37. data/lib/active_fields/customizable_config.rb +24 -0
  38. data/lib/active_fields/engine.rb +13 -0
  39. data/lib/active_fields/has_active_fields.rb +19 -0
  40. data/lib/active_fields/validators/boolean_validator.rb +0 -2
  41. data/lib/active_fields/validators/date_array_validator.rb +0 -2
  42. data/lib/active_fields/validators/date_validator.rb +0 -2
  43. data/lib/active_fields/validators/decimal_array_validator.rb +0 -2
  44. data/lib/active_fields/validators/decimal_validator.rb +0 -2
  45. data/lib/active_fields/validators/enum_array_validator.rb +0 -2
  46. data/lib/active_fields/validators/enum_validator.rb +0 -2
  47. data/lib/active_fields/validators/integer_array_validator.rb +0 -2
  48. data/lib/active_fields/validators/integer_validator.rb +0 -2
  49. data/lib/active_fields/validators/text_array_validator.rb +0 -2
  50. data/lib/active_fields/validators/text_validator.rb +0 -2
  51. data/lib/active_fields/version.rb +1 -1
  52. data/lib/active_fields.rb +43 -7
  53. data/lib/tasks/active_fields_tasks.rake +6 -0
  54. metadata +52 -34
  55. data/lib/active_fields/models/concerns/concern.rb +0 -16
  56. data/lib/active_fields/models/field.rb +0 -27
  57. data/lib/active_fields/models/value.rb +0 -30
  58. data/lib/generators/active_fields/install/USAGE +0 -5
  59. data/lib/generators/active_fields/install/install_generator.rb +0 -33
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81a03041febfec92f7fdbbb6e9f8b9e1f40cc715744467541fbb0c08011f5a22
4
- data.tar.gz: f6d64ed4e8e703c66e0dd7deff7f41c1c8da60b35ff084b1ca8c20986b8776b5
3
+ metadata.gz: aee118ed966e5912d28dcf8cb2d1db0cdd68d85f7d4fc3bcdf267cc5629c7ff3
4
+ data.tar.gz: 99009dea7a5f940034263a85f5f20f76b84911772677dfc69fe97380bd308563
5
5
  SHA512:
6
- metadata.gz: 660528c8044ef3000116b892cc236de565fe5530f9958b4a0f4ae9c511d2a3c3f4d8944fe6b2894d59f16b6bff6cd529acb6a592100c9e3cc396c0030d2069c6
7
- data.tar.gz: 4843897cd9783e5d5489e0cf8c91d103d3136e23ee754ae447b77705f073e7e35750098a83e713ed77948d164d308eb8a690c206d0d52406c26eba0913817710
6
+ metadata.gz: ca324370030b6028c6a579e4782f37d010e6e01dff7d75054220302e6a36e9aaf35acb30488513f723eadef183ba8fda590890db7bd5f654517ceb03ddfd2a82
7
+ data.tar.gz: df6501f049664003e9d02571c6f6652b8eaacf92718038e98eea1daae7ee08ddba92a028f737fac5d08042a410f06445ef996bcf418844099d4003d568a53ed5
data/.rspec CHANGED
@@ -1,2 +1 @@
1
- --color
2
1
  --require spec_helper
data/.rubocop.yml CHANGED
@@ -1,9 +1,10 @@
1
1
  require:
2
+ # - rubocop-factory_bot
2
3
  - rubocop-performance
3
4
  - rubocop-rails
4
5
  - rubocop-rake
5
6
  - rubocop-rspec
6
- - rubocop-factory_bot
7
+ # - rubocop-rspec_rails
7
8
 
8
9
  inherit_gem:
9
10
  rubocop-shopify: rubocop.yml
@@ -13,7 +14,7 @@ AllCops:
13
14
  TargetRailsVersion: 7.1
14
15
  NewCops: enable
15
16
  Exclude:
16
- - "active_fields.gemspec"
17
+ - "spec/dummy/db/schema.rb"
17
18
 
18
19
  Layout/EmptyLinesAroundAccessModifier:
19
20
  EnforcedStyle: around
@@ -45,16 +46,3 @@ RSpec/ContextWording:
45
46
 
46
47
  RSpec/MultipleExpectations:
47
48
  Enabled: false
48
-
49
- Rails/ApplicationRecord:
50
- Enabled: false
51
-
52
- Rails/Date:
53
- Enabled: false
54
-
55
- RSpec/FilePath:
56
- Enabled: false
57
-
58
- RSpec/SpecFilePathFormat:
59
- Enabled: false
60
-
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2024-06-13
4
+
5
+ - Rewrited as a Rails plugin!
6
+ - Custom field types support
7
+ - Global configuration options for changing field and value classes
8
+ - Per-model configuration option for enabling specific field types only
9
+
3
10
  ## [0.1.0] - 2024-05-19
4
11
 
5
12
  - Initial release
data/Rakefile CHANGED
@@ -1,3 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "bundler/setup"
4
+
5
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
6
+ load "rails/tasks/engine.rake"
7
+
8
+ load "rails/tasks/statistics.rake"
9
+
3
10
  require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveFields
4
+ class ApplicationRecord < ActiveRecord::Base
5
+ self.abstract_class = true
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveFields
4
+ module Field
5
+ # If the field base class has been changed, we should prevent this model from being loaded.
6
+ # Since we cannot remove it entirely, we will not add any functionality to it.
7
+ if ActiveFields.config.field_base_class_changed?
8
+ class Base; end
9
+ else
10
+ class Base < ApplicationRecord
11
+ self.table_name = "active_fields"
12
+
13
+ include FieldConcern
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class Boolean < ActiveFields::Field
4
+ module Field
5
+ class Boolean < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :required, :nullable
9
7
 
10
8
  # attribute :required, :boolean, default: false
@@ -15,7 +13,7 @@ module ActiveFields
15
13
 
16
14
  %i[required nullable].each do |column|
17
15
  define_method(column) do
18
- ActiveFields::Casters::BooleanCaster.new.deserialize(super())
16
+ Casters::BooleanCaster.new.deserialize(super())
19
17
  end
20
18
 
21
19
  define_method(:"#{column}?") do
@@ -23,7 +21,7 @@ module ActiveFields
23
21
  end
24
22
 
25
23
  define_method(:"#{column}=") do |other|
26
- super(ActiveFields::Casters::BooleanCaster.new.serialize(other))
24
+ super(Casters::BooleanCaster.new.serialize(other))
27
25
  end
28
26
  end
29
27
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class Date < ActiveFields::Field
4
+ module Field
5
+ class Date < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :required, :min, :max
9
7
 
10
8
  # attribute :required, :boolean, default: false
@@ -16,7 +14,7 @@ module ActiveFields
16
14
 
17
15
  %i[required].each do |column|
18
16
  define_method(column) do
19
- ActiveFields::Casters::BooleanCaster.new.deserialize(super())
17
+ Casters::BooleanCaster.new.deserialize(super())
20
18
  end
21
19
 
22
20
  define_method(:"#{column}?") do
@@ -24,17 +22,17 @@ module ActiveFields
24
22
  end
25
23
 
26
24
  define_method(:"#{column}=") do |other|
27
- super(ActiveFields::Casters::BooleanCaster.new.serialize(other))
25
+ super(Casters::BooleanCaster.new.serialize(other))
28
26
  end
29
27
  end
30
28
 
31
29
  %i[min max].each do |column|
32
30
  define_method(column) do
33
- ActiveFields::Casters::DateCaster.new.deserialize(super())
31
+ Casters::DateCaster.new.deserialize(super())
34
32
  end
35
33
 
36
34
  define_method(:"#{column}=") do |other|
37
- super(ActiveFields::Casters::DateCaster.new.serialize(other))
35
+ super(Casters::DateCaster.new.serialize(other))
38
36
  end
39
37
  end
40
38
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class DateArray < ActiveFields::Field
4
+ module Field
5
+ class DateArray < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :min_size, :max_size, :min, :max
9
7
 
10
8
  # attribute :min_size, :integer
@@ -18,21 +16,21 @@ module ActiveFields
18
16
 
19
17
  %i[min_size max_size].each do |column|
20
18
  define_method(column) do
21
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
19
+ Casters::IntegerCaster.new.deserialize(super())
22
20
  end
23
21
 
24
22
  define_method(:"#{column}=") do |other|
25
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
23
+ super(Casters::IntegerCaster.new.serialize(other))
26
24
  end
27
25
  end
28
26
 
29
27
  %i[min max].each do |column|
30
28
  define_method(column) do
31
- ActiveFields::Casters::DateCaster.new.deserialize(super())
29
+ Casters::DateCaster.new.deserialize(super())
32
30
  end
33
31
 
34
32
  define_method(:"#{column}=") do |other|
35
- super(ActiveFields::Casters::DateCaster.new.serialize(other))
33
+ super(Casters::DateCaster.new.serialize(other))
36
34
  end
37
35
  end
38
36
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class Decimal < ActiveFields::Field
4
+ module Field
5
+ class Decimal < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :required, :min, :max
9
7
 
10
8
  # attribute :required, :boolean, default: false
@@ -16,7 +14,7 @@ module ActiveFields
16
14
 
17
15
  %i[required].each do |column|
18
16
  define_method(column) do
19
- ActiveFields::Casters::BooleanCaster.new.deserialize(super())
17
+ Casters::BooleanCaster.new.deserialize(super())
20
18
  end
21
19
 
22
20
  define_method(:"#{column}?") do
@@ -24,17 +22,17 @@ module ActiveFields
24
22
  end
25
23
 
26
24
  define_method(:"#{column}=") do |other|
27
- super(ActiveFields::Casters::BooleanCaster.new.serialize(other))
25
+ super(Casters::BooleanCaster.new.serialize(other))
28
26
  end
29
27
  end
30
28
 
31
29
  %i[min max].each do |column|
32
30
  define_method(column) do
33
- ActiveFields::Casters::DecimalCaster.new.deserialize(super())
31
+ Casters::DecimalCaster.new.deserialize(super())
34
32
  end
35
33
 
36
34
  define_method(:"#{column}=") do |other|
37
- super(ActiveFields::Casters::DecimalCaster.new.serialize(other))
35
+ super(Casters::DecimalCaster.new.serialize(other))
38
36
  end
39
37
  end
40
38
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class DecimalArray < ActiveFields::Field
4
+ module Field
5
+ class DecimalArray < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :min_size, :max_size, :min, :max
9
7
 
10
8
  # attribute :min_size, :integer
@@ -18,21 +16,21 @@ module ActiveFields
18
16
 
19
17
  %i[min_size max_size].each do |column|
20
18
  define_method(column) do
21
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
19
+ Casters::IntegerCaster.new.deserialize(super())
22
20
  end
23
21
 
24
22
  define_method(:"#{column}=") do |other|
25
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
23
+ super(Casters::IntegerCaster.new.serialize(other))
26
24
  end
27
25
  end
28
26
 
29
27
  %i[min max].each do |column|
30
28
  define_method(column) do
31
- ActiveFields::Casters::DecimalCaster.new.deserialize(super())
29
+ Casters::DecimalCaster.new.deserialize(super())
32
30
  end
33
31
 
34
32
  define_method(:"#{column}=") do |other|
35
- super(ActiveFields::Casters::DecimalCaster.new.serialize(other))
33
+ super(Casters::DecimalCaster.new.serialize(other))
36
34
  end
37
35
  end
38
36
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class Enum < ActiveFields::Field
4
+ module Field
5
+ class Enum < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :required, :allowed_values
9
7
 
10
8
  # attribute :required, :boolean, default: false
@@ -15,7 +13,7 @@ module ActiveFields
15
13
 
16
14
  %i[required].each do |column|
17
15
  define_method(column) do
18
- ActiveFields::Casters::BooleanCaster.new.deserialize(super())
16
+ Casters::BooleanCaster.new.deserialize(super())
19
17
  end
20
18
 
21
19
  define_method(:"#{column}?") do
@@ -23,17 +21,17 @@ module ActiveFields
23
21
  end
24
22
 
25
23
  define_method(:"#{column}=") do |other|
26
- super(ActiveFields::Casters::BooleanCaster.new.serialize(other))
24
+ super(Casters::BooleanCaster.new.serialize(other))
27
25
  end
28
26
  end
29
27
 
30
28
  %i[allowed_values].each do |column|
31
29
  define_method(column) do
32
- ActiveFields::Casters::TextArrayCaster.new.deserialize(super())
30
+ Casters::TextArrayCaster.new.deserialize(super())
33
31
  end
34
32
 
35
33
  define_method(:"#{column}=") do |other|
36
- super(ActiveFields::Casters::TextArrayCaster.new.serialize(other))
34
+ super(Casters::TextArrayCaster.new.serialize(other))
37
35
  end
38
36
  end
39
37
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class EnumArray < ActiveFields::Field
4
+ module Field
5
+ class EnumArray < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :min_size, :max_size, :allowed_values
9
7
 
10
8
  # attribute :min_size, :integer
@@ -17,21 +15,21 @@ module ActiveFields
17
15
 
18
16
  %i[min_size max_size].each do |column|
19
17
  define_method(column) do
20
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
18
+ Casters::IntegerCaster.new.deserialize(super())
21
19
  end
22
20
 
23
21
  define_method(:"#{column}=") do |other|
24
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
22
+ super(Casters::IntegerCaster.new.serialize(other))
25
23
  end
26
24
  end
27
25
 
28
26
  %i[allowed_values].each do |column|
29
27
  define_method(column) do
30
- ActiveFields::Casters::TextArrayCaster.new.deserialize(super())
28
+ Casters::TextArrayCaster.new.deserialize(super())
31
29
  end
32
30
 
33
31
  define_method(:"#{column}=") do |other|
34
- super(ActiveFields::Casters::TextArrayCaster.new.serialize(other))
32
+ super(Casters::TextArrayCaster.new.serialize(other))
35
33
  end
36
34
  end
37
35
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class Integer < ActiveFields::Field
4
+ module Field
5
+ class Integer < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :required, :min, :max
9
7
 
10
8
  # attribute :required, :boolean, default: false
@@ -16,7 +14,7 @@ module ActiveFields
16
14
 
17
15
  %i[required].each do |column|
18
16
  define_method(column) do
19
- ActiveFields::Casters::BooleanCaster.new.deserialize(super())
17
+ Casters::BooleanCaster.new.deserialize(super())
20
18
  end
21
19
 
22
20
  define_method(:"#{column}?") do
@@ -24,17 +22,17 @@ module ActiveFields
24
22
  end
25
23
 
26
24
  define_method(:"#{column}=") do |other|
27
- super(ActiveFields::Casters::BooleanCaster.new.serialize(other))
25
+ super(Casters::BooleanCaster.new.serialize(other))
28
26
  end
29
27
  end
30
28
 
31
29
  %i[min max].each do |column|
32
30
  define_method(column) do
33
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
31
+ Casters::IntegerCaster.new.deserialize(super())
34
32
  end
35
33
 
36
34
  define_method(:"#{column}=") do |other|
37
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
35
+ super(Casters::IntegerCaster.new.serialize(other))
38
36
  end
39
37
  end
40
38
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class IntegerArray < ActiveFields::Field
4
+ module Field
5
+ class IntegerArray < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :min_size, :max_size, :min, :max
9
7
 
10
8
  # attribute :min_size, :integer
@@ -18,11 +16,11 @@ module ActiveFields
18
16
 
19
17
  %i[min_size max_size min max].each do |column|
20
18
  define_method(column) do
21
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
19
+ Casters::IntegerCaster.new.deserialize(super())
22
20
  end
23
21
 
24
22
  define_method(:"#{column}=") do |other|
25
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
23
+ super(Casters::IntegerCaster.new.serialize(other))
26
24
  end
27
25
  end
28
26
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class Text < ActiveFields::Field
4
+ module Field
5
+ class Text < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :required, :min_length, :max_length
9
7
 
10
8
  # attribute :required, :boolean, default: false
@@ -17,7 +15,7 @@ module ActiveFields
17
15
 
18
16
  %i[required].each do |column|
19
17
  define_method(column) do
20
- ActiveFields::Casters::BooleanCaster.new.deserialize(super())
18
+ Casters::BooleanCaster.new.deserialize(super())
21
19
  end
22
20
 
23
21
  define_method(:"#{column}?") do
@@ -25,17 +23,17 @@ module ActiveFields
25
23
  end
26
24
 
27
25
  define_method(:"#{column}=") do |other|
28
- super(ActiveFields::Casters::BooleanCaster.new.serialize(other))
26
+ super(Casters::BooleanCaster.new.serialize(other))
29
27
  end
30
28
  end
31
29
 
32
30
  %i[min_length max_length].each do |column|
33
31
  define_method(column) do
34
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
32
+ Casters::IntegerCaster.new.deserialize(super())
35
33
  end
36
34
 
37
35
  define_method(:"#{column}=") do |other|
38
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
36
+ super(Casters::IntegerCaster.new.serialize(other))
39
37
  end
40
38
  end
41
39
 
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../field"
4
-
5
3
  module ActiveFields
6
- class Field
7
- class TextArray < ActiveFields::Field
4
+ module Field
5
+ class TextArray < ActiveFields.config.field_base_class
8
6
  store_accessor :options, :min_size, :max_size, :min_length, :max_length
9
7
 
10
8
  # attribute :min_size, :integer
@@ -19,11 +17,11 @@ module ActiveFields
19
17
 
20
18
  %i[min_size max_size min_length max_length].each do |column|
21
19
  define_method(column) do
22
- ActiveFields::Casters::IntegerCaster.new.deserialize(super())
20
+ Casters::IntegerCaster.new.deserialize(super())
23
21
  end
24
22
 
25
23
  define_method(:"#{column}=") do |other|
26
- super(ActiveFields::Casters::IntegerCaster.new.serialize(other))
24
+ super(Casters::IntegerCaster.new.serialize(other))
27
25
  end
28
26
  end
29
27
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveFields
4
+ # If value base class has been changed, we should prevent this model from being loaded.
5
+ # Since we cannot remove it entirely, we will not add any functionality to it.
6
+ if ActiveFields.config.value_class_changed?
7
+ class Value; end
8
+ else
9
+ class Value < ApplicationRecord
10
+ self.table_name = "active_fields_values"
11
+
12
+ include ValueConcern
13
+ end
14
+ end
15
+ end
@@ -1,20 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Mix-in that adds the active fields functionality to the ActiveRecord model
4
3
  module ActiveFields
5
- module Customizable
4
+ # Mix-in that adds the active fields functionality to the ActiveRecord model
5
+ module CustomizableConcern
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
9
  # active_values association for the owner record.
10
10
  # We skip built-in autosave because it doesn't work if the owner record isn't changed.
11
11
  # Instead, we implement our own autosave callback: `save_changed_active_values`.
12
+ # rubocop:disable Rails/ReflectionClassName
12
13
  has_many :active_values,
13
- class_name: "ActiveFields::Value",
14
+ class_name: ActiveFields.config.value_class_name,
14
15
  as: :customizable,
15
16
  inverse_of: :customizable,
16
17
  autosave: false,
17
18
  dependent: :destroy
19
+ # rubocop:enable Rails/ReflectionClassName
18
20
 
19
21
  # Firstly, we build active_values that hasn't been already created.
20
22
  # Than, we set values for active_values whose values should be changed
@@ -35,7 +37,7 @@ module ActiveFields
35
37
  end
36
38
 
37
39
  def active_fields
38
- ActiveFields::Field.for(model_name.name)
40
+ ActiveFields.config.field_base_class.for(model_name.name)
39
41
  end
40
42
 
41
43
  # Convert hash keys to strings for easier access by fields names.
@@ -1,16 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Mix-in with a base logic for the active fields model
4
3
  module ActiveFields
4
+ # Mix-in with a base logic for the active fields model
5
5
  module FieldConcern
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
+ # rubocop:disable Rails/ReflectionClassName
9
10
  has_many :active_values,
10
- class_name: "ActiveFields::Value",
11
+ class_name: ActiveFields.config.value_class_name,
11
12
  foreign_key: :active_field_id,
12
13
  inverse_of: :active_field,
13
14
  dependent: :destroy
15
+ # rubocop:enable Rails/ReflectionClassName
14
16
 
15
17
  scope :for, ->(customizable_type) { where(customizable_type: customizable_type) }
16
18
 
@@ -18,6 +20,7 @@ module ActiveFields
18
20
  validates :name, presence: true, uniqueness: { scope: :customizable_type }
19
21
  validates :name, format: { with: /\A[a-z0-9_]+\z/ }, allow_blank: true
20
22
  validate :validate_default_value
23
+ validate :validate_customizable_model_allows_type
21
24
 
22
25
  after_create :add_field_to_records
23
26
  after_initialize :set_defaults
@@ -68,9 +71,17 @@ module ActiveFields
68
71
  end
69
72
  end
70
73
 
74
+ def validate_customizable_model_allows_type
75
+ allowed_types = customizable_model.active_fields_config&.types || []
76
+ return true if ActiveFields.config.fields.values_at(*allowed_types).include?(type)
77
+
78
+ errors.add(:customizable_type, :inclusion)
79
+ false
80
+ end
81
+
71
82
  def add_field_to_records
72
83
  customizable_model.find_each do |record|
73
- ActiveFields::Value.create!(active_field: self, customizable: record, value: default_value)
84
+ ActiveFields.config.value_class.create!(active_field: self, customizable: record, value: default_value)
74
85
  end
75
86
  end
76
87
 
@@ -1,13 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Mix-in with a base logic for the active fields value model
4
3
  module ActiveFields
4
+ # Mix-in with a base logic for the active fields value model
5
5
  module ValueConcern
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
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
10
+ # rubocop:disable Rails/ReflectionClassName
11
+ belongs_to :active_field,
12
+ class_name: ActiveFields.config.field_base_class_name,
13
+ optional: false,
14
+ inverse_of: :active_values
15
+ # rubocop:enable Rails/ReflectionClassName
11
16
 
12
17
  validates :active_field_id, uniqueness: { scope: %i[customizable_id customizable_type] }
13
18
  validate :validate_value
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ ActiveFields::Engine.routes.draw do
4
+ end