all_futures 1.0.3 → 2.0.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.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -1
  3. data/Gemfile.lock +272 -61
  4. data/LICENSE.txt +1 -1
  5. data/README.md +59 -79
  6. data/Rakefile +13 -1
  7. data/SUMMARY.md +22 -2
  8. data/bin/console +8 -0
  9. data/bin/loc +0 -0
  10. data/bin/setup +0 -0
  11. data/bin/standardize +0 -0
  12. data/lib/all_futures/association.rb +42 -0
  13. data/lib/all_futures/association_index.rb +69 -0
  14. data/lib/all_futures/attributes.rb +28 -0
  15. data/lib/all_futures/base.rb +66 -0
  16. data/lib/all_futures/callbacks.rb +71 -0
  17. data/lib/all_futures/configuration.rb +9 -0
  18. data/lib/all_futures/dirty.rb +116 -0
  19. data/lib/all_futures/embeds.rb +40 -0
  20. data/lib/all_futures/errors.rb +37 -0
  21. data/lib/all_futures/finder.rb +214 -0
  22. data/lib/all_futures/locale/en.yml +49 -0
  23. data/lib/all_futures/persistence.rb +595 -0
  24. data/lib/all_futures/presenter.rb +35 -0
  25. data/lib/all_futures/railtie.rb +26 -0
  26. data/lib/all_futures/translation.rb +22 -0
  27. data/lib/all_futures/validations.rb +10 -0
  28. data/lib/all_futures/version.rb +3 -1
  29. data/lib/all_futures/versions.rb +80 -0
  30. data/lib/all_futures.rb +24 -1
  31. data/lib/vendor/ACTIVE_ENTITY_MIT-LICENSE +42 -0
  32. data/lib/vendor/README.md +8 -0
  33. data/lib/vendor/active_entity/aggregations.rb +277 -0
  34. data/lib/vendor/active_entity/associations/embeds/association.rb +134 -0
  35. data/lib/vendor/active_entity/associations/embeds/builder/association.rb +105 -0
  36. data/lib/vendor/active_entity/associations/embeds/builder/collection_association.rb +71 -0
  37. data/lib/vendor/active_entity/associations/embeds/builder/embedded_in.rb +38 -0
  38. data/lib/vendor/active_entity/associations/embeds/builder/embeds_many.rb +13 -0
  39. data/lib/vendor/active_entity/associations/embeds/builder/embeds_one.rb +16 -0
  40. data/lib/vendor/active_entity/associations/embeds/builder/singular_association.rb +28 -0
  41. data/lib/vendor/active_entity/associations/embeds/collection_association.rb +209 -0
  42. data/lib/vendor/active_entity/associations/embeds/collection_proxy.rb +310 -0
  43. data/lib/vendor/active_entity/associations/embeds/embedded_in_association.rb +31 -0
  44. data/lib/vendor/active_entity/associations/embeds/embeds_many_association.rb +15 -0
  45. data/lib/vendor/active_entity/associations/embeds/embeds_one_association.rb +26 -0
  46. data/lib/vendor/active_entity/associations/embeds/singular_association.rb +35 -0
  47. data/lib/vendor/active_entity/associations.rb +168 -0
  48. data/lib/vendor/active_entity/attribute_assignment.rb +87 -0
  49. data/lib/vendor/active_entity/attribute_decorators.rb +90 -0
  50. data/lib/vendor/active_entity/attribute_methods/before_type_cast.rb +78 -0
  51. data/lib/vendor/active_entity/attribute_methods/dirty.rb +180 -0
  52. data/lib/vendor/active_entity/attribute_methods/primary_key.rb +96 -0
  53. data/lib/vendor/active_entity/attribute_methods/query.rb +38 -0
  54. data/lib/vendor/active_entity/attribute_methods/read.rb +44 -0
  55. data/lib/vendor/active_entity/attribute_methods/serialization.rb +90 -0
  56. data/lib/vendor/active_entity/attribute_methods/time_zone_conversion.rb +93 -0
  57. data/lib/vendor/active_entity/attribute_methods/write.rb +54 -0
  58. data/lib/vendor/active_entity/attribute_methods.rb +833 -0
  59. data/lib/vendor/active_entity/attributes.rb +240 -0
  60. data/lib/vendor/active_entity/base.rb +293 -0
  61. data/lib/vendor/active_entity/callbacks.rb +21 -0
  62. data/lib/vendor/active_entity/coders/json.rb +15 -0
  63. data/lib/vendor/active_entity/coders/yaml_column.rb +50 -0
  64. data/lib/vendor/active_entity/core.rb +348 -0
  65. data/lib/vendor/active_entity/enum.rb +260 -0
  66. data/lib/vendor/active_entity/errors.rb +69 -0
  67. data/lib/vendor/active_entity/gem_version.rb +17 -0
  68. data/lib/vendor/active_entity/inheritance.rb +176 -0
  69. data/lib/vendor/active_entity/integration.rb +78 -0
  70. data/lib/vendor/active_entity/locale/en.yml +45 -0
  71. data/lib/vendor/active_entity/model_schema.rb +118 -0
  72. data/lib/vendor/active_entity/nested_attributes.rb +554 -0
  73. data/lib/vendor/active_entity/persistence.rb +20 -0
  74. data/lib/vendor/active_entity/railtie.rb +76 -0
  75. data/lib/vendor/active_entity/readonly_attributes.rb +55 -0
  76. data/lib/vendor/active_entity/reflection.rb +447 -0
  77. data/lib/vendor/active_entity/serialization.rb +28 -0
  78. data/lib/vendor/active_entity/store.rb +291 -0
  79. data/lib/vendor/active_entity/translation.rb +24 -0
  80. data/lib/vendor/active_entity/type/date.rb +9 -0
  81. data/lib/vendor/active_entity/type/date_time.rb +9 -0
  82. data/lib/vendor/active_entity/type/internal/timezone.rb +17 -0
  83. data/lib/vendor/active_entity/type/json.rb +30 -0
  84. data/lib/vendor/active_entity/type/modifiers/array.rb +72 -0
  85. data/lib/vendor/active_entity/type/modifiers/array_without_blank.rb +19 -0
  86. data/lib/vendor/active_entity/type/registry.rb +112 -0
  87. data/lib/vendor/active_entity/type/serialized.rb +71 -0
  88. data/lib/vendor/active_entity/type/text.rb +11 -0
  89. data/lib/vendor/active_entity/type/time.rb +21 -0
  90. data/lib/vendor/active_entity/type/unsigned_integer.rb +17 -0
  91. data/lib/vendor/active_entity/type.rb +75 -0
  92. data/lib/vendor/active_entity/validate_embeds_association.rb +300 -0
  93. data/lib/vendor/active_entity/validations/absence.rb +25 -0
  94. data/lib/vendor/active_entity/validations/associated.rb +60 -0
  95. data/lib/vendor/active_entity/validations/length.rb +26 -0
  96. data/lib/vendor/active_entity/validations/presence.rb +68 -0
  97. data/lib/vendor/active_entity/validations/subset.rb +76 -0
  98. data/lib/vendor/active_entity/validations/uniqueness_in_embeds.rb +91 -0
  99. data/lib/vendor/active_entity/validations/uniqueness_on_active_record.rb +234 -0
  100. data/lib/vendor/active_entity/validations.rb +47 -0
  101. data/lib/vendor/active_entity/version.rb +10 -0
  102. data/lib/vendor/active_entity.rb +99 -0
  103. data/lib/vendor/core_ext/array_without_blank.rb +48 -0
  104. metadata +175 -21
  105. data/lib/all_futures/all_futures.rb +0 -28
  106. data/lib/all_futures/config.rb +0 -14
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveEntity
4
+ module Type
5
+ class UnsignedInteger < ActiveModel::Type::Integer # :nodoc:
6
+ private
7
+
8
+ def max_value
9
+ super * 2
10
+ end
11
+
12
+ def min_value
13
+ 0
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_model/type"
4
+
5
+ require "active_entity/type/internal/timezone"
6
+
7
+ require "active_entity/type/date"
8
+ require "active_entity/type/date_time"
9
+ require "active_entity/type/json"
10
+ require "active_entity/type/time"
11
+ require "active_entity/type/text"
12
+ require "active_entity/type/unsigned_integer"
13
+
14
+ require "active_entity/type/modifiers/array"
15
+ require "active_entity/type/modifiers/array_without_blank"
16
+
17
+ require "active_entity/type/serialized"
18
+ require "active_entity/type/registry"
19
+
20
+ module ActiveEntity
21
+ module Type
22
+ @registry = Registry.new
23
+
24
+ class << self
25
+ attr_accessor :registry # :nodoc:
26
+ delegate :add_modifier, to: :registry
27
+
28
+ # Add a new type to the registry, allowing it to be referenced as a
29
+ # symbol by {ActiveEntity::Base.attribute}[rdoc-ref:Attributes::ClassMethods#attribute].
30
+ # If your type is only meant to be used with a specific database adapter, you can
31
+ # do so by passing <tt>adapter: :postgresql</tt>. If your type has the same
32
+ # name as a native type for the current adapter, an exception will be
33
+ # raised unless you specify an +:override+ option. <tt>override: true</tt> will
34
+ # cause your type to be used instead of the native type. <tt>override:
35
+ # false</tt> will cause the native type to be used over yours if one exists.
36
+ def register(type_name, klass = nil, **options, &block)
37
+ registry.register(type_name, klass, **options, &block)
38
+ end
39
+
40
+ def lookup(*args, **kwargs) # :nodoc:
41
+ registry.lookup(*args, **kwargs)
42
+ end
43
+
44
+ def default_value # :nodoc:
45
+ @default_value ||= Value.new
46
+ end
47
+ end
48
+
49
+ BigInteger = ActiveModel::Type::BigInteger
50
+ Binary = ActiveModel::Type::Binary
51
+ Boolean = ActiveModel::Type::Boolean
52
+ Decimal = ActiveModel::Type::Decimal
53
+ Float = ActiveModel::Type::Float
54
+ Integer = ActiveModel::Type::Integer
55
+ String = ActiveModel::Type::String
56
+ Value = ActiveModel::Type::Value
57
+
58
+ add_modifier({ array: true }, Modifiers::Array)
59
+ add_modifier({ array_without_blank: true }, Modifiers::ArrayWithoutBlank)
60
+
61
+ register(:big_integer, Type::BigInteger, override: false)
62
+ register(:binary, Type::Binary, override: false)
63
+ register(:boolean, Type::Boolean, override: false)
64
+ register(:date, Type::Date, override: false)
65
+ register(:datetime, Type::DateTime, override: false)
66
+ register(:decimal, Type::Decimal, override: false)
67
+ register(:float, Type::Float, override: false)
68
+ register(:integer, Type::Integer, override: false)
69
+ register(:unsigned_integer, Type::UnsignedInteger, override: false)
70
+ register(:json, Type::Json, override: false)
71
+ register(:string, Type::String, override: false)
72
+ register(:text, Type::Text, override: false)
73
+ register(:time, Type::Time, override: false)
74
+ end
75
+ end
@@ -0,0 +1,300 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveEntity
4
+ # = Active Entity Autosave Association
5
+ #
6
+ # AutosaveAssociation is a module that takes care of automatically saving
7
+ # associated records when their parent is saved. In addition to saving, it
8
+ # also destroys any associated records that were marked for destruction.
9
+ # (See #mark_for_destruction and #marked_for_destruction?).
10
+ #
11
+ # Saving of the parent, its associations, and the destruction of marked
12
+ # associations, all happen inside a transaction. This should never leave the
13
+ # database in an inconsistent state.
14
+ #
15
+ # If validations for any of the associations fail, their error messages will
16
+ # be applied to the parent.
17
+ #
18
+ # Note that it also means that associations marked for destruction won't
19
+ # be destroyed directly. They will however still be marked for destruction.
20
+ #
21
+ # Note that <tt>autosave: false</tt> is not same as not declaring <tt>:autosave</tt>.
22
+ # When the <tt>:autosave</tt> option is not present then new association records are
23
+ # saved but the updated association records are not saved.
24
+ #
25
+ # == Validation
26
+ #
27
+ # Child records are validated unless <tt>:validate</tt> is +false+.
28
+ #
29
+ # == Callbacks
30
+ #
31
+ # Association with autosave option defines several callbacks on your
32
+ # model (before_save, after_create, after_update). Please note that
33
+ # callbacks are executed in the order they were defined in
34
+ # model. You should avoid modifying the association content, before
35
+ # autosave callbacks are executed. Placing your callbacks after
36
+ # associations is usually a good practice.
37
+ #
38
+ # === One-to-one Example
39
+ #
40
+ # class Post < ActiveEntity::Base
41
+ # has_one :author, autosave: true
42
+ # end
43
+ #
44
+ # Saving changes to the parent and its associated model can now be performed
45
+ # automatically _and_ atomically:
46
+ #
47
+ # post = Post.find(1)
48
+ # post.title # => "The current global position of migrating ducks"
49
+ # post.author.name # => "alloy"
50
+ #
51
+ # post.title = "On the migration of ducks"
52
+ # post.author.name = "Eloy Duran"
53
+ #
54
+ # post.save
55
+ # post.reload
56
+ # post.title # => "On the migration of ducks"
57
+ # post.author.name # => "Eloy Duran"
58
+ #
59
+ # Destroying an associated model, as part of the parent's save action, is as
60
+ # simple as marking it for destruction:
61
+ #
62
+ # post.author.mark_for_destruction
63
+ # post.author.marked_for_destruction? # => true
64
+ #
65
+ # Note that the model is _not_ yet removed from the database:
66
+ #
67
+ # id = post.author.id
68
+ # Author.find_by(id: id).nil? # => false
69
+ #
70
+ # post.save
71
+ # post.reload.author # => nil
72
+ #
73
+ # Now it _is_ removed from the database:
74
+ #
75
+ # Author.find_by(id: id).nil? # => true
76
+ #
77
+ # === One-to-many Example
78
+ #
79
+ # When <tt>:autosave</tt> is not declared new children are saved when their parent is saved:
80
+ #
81
+ # class Post < ActiveEntity::Base
82
+ # has_many :comments # :autosave option is not declared
83
+ # end
84
+ #
85
+ # post = Post.new(title: 'ruby rocks')
86
+ # post.comments.build(body: 'hello world')
87
+ # post.save # => saves both post and comment
88
+ #
89
+ # post = Post.create(title: 'ruby rocks')
90
+ # post.comments.build(body: 'hello world')
91
+ # post.save # => saves both post and comment
92
+ #
93
+ # post = Post.create(title: 'ruby rocks')
94
+ # post.comments.create(body: 'hello world')
95
+ # post.save # => saves both post and comment
96
+ #
97
+ # When <tt>:autosave</tt> is true all children are saved, no matter whether they
98
+ # are new records or not:
99
+ #
100
+ # class Post < ActiveEntity::Base
101
+ # has_many :comments, autosave: true
102
+ # end
103
+ #
104
+ # post = Post.create(title: 'ruby rocks')
105
+ # post.comments.create(body: 'hello world')
106
+ # post.comments[0].body = 'hi everyone'
107
+ # post.comments.build(body: "good morning.")
108
+ # post.title += "!"
109
+ # post.save # => saves both post and comments.
110
+ #
111
+ # Destroying one of the associated models as part of the parent's save action
112
+ # is as simple as marking it for destruction:
113
+ #
114
+ # post.comments # => [#<Comment id: 1, ...>, #<Comment id: 2, ...]>
115
+ # post.comments[1].mark_for_destruction
116
+ # post.comments[1].marked_for_destruction? # => true
117
+ # post.comments.length # => 2
118
+ #
119
+ # Note that the model is _not_ yet removed from the database:
120
+ #
121
+ # id = post.comments.last.id
122
+ # Comment.find_by(id: id).nil? # => false
123
+ #
124
+ # post.save
125
+ # post.reload.comments.length # => 1
126
+ #
127
+ # Now it _is_ removed from the database:
128
+ #
129
+ # Comment.find_by(id: id).nil? # => true
130
+ module ValidateEmbedsAssociation
131
+ extend ActiveSupport::Concern
132
+
133
+ module AssociationBuilderExtension #:nodoc:
134
+ def self.build(model, reflection)
135
+ model.send(:add_embeds_associations_validation_callbacks, reflection)
136
+ end
137
+
138
+ def self.valid_options
139
+ []
140
+ end
141
+ end
142
+
143
+ included do
144
+ Associations::Embeds::Builder::Association.extensions << AssociationBuilderExtension
145
+
146
+ unless respond_to?(:index_nested_attribute_errors)
147
+ mattr_accessor :index_nested_attribute_errors, instance_writer: false, default: false
148
+ end
149
+ end
150
+
151
+ module ClassMethods # :nodoc:
152
+ private
153
+
154
+ def define_non_cyclic_method(name, &block)
155
+ return if instance_methods(false).include?(name)
156
+ define_method(name) do |*args|
157
+ result = true; @_already_called ||= {}
158
+ # Loop prevention for validation of associations
159
+ unless @_already_called[name]
160
+ begin
161
+ @_already_called[name] = true
162
+ result = instance_eval(&block)
163
+ ensure
164
+ @_already_called[name] = false
165
+ end
166
+ end
167
+
168
+ result
169
+ end
170
+ end
171
+
172
+ # Adds validation and save callbacks for the association as specified by
173
+ # the +reflection+.
174
+ #
175
+ # For performance reasons, we don't check whether to validate at runtime.
176
+ # However the validation and callback methods are lazy and those methods
177
+ # get created when they are invoked for the very first time. However,
178
+ # this can change, for instance, when using nested attributes, which is
179
+ # called _after_ the association has been defined. Since we don't want
180
+ # the callbacks to get defined multiple times, there are guards that
181
+ # check if the save or validation methods have already been defined
182
+ # before actually defining them.
183
+ def add_embeds_associations_validation_callbacks(reflection)
184
+ define_embeds_associations_validation_callbacks(reflection)
185
+ end
186
+
187
+ def define_embeds_associations_validation_callbacks(reflection)
188
+ validation_method = :"validate_associated_records_for_#{reflection.name}"
189
+ if reflection.validate? && !method_defined?(validation_method)
190
+ if reflection.collection?
191
+ method = :validate_collection_association
192
+ else
193
+ method = :validate_single_association
194
+ end
195
+
196
+ define_non_cyclic_method(validation_method) { send(method, reflection) }
197
+ validate validation_method
198
+ after_validation :_ensure_no_duplicate_errors
199
+ end
200
+ end
201
+ end
202
+
203
+ # Marks this record to be destroyed as part of the parent's save transaction.
204
+ # This does _not_ actually destroy the record instantly, rather child record will be destroyed
205
+ # when <tt>parent.save</tt> is called.
206
+ #
207
+ # Only useful if the <tt>:autosave</tt> option on the parent is enabled for this associated model.
208
+ def mark_for_destruction
209
+ @marked_for_destruction = true
210
+ end
211
+
212
+ # Returns whether or not this record will be destroyed as part of the parent's save transaction.
213
+ #
214
+ # Only useful if the <tt>:autosave</tt> option on the parent is enabled for this associated model.
215
+ def marked_for_destruction?
216
+ @marked_for_destruction
217
+ end
218
+
219
+ # Records the association that is being destroyed and destroying this
220
+ # record in the process.
221
+ def destroyed_by_association=(reflection)
222
+ @destroyed_by_association = reflection
223
+ end
224
+
225
+ # Returns the association for the parent being destroyed.
226
+ #
227
+ # Used to avoid updating the counter cache unnecessarily.
228
+ def destroyed_by_association
229
+ @destroyed_by_association
230
+ end
231
+
232
+ private
233
+
234
+ # Returns the record for an association collection that should be validated
235
+ # or saved. If +autosave+ is +false+ only new records will be returned,
236
+ # unless the parent is/was a new record itself.
237
+ def associated_records_to_validate(association)
238
+ association&.target
239
+ end
240
+
241
+ # Validate the association if <tt>:validate</tt> or <tt>:autosave</tt> is
242
+ # turned on for the association.
243
+ def validate_single_association(reflection)
244
+ association = association_instance_get(reflection.name)
245
+ record = association&.reader
246
+ association_valid?(reflection, record) if record
247
+ end
248
+
249
+ # Validate the associated records if <tt>:validate</tt> or
250
+ # <tt>:autosave</tt> is turned on for the association specified by
251
+ # +reflection+.
252
+ def validate_collection_association(reflection)
253
+ if association = association_instance_get(reflection.name)
254
+ if records = associated_records_to_validate(association)
255
+ records.each_with_index { |record, index| association_valid?(reflection, record, index) }
256
+ end
257
+ end
258
+ end
259
+
260
+ # Returns whether or not the association is valid and applies any errors to
261
+ # the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
262
+ # enabled records if they're marked_for_destruction? or destroyed.
263
+ def association_valid?(reflection, record, index = nil)
264
+ return true if record.marked_for_destruction?
265
+
266
+ context = validation_context
267
+
268
+ unless valid = record.valid?(context)
269
+ indexed_attribute = !index.nil? && (reflection.options[:index_errors] || ActiveEntity::Base.index_nested_attribute_errors)
270
+
271
+ record.errors.group_by_attribute.each { |attribute, errors|
272
+ attribute = normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)
273
+
274
+ errors.each { |error|
275
+ self.errors.import(
276
+ error,
277
+ attribute: attribute
278
+ )
279
+ }
280
+ }
281
+ end
282
+
283
+ valid
284
+ end
285
+
286
+ def normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)
287
+ if indexed_attribute
288
+ "#{reflection.name}[#{index}].#{attribute}"
289
+ else
290
+ "#{reflection.name}.#{attribute}"
291
+ end
292
+ end
293
+
294
+ def _ensure_no_duplicate_errors
295
+ errors.messages.each_key do |attribute|
296
+ errors[attribute].uniq!
297
+ end
298
+ end
299
+ end
300
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveEntity
4
+ module Validations
5
+ class AbsenceValidator < ActiveModel::Validations::AbsenceValidator # :nodoc:
6
+ def validate_each(record, attribute, association_or_value)
7
+ if record.class._reflect_on_association(attribute)
8
+ association_or_value = Array.wrap(association_or_value).reject(&:marked_for_destruction?)
9
+ end
10
+ super
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ # Validates that the specified attributes are not present (as defined by
16
+ # Object#present?). If the attribute is an association, the associated object
17
+ # is considered absent if it was marked for destruction.
18
+ #
19
+ # See ActiveModel::Validations::HelperMethods.validates_absence_of for more information.
20
+ def validates_absence_of(*attr_names)
21
+ validates_with AbsenceValidator, _merge_attributes(attr_names)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveEntity
4
+ module Validations
5
+ class AssociatedValidator < ActiveModel::EachValidator #:nodoc:
6
+ def validate_each(record, attribute, value)
7
+ if Array(value).reject { |r| valid_object?(r) }.any?
8
+ record.errors.add(attribute, :invalid, **options.merge(value: value))
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def valid_object?(record)
15
+ (record.respond_to?(:marked_for_destruction?) && record.marked_for_destruction?) || record.valid?
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+ # Validates whether the associated object or objects are all valid.
21
+ # Works with any kind of association.
22
+ #
23
+ # class Book < ActiveEntity::Base
24
+ # has_many :pages
25
+ # belongs_to :library
26
+ #
27
+ # validates_associated :pages, :library
28
+ # end
29
+ #
30
+ # WARNING: This validation must not be used on both ends of an association.
31
+ # Doing so will lead to a circular dependency and cause infinite recursion.
32
+ #
33
+ # NOTE: This validation will not fail if the association hasn't been
34
+ # assigned. If you want to ensure that the association is both present and
35
+ # guaranteed to be valid, you also need to use
36
+ # {validates_presence_of}[rdoc-ref:Validations::ClassMethods#validates_presence_of].
37
+ #
38
+ # Configuration options:
39
+ #
40
+ # * <tt>:message</tt> - A custom error message (default is: "is invalid").
41
+ # * <tt>:on</tt> - Specifies the contexts where this validation is active.
42
+ # Runs in all validation contexts by default +nil+. You can pass a symbol
43
+ # or an array of symbols. (e.g. <tt>on: :create</tt> or
44
+ # <tt>on: :custom_validation_context</tt> or
45
+ # <tt>on: [:create, :custom_validation_context]</tt>)
46
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine
47
+ # if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
48
+ # or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
49
+ # proc or string should return or evaluate to a +true+ or +false+ value.
50
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to
51
+ # determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
52
+ # or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
53
+ # method, proc or string should return or evaluate to a +true+ or +false+
54
+ # value.
55
+ def validates_associated(*attr_names)
56
+ validates_with AssociatedValidator, _merge_attributes(attr_names)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveEntity
4
+ module Validations
5
+ class LengthValidator < ActiveModel::Validations::LengthValidator # :nodoc:
6
+ def validate_each(record, attribute, association_or_value)
7
+ if association_or_value.respond_to?(:loaded?) && association_or_value.loaded?
8
+ association_or_value = association_or_value.target.reject(&:marked_for_destruction?)
9
+ end
10
+ super
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ # Validates that the specified attributes match the length restrictions supplied.
16
+ # If the attribute is an association, records that are marked for destruction are not counted.
17
+ #
18
+ # See ActiveModel::Validations::HelperMethods.validates_length_of for more information.
19
+ def validates_length_of(*attr_names)
20
+ validates_with LengthValidator, _merge_attributes(attr_names)
21
+ end
22
+
23
+ alias_method :validates_size_of, :validates_length_of
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveEntity
4
+ module Validations
5
+ class PresenceValidator < ActiveModel::Validations::PresenceValidator # :nodoc:
6
+ def validate_each(record, attribute, association_or_value)
7
+ if record.class._reflect_on_association(attribute)
8
+ association_or_value = Array.wrap(association_or_value).reject(&:marked_for_destruction?)
9
+ end
10
+ super
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ # Validates that the specified attributes are not blank (as defined by
16
+ # Object#blank?), and, if the attribute is an association, that the
17
+ # associated object is not marked for destruction. Happens by default
18
+ # on save.
19
+ #
20
+ # class Person < ActiveEntity::Base
21
+ # has_one :face
22
+ # validates_presence_of :face
23
+ # end
24
+ #
25
+ # The face attribute must be in the object and it cannot be blank or marked
26
+ # for destruction.
27
+ #
28
+ # If you want to validate the presence of a boolean field (where the real values
29
+ # are true and false), you will want to use
30
+ # <tt>validates_inclusion_of :field_name, in: [true, false]</tt>.
31
+ #
32
+ # This is due to the way Object#blank? handles boolean values:
33
+ # <tt>false.blank? # => true</tt>.
34
+ #
35
+ # This validator defers to the Active Model validation for presence, adding the
36
+ # check to see that an associated object is not marked for destruction. This
37
+ # prevents the parent object from validating successfully and saving, which then
38
+ # deletes the associated object, thus putting the parent object into an invalid
39
+ # state.
40
+ #
41
+ # NOTE: This validation will not fail while using it with an association
42
+ # if the latter was assigned but not valid. If you want to ensure that
43
+ # it is both present and valid, you also need to use
44
+ # {validates_associated}[rdoc-ref:Validations::ClassMethods#validates_associated].
45
+ #
46
+ # Configuration options:
47
+ # * <tt>:message</tt> - A custom error message (default is: "can't be blank").
48
+ # * <tt>:on</tt> - Specifies the contexts where this validation is active.
49
+ # Runs in all validation contexts by default +nil+. You can pass a symbol
50
+ # or an array of symbols. (e.g. <tt>on: :create</tt> or
51
+ # <tt>on: :custom_validation_context</tt> or
52
+ # <tt>on: [:create, :custom_validation_context]</tt>)
53
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
54
+ # the validation should occur (e.g. <tt>if: :allow_validation</tt>, or
55
+ # <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
56
+ # or string should return or evaluate to a +true+ or +false+ value.
57
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
58
+ # if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
59
+ # or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
60
+ # proc or string should return or evaluate to a +true+ or +false+ value.
61
+ # * <tt>:strict</tt> - Specifies whether validation should be strict.
62
+ # See ActiveModel::Validations#validates! for more information.
63
+ def validates_presence_of(*attr_names)
64
+ validates_with PresenceValidator, _merge_attributes(attr_names)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveEntity
4
+ module Validations
5
+ class SubsetValidator < ActiveModel::EachValidator # :nodoc:
6
+ ERROR_MESSAGE = "An object with the method #include? or a proc, lambda or symbol is required, " \
7
+ "and must be supplied as the :in (or :within) option of the configuration hash"
8
+
9
+ def check_validity!
10
+ unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym)
11
+ raise ArgumentError, ERROR_MESSAGE
12
+ end
13
+ end
14
+
15
+ def validate_each(record, attribute, value)
16
+ if value && !value.respond_to?(:to_a)
17
+ raise ArgumentError, "#{record} can't respond `to_a`."
18
+ end
19
+
20
+ unless subset?(record, value)
21
+ record.errors.add(attribute, :non_subset, **options.except(:in, :within).merge(value: value))
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def delimiter
28
+ @delimiter ||= options[:in] || options[:within]
29
+ end
30
+
31
+ def subset?(record, value)
32
+ enumerable = value.to_a
33
+ members =
34
+ if delimiter.respond_to?(:call)
35
+ delimiter.call(record)
36
+ elsif delimiter.respond_to?(:to_sym)
37
+ record.send(delimiter)
38
+ else
39
+ delimiter
40
+ end
41
+
42
+ (members & enumerable).size == enumerable.size
43
+ end
44
+ end
45
+
46
+ module ClassMethods
47
+ # Validates whether the value of the specified attribute is available in a
48
+ # particular enumerable object.
49
+ #
50
+ # class Person < ActiveRecord::Base
51
+ # validates :tags, subset: %w( m f )
52
+ # validates_subset_of :tags, in: %w( m f )
53
+ # validates_inclusion_of :tags, in: %w( m f ), message: "tag %{value} is not included in the list"
54
+ # validates_inclusion_of :tags, in: ->(record) { record.available_tags }
55
+ # validates_inclusion_of :tags, in: :available_tags
56
+ # end
57
+ #
58
+ # Configuration options:
59
+ # * <tt>:in</tt> - An enumerable object of available items. This can be
60
+ # supplied as a proc, lambda or symbol which returns an enumerable. If the
61
+ # enumerable is a numerical, time or datetime range the test is performed
62
+ # with <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>. When using
63
+ # a proc or lambda the instance under validation is passed as an argument.
64
+ # * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
65
+ # * <tt>:message</tt> - Specifies a custom error message (default is: "is
66
+ # not included in the list").
67
+ #
68
+ # There is also a list of default options supported by every validator:
69
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
70
+ # See <tt>ActiveModel::Validations#validates</tt> for more information
71
+ def validates_subset_of(*attr_names)
72
+ validates_with SubsetValidator, _merge_attributes(attr_names)
73
+ end
74
+ end
75
+ end
76
+ end