active_storage_validations 2.0.4 → 3.0.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.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +125 -19
  3. data/config/locales/da.yml +9 -0
  4. data/config/locales/de.yml +9 -0
  5. data/config/locales/en-GB.yml +9 -0
  6. data/config/locales/en.yml +9 -0
  7. data/config/locales/es.yml +9 -0
  8. data/config/locales/fr.yml +9 -0
  9. data/config/locales/it.yml +9 -0
  10. data/config/locales/ja.yml +55 -46
  11. data/config/locales/nl.yml +9 -0
  12. data/config/locales/pl.yml +9 -0
  13. data/config/locales/pt-BR.yml +9 -0
  14. data/config/locales/ru.yml +9 -0
  15. data/config/locales/sv.yml +10 -1
  16. data/config/locales/tr.yml +9 -0
  17. data/config/locales/uk.yml +9 -0
  18. data/config/locales/vi.yml +9 -0
  19. data/config/locales/zh-CN.yml +9 -0
  20. data/lib/active_storage_validations/analyzer/pdf_analyzer.rb +89 -0
  21. data/lib/active_storage_validations/base_comparison_validator.rb +13 -1
  22. data/lib/active_storage_validations/duration_validator.rb +1 -0
  23. data/lib/active_storage_validations/matchers/base_comparison_validator_matcher.rb +11 -1
  24. data/lib/active_storage_validations/matchers/pages_validator_matcher.rb +39 -0
  25. data/lib/active_storage_validations/matchers.rb +1 -0
  26. data/lib/active_storage_validations/pages_validator.rb +61 -0
  27. data/lib/active_storage_validations/shared/asv_analyzable.rb +6 -0
  28. data/lib/active_storage_validations/shared/asv_attachable.rb +1 -1
  29. data/lib/active_storage_validations/size_validator.rb +1 -0
  30. data/lib/active_storage_validations/total_size_validator.rb +1 -0
  31. data/lib/active_storage_validations/version.rb +1 -1
  32. data/lib/active_storage_validations.rb +2 -0
  33. metadata +8 -2
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base_comparison_validator_matcher"
4
+
5
+ module ActiveStorageValidations
6
+ module Matchers
7
+ def validate_pages_of(attribute_name)
8
+ PagesValidatorMatcher.new(attribute_name)
9
+ end
10
+
11
+ class PagesValidatorMatcher < BaseComparisonValidatorMatcher
12
+ def description
13
+ "validate file number of pages of :#{@attribute_name}"
14
+ end
15
+
16
+ def failure_message
17
+ message = [ "is expected to validate file number of pages of :#{@attribute_name}" ]
18
+ build_failure_message(message)
19
+ message.join("\n")
20
+ end
21
+
22
+ private
23
+
24
+ def failure_message_unit
25
+ "pages"
26
+ end
27
+
28
+ def smallest_measurement
29
+ 1
30
+ end
31
+
32
+ def mock_value_for(io, pages)
33
+ Matchers.mock_metadata(io, { pages: pages }) do
34
+ yield
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -9,6 +9,7 @@ require "active_storage_validations/matchers/dimension_validator_matcher"
9
9
  require "active_storage_validations/matchers/duration_validator_matcher"
10
10
  require "active_storage_validations/matchers/size_validator_matcher"
11
11
  require "active_storage_validations/matchers/total_size_validator_matcher"
12
+ require "active_storage_validations/matchers/pages_validator_matcher"
12
13
 
13
14
  module ActiveStorageValidations
14
15
  module Matchers
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base_comparison_validator"
4
+ require_relative "shared/asv_analyzable"
5
+ require_relative "shared/asv_attachable"
6
+
7
+ module ActiveStorageValidations
8
+ class PagesValidator < BaseComparisonValidator
9
+ include ASVAnalyzable
10
+ include ASVAttachable
11
+
12
+ ERROR_TYPES = %i[
13
+ pages_not_less_than
14
+ pages_not_less_than_or_equal_to
15
+ pages_not_greater_than
16
+ pages_not_greater_than_or_equal_to
17
+ pages_not_between
18
+ pages_not_equal_to
19
+ ].freeze
20
+ METADATA_KEYS = %i[pages].freeze
21
+
22
+ delegate :number_to_delimited, to: ActiveSupport::NumberHelper
23
+
24
+ def validate_each(record, attribute, _value)
25
+ return if no_attachments?(record, attribute)
26
+
27
+ validate_changed_files_from_metadata(record, attribute, METADATA_KEYS)
28
+ end
29
+
30
+ private
31
+
32
+ def is_valid?(record, attribute, file, metadata)
33
+ flat_options = set_flat_options(record)
34
+ errors_options = initialize_error_options(options, file)
35
+
36
+ unless valid_metadata?(metadata)
37
+ add_media_metadata_missing_error(record, attribute, file, errors_options)
38
+ return false
39
+ end
40
+
41
+ return true if super(metadata[:pages], flat_options)
42
+
43
+ populate_error_options(errors_options, flat_options)
44
+ errors_options[:pages] = format_bound_value(metadata[:pages])
45
+
46
+ keys = AVAILABLE_CHECKS & flat_options.keys
47
+ error_type = "pages_not_#{keys.first}".to_sym
48
+
49
+ add_error(record, attribute, error_type, **errors_options)
50
+ false
51
+ end
52
+
53
+ def valid_metadata?(metadata)
54
+ metadata[:pages].to_i > 0
55
+ end
56
+
57
+ def format_bound_value(value)
58
+ number_to_delimited(value)
59
+ end
60
+ end
61
+ end
@@ -38,6 +38,8 @@ module ActiveStorageValidations
38
38
  end
39
39
 
40
40
  def metadata_analyzer_for(attachable)
