active_storage_validations 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +133 -69
  3. data/config/locales/da.yml +33 -0
  4. data/config/locales/de.yml +5 -0
  5. data/config/locales/en.yml +5 -0
  6. data/config/locales/es.yml +5 -0
  7. data/config/locales/fr.yml +5 -0
  8. data/config/locales/it.yml +5 -0
  9. data/config/locales/ja.yml +5 -0
  10. data/config/locales/nl.yml +5 -0
  11. data/config/locales/pl.yml +5 -0
  12. data/config/locales/pt-BR.yml +5 -0
  13. data/config/locales/ru.yml +5 -0
  14. data/config/locales/sv.yml +10 -1
  15. data/config/locales/tr.yml +5 -0
  16. data/config/locales/uk.yml +5 -0
  17. data/config/locales/vi.yml +5 -0
  18. data/config/locales/zh-CN.yml +5 -0
  19. data/lib/active_storage_validations/aspect_ratio_validator.rb +47 -22
  20. data/lib/active_storage_validations/attached_validator.rb +12 -3
  21. data/lib/active_storage_validations/base_size_validator.rb +66 -0
  22. data/lib/active_storage_validations/concerns/errorable.rb +38 -0
  23. data/lib/active_storage_validations/concerns/symbolizable.rb +8 -6
  24. data/lib/active_storage_validations/content_type_validator.rb +41 -6
  25. data/lib/active_storage_validations/dimension_validator.rb +15 -15
  26. data/lib/active_storage_validations/limit_validator.rb +44 -7
  27. data/lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb +119 -0
  28. data/lib/active_storage_validations/matchers/attached_validator_matcher.rb +25 -36
  29. data/lib/active_storage_validations/matchers/base_size_validator_matcher.rb +134 -0
  30. data/lib/active_storage_validations/matchers/concerns/active_storageable.rb +17 -0
  31. data/lib/active_storage_validations/matchers/concerns/allow_blankable.rb +26 -0
  32. data/lib/active_storage_validations/matchers/concerns/attachable.rb +48 -0
  33. data/lib/active_storage_validations/matchers/concerns/contextable.rb +47 -0
  34. data/lib/active_storage_validations/matchers/concerns/messageable.rb +26 -0
  35. data/lib/active_storage_validations/matchers/concerns/rspecable.rb +25 -0
  36. data/lib/active_storage_validations/matchers/concerns/validatable.rb +11 -10
  37. data/lib/active_storage_validations/matchers/content_type_validator_matcher.rb +44 -27
  38. data/lib/active_storage_validations/matchers/dimension_validator_matcher.rb +67 -59
  39. data/lib/active_storage_validations/matchers/processable_image_validator_matcher.rb +78 -0
  40. data/lib/active_storage_validations/matchers/size_validator_matcher.rb +8 -126
  41. data/lib/active_storage_validations/matchers/total_size_validator_matcher.rb +40 -0
  42. data/lib/active_storage_validations/matchers.rb +3 -0
  43. data/lib/active_storage_validations/metadata.rb +60 -28
  44. data/lib/active_storage_validations/processable_image_validator.rb +14 -5
  45. data/lib/active_storage_validations/size_validator.rb +7 -51
  46. data/lib/active_storage_validations/total_size_validator.rb +49 -0
  47. data/lib/active_storage_validations/version.rb +1 -1
  48. data/lib/active_storage_validations.rb +3 -2
  49. metadata +38 -39
  50. data/lib/active_storage_validations/error_handler.rb +0 -21
@@ -1,22 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'concerns/errorable.rb'
3
4
  require_relative 'concerns/symbolizable.rb'
5
+ require_relative 'base_size_validator.rb'
4
6
 
5
7
  module ActiveStorageValidations
6
- class SizeValidator < ActiveModel::EachValidator # :nodoc:
7
- include OptionProcUnfolding
8
- include ErrorHandler
9
- include Symbolizable
10
-
11
- delegate :number_to_human_size, to: ActiveSupport::NumberHelper
12
-
13
- AVAILABLE_CHECKS = %i[
14
- less_than
15
- less_than_or_equal_to
16
- greater_than
17
- greater_than_or_equal_to
18
- between
19
- ].freeze
8
+ class SizeValidator < BaseSizeValidator
20
9
  ERROR_TYPES = %i[
21
10
  file_size_not_less_than
22
11
  file_size_not_less_than_or_equal_to
@@ -25,28 +14,19 @@ module ActiveStorageValidations
25
14
  file_size_not_between
26
15
  ].freeze
27
16
 
28
- def check_validity!
29
- unless AVAILABLE_CHECKS.one? { |argument| options.key?(argument) }
30
- raise ArgumentError, 'You must pass either :less_than(_or_equal_to), :greater_than(_or_equal_to), or :between to the validator'
31
- end
32
- end
33
-
34
17
  def validate_each(record, attribute, _value)
35
- # only attached
36
18
  return true unless record.send(attribute).attached?
37
19
 
38
20
  files = Array.wrap(record.send(attribute))
39
-
40
- errors_options = initialize_error_options(options)
41
-
42
21
  flat_options = unfold_procs(record, self.options, AVAILABLE_CHECKS)
43
22
 
44
23
  files.each do |file|
45
- next if content_size_valid?(file.blob.byte_size, flat_options)
24
+ next if is_valid?(file.blob.byte_size, flat_options)
46
25
 
26
+ errors_options = initialize_error_options(options, file)
27
+ populate_error_options(errors_options, flat_options)
47
28
  errors_options[:file_size] = number_to_human_size(file.blob.byte_size)
48
- errors_options[:min_size] = number_to_human_size(min_size(flat_options))
49
- errors_options[:max_size] = number_to_human_size(max_size(flat_options))
29
+
50
30
  keys = AVAILABLE_CHECKS & flat_options.keys
51
31
  error_type = "file_size_not_#{keys.first}".to_sym
52
32
 
@@ -54,29 +34,5 @@ module ActiveStorageValidations
54
34
  break
55
35
  end
56
36
  end
57
-
58
- def content_size_valid?(file_size, flat_options)
59
- return false if file_size < 0
60
-
61
- if flat_options[:between].present?
62
- flat_options[:between].include?(file_size)
63
- elsif flat_options[:less_than].present?
64
- file_size < flat_options[:less_than]
65
- elsif flat_options[:less_than_or_equal_to].present?
66
- file_size <= flat_options[:less_than_or_equal_to]
67
- elsif flat_options[:greater_than].present?
68
- file_size > flat_options[:greater_than]
69
- elsif flat_options[:greater_than_or_equal_to].present?
70
- file_size >= flat_options[:greater_than_or_equal_to]
71
- end
72
- end
73
-
74
- def min_size(flat_options)
75
- flat_options[:between]&.min || flat_options[:greater_than] || flat_options[:greater_than_or_equal_to]
76
- end
77
-
78
- def max_size(flat_options)
79
- flat_options[:between]&.max || flat_options[:less_than] || flat_options[:less_than_or_equal_to]
80
- end
81
37
  end
82
38
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'concerns/errorable.rb'
4
+ require_relative 'concerns/symbolizable.rb'
5
+ require_relative 'base_size_validator.rb'
6
+
7
+ module ActiveStorageValidations
8
+ class TotalSizeValidator < BaseSizeValidator
9
+ ERROR_TYPES = %i[
10
+ total_file_size_not_less_than
11
+ total_file_size_not_less_than_or_equal_to
12
+ total_file_size_not_greater_than
13
+ total_file_size_not_greater_than_or_equal_to
14
+ total_file_size_not_between
15
+ ].freeze
16
+
17
+ def validate_each(record, attribute, _value)
18
+ custom_check_validity!(record, attribute)
19
+
20
+ return true unless record.send(attribute).attached?
21
+
22
+ files = Array.wrap(record.send(attribute))
23
+ flat_options = unfold_procs(record, self.options, AVAILABLE_CHECKS)
24
+
25
+ total_file_size = files.sum { |file| file.blob.byte_size }
26
+
27
+ return true if is_valid?(total_file_size, flat_options)
28
+
29
+ errors_options = initialize_error_options(options, nil)
30
+ populate_error_options(errors_options, flat_options)
31
+ errors_options[:total_file_size] = number_to_human_size(total_file_size)
32
+
33
+ keys = AVAILABLE_CHECKS & flat_options.keys
34
+ error_type = "total_file_size_not_#{keys.first}".to_sym
35
+
36
+ add_error(record, attribute, error_type, **errors_options)
37
+ end
38
+
39
+ private
40
+
41
+ def custom_check_validity!(record, attribute)
42
+ # We can't perform this check in the #check_validity! hook because we do not
43
+ # have enough data (only options & attributes are accessible)
44
+ unless record.send(attribute).is_a?(ActiveStorage::Attached::Many)
45
+ raise ArgumentError, 'This validator is only available for has_many_attached relations'
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStorageValidations
4
- VERSION = '1.1.3'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -1,18 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_model'
4
+ require 'active_support/concern'
4
5
 
5
6
  require 'active_storage_validations/railtie'
6
7
  require 'active_storage_validations/engine'
7
8
  require 'active_storage_validations/option_proc_unfolding'
8
- require 'active_storage_validations/error_handler'
9
9
  require 'active_storage_validations/attached_validator'
10
10
  require 'active_storage_validations/content_type_validator'
11
- require 'active_storage_validations/size_validator'
12
11
  require 'active_storage_validations/limit_validator'
13
12
  require 'active_storage_validations/dimension_validator'
14
13
  require 'active_storage_validations/aspect_ratio_validator'
15
14
  require 'active_storage_validations/processable_image_validator'
15
+ require 'active_storage_validations/size_validator'
16
+ require 'active_storage_validations/total_size_validator'
16
17
 
17
18
  ActiveSupport.on_load(:active_record) do
18
19
  send :include, ActiveStorageValidations
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-21 00:00:00.000000000 Z
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 5.2.0
69
- - !ruby/object:Gem::Dependency
70
- name: minitest-focus
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '1.4'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '1.4'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: combustion
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -95,49 +81,49 @@ dependencies:
95
81
  - !ruby/object:Gem::Version
96
82
  version: '1.3'
97
83
  - !ruby/object:Gem::Dependency
98
- name: mini_magick
84
+ name: marcel
99
85
  requirement: !ruby/object:Gem::Requirement
100
86
  requirements:
101
87
  - - ">="
102
88
  - !ruby/object:Gem::Version
103
- version: 4.9.5
89
+ version: '0'
104
90
  type: :development
105
91
  prerelease: false
106
92
  version_requirements: !ruby/object:Gem::Requirement
107
93
  requirements:
108
94
  - - ">="
109
95
  - !ruby/object:Gem::Version
110
- version: 4.9.5
96
+ version: '0'
111
97
  - !ruby/object:Gem::Dependency
112
- name: ruby-vips
98
+ name: mini_magick
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
101
  - - ">="
116
102
  - !ruby/object:Gem::Version
117
- version: 2.1.0
103
+ version: 4.9.5
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
108
  - - ">="
123
109
  - !ruby/object:Gem::Version
124
- version: 2.1.0
110
+ version: 4.9.5
125
111
  - !ruby/object:Gem::Dependency
126
- name: pry
112
+ name: minitest-focus
127
113
  requirement: !ruby/object:Gem::Requirement
128
114
  requirements:
129
- - - ">="
115
+ - - "~>"
130
116
  - !ruby/object:Gem::Version
131
- version: '0'
117
+ version: '1.4'
132
118
  type: :development
133
119
  prerelease: false
134
120
  version_requirements: !ruby/object:Gem::Requirement
135
121
  requirements:
136
- - - ">="
122
+ - - "~>"
137
123
  - !ruby/object:Gem::Version
138
- version: '0'
124
+ version: '1.4'
139
125
  - !ruby/object:Gem::Dependency
140
- name: rubocop
126
+ name: pry
141
127
  requirement: !ruby/object:Gem::Requirement
142
128
  requirements:
143
129
  - - ">="
@@ -151,21 +137,21 @@ dependencies:
151
137
  - !ruby/object:Gem::Version
152
138
  version: '0'
153
139
  - !ruby/object:Gem::Dependency
154
- name: sqlite3
140
+ name: ruby-vips
155
141
  requirement: !ruby/object:Gem::Requirement
156
142
  requirements:
157
143
  - - ">="
158
144
  - !ruby/object:Gem::Version
159
- version: '0'
145
+ version: 2.1.0
160
146
  type: :development
161
147
  prerelease: false
162
148
  version_requirements: !ruby/object:Gem::Requirement
163
149
  requirements:
164
150
  - - ">="
165
151
  - !ruby/object:Gem::Version
166
- version: '0'
152
+ version: 2.1.0
167
153
  - !ruby/object:Gem::Dependency
168
- name: marcel
154
+ name: simplecov
169
155
  requirement: !ruby/object:Gem::Requirement
170
156
  requirements:
171
157
  - - ">="
@@ -179,7 +165,7 @@ dependencies:
179
165
  - !ruby/object:Gem::Version
180
166
  version: '0'
181
167
  - !ruby/object:Gem::Dependency
182
- name: simplecov
168
+ name: sqlite3
183
169
  requirement: !ruby/object:Gem::Requirement
184
170
  requirements:
185
171
  - - ">="
@@ -193,19 +179,19 @@ dependencies:
193
179
  - !ruby/object:Gem::Version
194
180
  version: '0'
195
181
  - !ruby/object:Gem::Dependency
196
- name: globalid
182
+ name: webmock
197
183
  requirement: !ruby/object:Gem::Requirement
198
184
  requirements:
199
185
  - - ">="
200
186
  - !ruby/object:Gem::Version
201
- version: '0'
187
+ version: '3'
202
188
  type: :development
203
189
  prerelease: false
204
190
  version_requirements: !ruby/object:Gem::Requirement
205
191
  requirements:
206
192
  - - ">="
207
193
  - !ruby/object:Gem::Version
208
- version: '0'
194
+ version: '3'
209
195
  description: Validations for Active Storage (presence)
210
196
  email:
211
197
  - igorkasyanchuk@gmail.com
@@ -216,6 +202,7 @@ files:
216
202
  - MIT-LICENSE
217
203
  - README.md
218
204
  - Rakefile
205
+ - config/locales/da.yml
219
206
  - config/locales/de.yml
220
207
  - config/locales/en.yml
221
208
  - config/locales/es.yml
@@ -234,23 +221,35 @@ files:
234
221
  - lib/active_storage_validations.rb
235
222
  - lib/active_storage_validations/aspect_ratio_validator.rb
236
223
  - lib/active_storage_validations/attached_validator.rb
224
+ - lib/active_storage_validations/base_size_validator.rb
225
+ - lib/active_storage_validations/concerns/errorable.rb
237
226
  - lib/active_storage_validations/concerns/symbolizable.rb
238
227
  - lib/active_storage_validations/content_type_validator.rb
239
228
  - lib/active_storage_validations/dimension_validator.rb
240
229
  - lib/active_storage_validations/engine.rb
241
- - lib/active_storage_validations/error_handler.rb
242
230
  - lib/active_storage_validations/limit_validator.rb
243
231
  - lib/active_storage_validations/matchers.rb
232
+ - lib/active_storage_validations/matchers/aspect_ratio_validator_matcher.rb
244
233
  - lib/active_storage_validations/matchers/attached_validator_matcher.rb
234
+ - lib/active_storage_validations/matchers/base_size_validator_matcher.rb
235
+ - lib/active_storage_validations/matchers/concerns/active_storageable.rb
236
+ - lib/active_storage_validations/matchers/concerns/allow_blankable.rb
237
+ - lib/active_storage_validations/matchers/concerns/attachable.rb
238
+ - lib/active_storage_validations/matchers/concerns/contextable.rb
239
+ - lib/active_storage_validations/matchers/concerns/messageable.rb
240
+ - lib/active_storage_validations/matchers/concerns/rspecable.rb
245
241
  - lib/active_storage_validations/matchers/concerns/validatable.rb
246
242
  - lib/active_storage_validations/matchers/content_type_validator_matcher.rb
247
243
  - lib/active_storage_validations/matchers/dimension_validator_matcher.rb
244
+ - lib/active_storage_validations/matchers/processable_image_validator_matcher.rb
248
245
  - lib/active_storage_validations/matchers/size_validator_matcher.rb
246
+ - lib/active_storage_validations/matchers/total_size_validator_matcher.rb
249
247
  - lib/active_storage_validations/metadata.rb
250
248
  - lib/active_storage_validations/option_proc_unfolding.rb
251
249
  - lib/active_storage_validations/processable_image_validator.rb
252
250
  - lib/active_storage_validations/railtie.rb
253
251
  - lib/active_storage_validations/size_validator.rb
252
+ - lib/active_storage_validations/total_size_validator.rb
254
253
  - lib/active_storage_validations/version.rb
255
254
  - lib/tasks/active_storage_validations_tasks.rake
256
255
  homepage: https://github.com/igorkasyanchuk/active_storage_validations
@@ -266,14 +265,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
266
265
  requirements:
267
266
  - - ">="
268
267
  - !ruby/object:Gem::Version
269
- version: '0'
268
+ version: 2.5.0
270
269
  required_rubygems_version: !ruby/object:Gem::Requirement
271
270
  requirements:
272
271
  - - ">="
273
272
  - !ruby/object:Gem::Version
274
273
  version: '0'
275
274
  requirements: []
276
- rubygems_version: 3.4.10
275
+ rubygems_version: 3.5.11
277
276
  signing_key:
278
277
  specification_version: 4
279
278
  summary: Validations for Active Storage
@@ -1,21 +0,0 @@
1
- module ActiveStorageValidations
2
- module ErrorHandler
3
-
4
- def initialize_error_options(options)
5
- {
6
- validator_type: self.class.to_sym,
7
- custom_message: (options[:message] if options[:message].present?)
8
- }.compact
9
- end
10
-
11
- def add_error(record, attribute, error_type, **errors_options)
12
- type = errors_options[:custom_message].presence || error_type
13
- return if record.errors.added?(attribute, type)
14
-
15
- # You can read https://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-add
16
- # to better understand how Rails model errors work
17
- record.errors.add(attribute, type, **errors_options)
18
- end
19
-
20
- end
21
- end