active_storage_validations 0.9.0 → 0.9.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63ae1be092a1780d41de3af6c4189ee9e92804ce53b9fb30f91c929eb4c2efc6
4
- data.tar.gz: 73e74ca24800fc11bbdc3b0b95e2f044311b96728cb7da30209c00af77e45b32
3
+ metadata.gz: 0475512c246a633adcc226c575f66826ffa09df688fda330f2b862d7229a3e3a
4
+ data.tar.gz: c52cbf3aa8527ddfc7e17f8619d63f1f3b008360e969f054a129d8e0f6af2e51
5
5
  SHA512:
6
- metadata.gz: c701c6d1db92c8d0db7ca00616a6247462abe94486d73dcb71c0d41e99f82081af8cfe84fb7f2d56a719dc70fe47d696208ee372a26dfed770dc0cf9106eafd3
7
- data.tar.gz: b437692cf0c6d11e4ad341c413c448e825bd29537696d885cdb6fbe30fd8a1b9b56d724cbaee41c9b3656b7261544b00d89cbaa809d69773d1786b21fad77d1a
6
+ metadata.gz: f472fa75d9d021f8ab55bff5dd900d738449884f9bf6ee72d953bb34c988acf01904c29dca7586f5d82ad192b9914bfd9e6f365d413615ef1b3aa18b9635cbb1
7
+ data.tar.gz: 8f1b3da90613ff72ab85aaa6faf6fe7403b132e1806c2e4f60156cf95d9f9dfd1dac20d950c7eb0cda64229427deb51d1ce2e4b1742020040c78853c8cdc1bba
data/README.md CHANGED
@@ -287,6 +287,7 @@ To run tests in root folder of gem:
287
287
 
288
288
  * `BUNDLE_GEMFILE=gemfiles/rails_5_2.gemfile bundle exec rake test` to run for Rails 5.2
289
289
  * `BUNDLE_GEMFILE=gemfiles/rails_6_0.gemfile bundle exec rake test` to run for Rails 6.0
290
+ * `BUNDLE_GEMFILE=gemfiles/rails_6_1.gemfile bundle exec rake test` to run for Rails 6.1 (TESTS ARE NOT READY, migration in DB is needed for specs)
290
291
 
291
292
  To play with app `cd test/dummy` and `rails s -b 0.0.0.0` (before `rails db:migrate`).
292
293
 
@@ -332,6 +333,9 @@ You are welcome to contribute.
332
333
  - https://github.com/mschnitzer
333
334
  - https://github.com/sinankeskin
334
335
  - https://github.com/alejandrodevs
336
+ - https://github.com/molfar
337
+ - https://github.com/connorshea
338
+ - https://github.com/yshmarov
335
339
 
336
340
  ## License
337
341
 
@@ -0,0 +1,22 @@
1
+ es:
2
+ errors:
3
+ messages:
4
+ content_type_invalid: "tiene un tipo de contenido inválido"
5
+ file_size_out_of_range: "tamaño %{file_size} no está entre el rango requerido"
6
+ limit_out_of_range: "el número total está fuera de rango"
7
+ image_metadata_missing: "no es una imagen válida"
8
+ dimension_min_inclusion: "debe ser mayor o igual a %{width} x %{height} pixel"
9
+ dimension_max_inclusion: "debe ser menor o igual a %{width} x %{height} pixel"
10
+ dimension_width_inclusion: "el ancho no se incluye entre %{min} y %{max} pixel"
11
+ dimension_height_inclusion: "la altura no se incluye entre %{min} y %{max} pixel"
12
+ dimension_width_greater_than_or_equal_to: "el ancho debe ser mayor o igual a %{length} pixel"
13
+ dimension_height_greater_than_or_equal_to: "la altura debe ser mayor o igual a %{length} pixel"
14
+ dimension_width_less_than_or_equal_to: "el ancho debe ser menor o igual a %{length} pixel"
15
+ dimension_height_less_than_or_equal_to: "la altura debe ser menor o igual a %{length} pixel"
16
+ dimension_width_equal_to: "el ancho debe ser igual a %{length} pixel"
17
+ dimension_height_equal_to: "la altura debe ser igual a %{length} pixel"
18
+ aspect_ratio_not_square: "debe ser una imagen cuadrada"
19
+ aspect_ratio_not_portrait: "debe ser una imagen de retrato"
20
+ aspect_ratio_not_landscape: "debe ser una imagen de paisaje"
21
+ aspect_ratio_is_not: "debe tener una relación de aspecto de %{aspect_ratio}"
22
+ aspect_ratio_unknown: "tiene una relación de aspecto desconocida"
@@ -0,0 +1,22 @@
1
+ nl:
2
+ errors:
3
+ messages:
4
+ content_type_invalid: "heeft een ongeldig inhoudstype"
5
+ file_size_out_of_range: "bestandsgrootte %{file_size} valt niet tussen het vereiste bereik"
6
+ limit_out_of_range: "totaal aantal valt buiten het vereiste bereik"
7
+ image_metadata_missing: "is geen geldige afbeelding"
8
+ dimension_min_inclusion: "moet groter of gelijk zijn aan %{width} x %{height} pixels"
9
+ dimension_max_inclusion: "moet kleiner of gelijk zijn aan %{width} x %{height} pixels"
10
+ dimension_width_inclusion: "breedte niet tussen de %{min} en %{max} pixels"
11
+ dimension_height_inclusion: "hoogte niet tussen de %{min} en %{max} pixels"
12
+ dimension_width_greater_than_or_equal_to: "breedte moet groter of gelijk zijn aan %{length} pixels"
13
+ dimension_height_greater_than_or_equal_to: "hoogte moet groter of gelijk zijn aan %{length} pixels"
14
+ dimension_width_less_than_or_equal_to: "breedte moet kleiner of gelijk zijn aan %{length} pixels"
15
+ dimension_height_less_than_or_equal_to: "hoogte moet kleiner of gelijk zijn aan %{length} pixels"
16
+ dimension_width_equal_to: "breedte moet gelijk zijn aan %{length} pixels"
17
+ dimension_height_equal_to: "hoogte moet gelijk zijn aan %{length} pixels"
18
+ aspect_ratio_not_square: "moet een vierkante afbeelding zijn"
19
+ aspect_ratio_not_portrait: "moet een staande afbeelding zijn"
20
+ aspect_ratio_not_landscape: "moet een liggende afbeelding zijn"
21
+ aspect_ratio_is_not: "moet een beeldverhouding hebben van %{aspect_ratio}"
22
+ aspect_ratio_unknown: "heeft een onbekende beeldverhouding"
@@ -1,22 +1,22 @@
1
1
  uk:
2
2
  errors:
3
3
  messages:
4
- content_type_invalid: "має неприпустимий тип вмісту"
5
- file_size_out_of_range: "розмір %{file_size} більше необхідного"
6
- limit_out_of_range: "кількість файлів більше необхідного"
7
- image_metadata_missing: "не є допустимим зображенням"
8
- dimension_min_inclusion: "мусить бути більше або дорівнювати %{width} x %{height} пікселям"
9
- dimension_max_inclusion: "мусить бути менше або дорівнювати %{width} x %{height} пікселям"
10
- dimension_width_inclusion: "ширина не включена між %{min} і %{max} пікселям"
11
- dimension_height_inclusion: "висота не включена між %{min} і %{max} пікселям"
12
- dimension_width_greater_than_or_equal_to: "ширина мусить бути більше або дорівнювати %{length} пікселям"
13
- dimension_height_greater_than_or_equal_to: "висота мусить бути більше або дорівнювати %{length} пікселям"
14
- dimension_width_less_than_or_equal_to: "ширина мусить бути менше або дорівнювати %{length} пікселям"
15
- dimension_height_less_than_or_equal_to: "висота мусить бути менше або дорівнювати %{length} пікселям"
16
- dimension_width_equal_to: "ширина мусить дорівнювати %{length} пікселям"
17
- dimension_height_equal_to: "висота мусить дорівнювати %{length} пікселям"
18
- aspect_ratio_not_square: "мусить бути квадратне зображення"
19
- aspect_ratio_not_portrait: "мусить бути портретне зображення"
20
- aspect_ratio_not_landscape: "мусить бути пейзажне зображення"
21
- aspect_ratio_is_not: "мусить мати співвідношення сторін %{aspect_ratio}"
22
- aspect_ratio_unknown: "має невідоме співвідношення сторін"
4
+ content_type_invalid: "має неприпустимий тип вмісту"
5
+ file_size_out_of_range: "розмір %{file_size} більше необхідного"
6
+ limit_out_of_range: "кількість файлів більше необхідного"
7
+ image_metadata_missing: "не є допустимим зображенням"
8
+ dimension_min_inclusion: "мусить бути більше або дорівнювати %{width} x %{height} пікселям"
9
+ dimension_max_inclusion: "мусить бути менше або дорівнювати %{width} x %{height} пікселям"
10
+ dimension_width_inclusion: "ширина не включена між %{min} і %{max} пікселям"
11
+ dimension_height_inclusion: "висота не включена між %{min} і %{max} пікселям"
12
+ dimension_width_greater_than_or_equal_to: "ширина мусить бути більше або дорівнювати %{length} пікселям"
13
+ dimension_height_greater_than_or_equal_to: "висота мусить бути більше або дорівнювати %{length} пікселям"
14
+ dimension_width_less_than_or_equal_to: "ширина мусить бути менше або дорівнювати %{length} пікселям"
15
+ dimension_height_less_than_or_equal_to: "висота мусить бути менше або дорівнювати %{length} пікселям"
16
+ dimension_width_equal_to: "ширина мусить дорівнювати %{length} пікселям"
17
+ dimension_height_equal_to: "висота мусить дорівнювати %{length} пікселям"
18
+ aspect_ratio_not_square: "мусить бути квадратне зображення"
19
+ aspect_ratio_not_portrait: "мусить бути портретне зображення"
20
+ aspect_ratio_not_landscape: "мусить бути пейзажне зображення"
21
+ aspect_ratio_is_not: "мусить мати співвідношення сторін %{aspect_ratio}"
22
+ aspect_ratio_unknown: "має невідоме співвідношення сторін"
@@ -14,7 +14,7 @@ module ActiveStorageValidations
14
14
  next if is_valid?(file)
15
15
 
16
16
  errors_options[:content_type] = content_type(file)
17
- record.errors.add(attribute, :content_type_invalid, errors_options)
17
+ record.errors.add(attribute, :content_type_invalid, **errors_options)
18
18
  break
19
19
  end
20
20
  end
@@ -123,7 +123,7 @@ module ActiveStorageValidations
123
123
  key = options[:message].presence || type
124
124
  return if record.errors.added?(attribute, key)
125
125
  record.errors.add(attribute, key, *attrs)
126
- end
126
+ end
127
127
 
128
128
  end
129
129
  end
@@ -17,7 +17,7 @@ module ActiveStorageValidations
17
17
  errors_options = { min: options[:min], max: options[:max] }
18
18
 
19
19
  return true if files_count_valid?(files.count)
20
- record.errors.add(attribute, options[:message].presence || :limit_out_of_range, errors_options)
20
+ record.errors.add(attribute, options[:message].presence || :limit_out_of_range, **errors_options)
21
21
  end
22
22
 
23
23
  def files_count_valid?(count)
@@ -17,7 +17,7 @@ module ActiveStorageValidations
17
17
 
18
18
  def matches?(subject)
19
19
  @subject = subject.is_a?(Class) ? subject.new : subject
20
- invalid_when_not_attached && valid_when_attached
20
+ responds_to_methods && valid_when_attached && invalid_when_not_attached
21
21
  end
22
22
 
23
23
  def failure_message
@@ -30,17 +30,25 @@ module ActiveStorageValidations
30
30
 
31
31
  private
32
32
 
33
+ def responds_to_methods
34
+ @subject.respond_to?(@attribute_name) &&
35
+ @subject.public_send(@attribute_name).respond_to?(:attach) &&
36
+ @subject.public_send(@attribute_name).respond_to?(:detach)
37
+ end
38
+
33
39
  def valid_when_attached
34
- instance = @subject
35
- instance.public_send(@attribute_name).attach(attachable)
36
- instance.validate
37
- instance.errors.details[@attribute_name].exclude?(error: :blank)
40
+ @subject.public_send(@attribute_name).attach(attachable) unless @subject.public_send(@attribute_name).attached?
41
+ @subject.validate
42
+ @subject.errors.details[@attribute_name].exclude?(error: :blank)
38
43
  end
39
44
 
40
45
  def invalid_when_not_attached
41
- instance = @subject
42
- instance.validate
43
- instance.errors.details[@attribute_name].include?(error: :blank)
46
+ @subject.public_send(@attribute_name).detach
47
+ # Unset the direct relation since `detach` on an unpersisted record does not set `attached?` to false.
48
+ @subject.public_send("#{@attribute_name}=", nil)
49
+
50
+ @subject.validate
51
+ @subject.errors.details[@attribute_name].include?(error: :blank)
44
52
  end
45
53
 
46
54
  def attachable
@@ -29,7 +29,7 @@ module ActiveStorageValidations
29
29
 
30
30
  def matches?(subject)
31
31
  @subject = subject.is_a?(Class) ? subject.new : subject
32
- allowed_types_allowed? && rejected_types_rejected?
32
+ responds_to_methods && allowed_types_allowed? && rejected_types_rejected?
33
33
  end
34
34
 
35
35
  def failure_message
@@ -46,6 +46,12 @@ module ActiveStorageValidations
46
46
 
47
47
  protected
48
48
 
49
+ def responds_to_methods
50
+ @subject.respond_to?(@attribute_name) &&
51
+ @subject.public_send(@attribute_name).respond_to?(:attach) &&
52
+ @subject.public_send(@attribute_name).respond_to?(:detach)
53
+ end
54
+
49
55
  def allowed_types
50
56
  @allowed_types || []
51
57
  end
@@ -58,7 +58,8 @@ module ActiveStorageValidations
58
58
 
59
59
  def matches?(subject)
60
60
  @subject = subject.is_a?(Class) ? subject.new : subject
61
- width_smaller_than_min? && width_larger_than_min? && width_smaller_than_max? && width_larger_than_max? && width_equals? &&
61
+ responds_to_methods &&
62
+ width_smaller_than_min? && width_larger_than_min? && width_smaller_than_max? && width_larger_than_max? && width_equals? &&
62
63
  height_smaller_than_min? && height_larger_than_min? && height_smaller_than_max? && height_larger_than_max? && height_equals?
63
64
  end
64
65
 
@@ -72,6 +73,12 @@ module ActiveStorageValidations
72
73
 
73
74
  protected
74
75
 
76
+ def responds_to_methods
77
+ @subject.respond_to?(@attribute_name) &&
78
+ @subject.public_send(@attribute_name).respond_to?(:attach) &&
79
+ @subject.public_send(@attribute_name).respond_to?(:detach)
80
+ end
81
+
75
82
  def valid_width
76
83
  ((@width_min || 0) + (@width_max || 2000)) / 2
77
84
  end
@@ -45,7 +45,7 @@ module ActiveStorageValidations
45
45
 
46
46
  def matches?(subject)
47
47
  @subject = subject.is_a?(Class) ? subject.new : subject
48
- lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
48
+ responds_to_methods && lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
49
49
  end
50
50
 
51
51
  def failure_message
@@ -58,6 +58,12 @@ module ActiveStorageValidations
58
58
 
59
59
  protected
60
60
 
61
+ def responds_to_methods
62
+ @subject.respond_to?(@attribute_name) &&
63
+ @subject.public_send(@attribute_name).respond_to?(:attach) &&
64
+ @subject.public_send(@attribute_name).respond_to?(:detach)
65
+ end
66
+
61
67
  def lower_than_low?
62
68
  @low.nil? || !passes_validation_with_size(@low - 1)
63
69
  end
@@ -21,7 +21,17 @@ module ActiveStorageValidations
21
21
  def read_image
22
22
  is_string = file.is_a?(String)
23
23
  if is_string || file.is_a?(ActiveStorage::Blob)
24
- blob = is_string ? ActiveStorage::Blob.find_signed(file) : file
24
+ if is_string
25
+ # If Rails 5.2 or 6.0, use `find_signed`
26
+ if Rails::VERSION::MAJOR < 6 || (Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 0)
27
+ blob = ActiveStorage::Blob.find_signed(file)
28
+ # If Rails 6.1 or higher, use `find_signed!`
29
+ elsif Rails::VERSION::MAJOR > 6 || (Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR >= 1)
30
+ blob = ActiveStorage::Blob.find_signed!(file)
31
+ end
32
+ else
33
+ blob = file
34
+ end
25
35
 
26
36
  tempfile = Tempfile.new(["ActiveStorage-#{blob.id}-", blob.filename.extension_with_delimiter])
27
37
  tempfile.binmode
@@ -25,7 +25,7 @@ module ActiveStorageValidations
25
25
  next if content_size_valid?(file.blob.byte_size)
26
26
 
27
27
  errors_options[:file_size] = number_to_human_size(file.blob.byte_size)
28
- record.errors.add(attribute, :file_size_out_of_range, errors_options)
28
+ record.errors.add(attribute, :file_size_out_of_range, **errors_options)
29
29
  break
30
30
  end
31
31
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStorageValidations
4
- VERSION = '0.9.0'
4
+ VERSION = '0.9.1'
5
5
  end
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: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-18 00:00:00.000000000 Z
11
+ date: 2020-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -92,8 +92,10 @@ files:
92
92
  - Rakefile
93
93
  - config/locales/de.yml
94
94
  - config/locales/en.yml
95
+ - config/locales/es.yml
95
96
  - config/locales/fr.yml
96
97
  - config/locales/ja.yml
98
+ - config/locales/nl.yml
97
99
  - config/locales/pt-BR.yml
98
100
  - config/locales/ru.yml
99
101
  - config/locales/tr.yml