41
+ return pdf_analyzer_for(attachable) if attachable_content_type(attachable) == "application/pdf"
42
+
41
43
  case attachable_media_type(attachable)
42
44
  when "image" then image_analyzer_for(attachable)
43
45
  when "video" then video_analyzer_for(attachable)
@@ -46,6 +48,10 @@ module ActiveStorageValidations
46
48
  end
47
49
  end
48
50
 
51
+ def pdf_analyzer_for(attachable)
52
+ ActiveStorageValidations::Analyzer::PdfAnalyzer.new(attachable)
53
+ end
54
+
49
55
  def image_analyzer_for(attachable)
50
56
  case image_processor
51
57
  when :mini_magick
@@ -88,7 +88,7 @@ module ActiveStorageValidations
88
88
  # Retrieve the declared content_type from attachable without potential mime
89
89
  # type parameters (e.g. 'application/x-rar-compressed;version=5')
90
90
  def attachable_content_type(attachable)
91
- full_attachable_content_type(attachable) && content_type_without_parameters(full_attachable_content_type(attachable))
91
+ (full_attachable_content_type(attachable) && content_type_without_parameters(full_attachable_content_type(attachable)) || marcel_content_type_from_filename(attachable))
92
92
  end
93
93
 
94
94
  # Remove the potential mime type parameters from the content_type (e.g.
@@ -10,6 +10,7 @@ module ActiveStorageValidations
10
10
  file_size_not_greater_than
11
11
  file_size_not_greater_than_or_equal_to
12
12
  file_size_not_between
13
+ file_size_not_equal_to
13
14
  ].freeze
14
15
 
15
16
  delegate :number_to_human_size, to: ActiveSupport::NumberHelper
@@ -10,6 +10,7 @@ module ActiveStorageValidations
10
10
  total_file_size_not_greater_than
11
11
  total_file_size_not_greater_than_or_equal_to
12
12
  total_file_size_not_between
13
+ total_file_size_not_equal_to
13
14
  ].freeze
14
15
 
15
16
  delegate :number_to_human_size, to: ActiveSupport::NumberHelper
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStorageValidations
4
- VERSION = "2.0.4"
4
+ VERSION = "3.0.1"
5
5
  end
@@ -10,6 +10,7 @@ require "active_storage_validations/analyzer/image_analyzer/vips"
10
10
  require "active_storage_validations/analyzer/null_analyzer"
11
11
  require "active_storage_validations/analyzer/video_analyzer"
12
12
  require "active_storage_validations/analyzer/audio_analyzer"
13
+ require "active_storage_validations/analyzer/pdf_analyzer"
13
14
 
14
15
  require "active_storage_validations/extensors/asv_blob_metadatable"
15
16
  require "active_storage_validations/extensors/asv_marcelable"
@@ -23,6 +24,7 @@ require "active_storage_validations/aspect_ratio_validator"
23
24
  require "active_storage_validations/processable_file_validator"
24
25
  require "active_storage_validations/size_validator"
25
26
  require "active_storage_validations/total_size_validator"
27
+ require "active_storage_validations/pages_validator"
26
28
 
27
29
  require "active_storage_validations/engine"
28
30
  require "active_storage_validations/railtie"
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: 2.0.4
4
+ version: 3.0.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: 2025-05-25 00:00:00.000000000 Z
11
+ date: 2025-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -267,6 +267,7 @@ files:
267
267
  - lib/active_storage_validations/analyzer/image_analyzer/image_magick.rb
268
268
  - lib/active_storage_validations/analyzer/image_analyzer/vips.rb
269
269
  - lib/active_storage_validations/analyzer/null_analyzer.rb
270
+ - lib/active_storage_validations/analyzer/pdf_analyzer.rb
270
271
  - lib/active_storage_validations/analyzer/shared/asv_ff_probable.rb
271
272
  - lib/active_storage_validations/analyzer/video_analyzer.rb
272
273
  - lib/active_storage_validations/aspect_ratio_validator.rb
@@ -287,6 +288,7 @@ files:
287
288
  - lib/active_storage_validations/matchers/dimension_validator_matcher.rb
288
289
  - lib/active_storage_validations/matchers/duration_validator_matcher.rb
289
290
  - lib/active_storage_validations/matchers/limit_validator_matcher.rb
291
+ - lib/active_storage_validations/matchers/pages_validator_matcher.rb
290
292
  - lib/active_storage_validations/matchers/processable_file_validator_matcher.rb
291
293
  - lib/active_storage_validations/matchers/shared/asv_active_storageable.rb
292
294
  - lib/active_storage_validations/matchers/shared/asv_allow_blankable.rb
@@ -297,6 +299,7 @@ files:
297
299
  - lib/active_storage_validations/matchers/shared/asv_validatable.rb
298
300
  - lib/active_storage_validations/matchers/size_validator_matcher.rb
299
301
  - lib/active_storage_validations/matchers/total_size_validator_matcher.rb
302
+ - lib/active_storage_validations/pages_validator.rb
300
303
  - lib/active_storage_validations/processable_file_validator.rb
301
304
  - lib/active_storage_validations/railtie.rb
302
305
  - lib/active_storage_validations/shared/asv_active_storageable.rb
@@ -315,6 +318,9 @@ licenses:
315
318
  - MIT
316
319
  metadata:
317
320
  rubygems_mfa_required: 'true'
321
+ changelog_uri: https://github.com/igorkasyanchuk/active_storage_validations/blob/master/CHANGES.md
322
+ source_code_uri: https://github.com/igorkasyanchuk/active_storage_validations
323
+ bug_tracker_uri: https://github.com/igorkasyanchuk/active_storage_validations/issues
318
324
  post_install_message:
319
325
  rdoc_options: []
320
326
  require_paths: