active_storage_validations 0.8.2 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Big thank you to the paperclip validation matchers:
4
+ # https://github.com/thoughtbot/paperclip/blob/v6.1.0/lib/paperclip/matchers/validate_attachment_size_matcher.rb
5
+ module ActiveStorageValidations
6
+ module Matchers
7
+ def validate_size_of(name)
8
+ SizeValidatorMatcher.new(name)
9
+ end
10
+
11
+ class SizeValidatorMatcher
12
+ def initialize(attribute_name)
13
+ @attribute_name = attribute_name
14
+ @low = @high = nil
15
+ end
16
+
17
+ def description
18
+ "validate file size of #{@attribute_name}"
19
+ end
20
+
21
+ def less_than(size)
22
+ @high = size - 1.byte
23
+ self
24
+ end
25
+
26
+ def less_than_or_equal_to(size)
27
+ @high = size
28
+ self
29
+ end
30
+
31
+ def greater_than(size)
32
+ @low = size + 1.byte
33
+ self
34
+ end
35
+
36
+ def greater_than_or_equal_to(size)
37
+ @low = size
38
+ self
39
+ end
40
+
41
+ def between(range)
42
+ @low, @high = range.first, range.last
43
+ self
44
+ end
45
+
46
+ def matches?(subject)
47
+ @subject = subject.is_a?(Class) ? subject.new : subject
48
+ responds_to_methods && lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
49
+ end
50
+
51
+ def failure_message
52
+ "is expected to validate file size of #{@attribute_name} to be between #{@low} and #{@high} bytes"
53
+ end
54
+
55
+ def failure_message_when_negated
56
+ "is expected to not validate file size of #{@attribute_name} to be between #{@low} and #{@high} bytes"
57
+ end
58
+
59
+ protected
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
+
67
+ def lower_than_low?
68
+ @low.nil? || !passes_validation_with_size(@low - 1)
69
+ end
70
+
71
+ def higher_than_low?
72
+ @low.nil? || passes_validation_with_size(@low + 1)
73
+ end
74
+
75
+ def lower_than_high?
76
+ @high.nil? || @high == Float::INFINITY || passes_validation_with_size(@high - 1)
77
+ end
78
+
79
+ def higher_than_high?
80
+ @high.nil? || @high == Float::INFINITY || !passes_validation_with_size(@high + 1)
81
+ end
82
+
83
+ def passes_validation_with_size(new_size)
84
+ io = Tempfile.new('Hello world!')
85
+ Matchers.stub_method(io, :size, new_size) do
86
+ @subject.public_send(@attribute_name).attach(io: io, filename: 'test.png', content_type: 'image/pg')
87
+ @subject.validate
88
+ @subject.errors.details[@attribute_name].all? { |error| error[:error] != :file_size_out_of_range }
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -19,11 +19,22 @@ module ActiveStorageValidations
19
19
  private
20
20
 
21
21
  def read_image
22
- if file.is_a?(String)
23
- blob = ActiveStorage::Blob.find_signed(file)
22
+ is_string = file.is_a?(String)
23
+ if is_string || file.is_a?(ActiveStorage::Blob)
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
24
35
 
25
36
  tempfile = Tempfile.new(["ActiveStorage-#{blob.id}-", blob.filename.extension_with_delimiter])
26
- tempfile.binmode
37
+ tempfile.binmode
27
38
 
28
39
  blob.download do |chunk|
29
40
  tempfile.write(chunk)
@@ -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.8.2'
4
+ VERSION = '0.9.4'
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.8.2
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-07 00:00:00.000000000 Z
11
+ date: 2021-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -25,35 +25,35 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 5.2.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: mini_magick
28
+ name: combustion
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 4.9.5
33
+ version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 4.9.5
40
+ version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
- name: pry
42
+ name: mini_magick
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 4.9.5
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 4.9.5
55
55
  - !ruby/object:Gem::Dependency
56
- name: rubocop
56
+ name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: sqlite3
70
+ name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: coffee-rails
84
+ name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -106,6 +106,17 @@ files:
106
106
  - Rakefile
107
107
  - config/locales/de.yml
108
108
  - config/locales/en.yml
109
+ - config/locales/es.yml
110
+ - config/locales/fr.yml
111
+ - config/locales/it.yml
112
+ - config/locales/ja.yml
113
+ - config/locales/nl.yml
114
+ - config/locales/pl.yml
115
+ - config/locales/pt-BR.yml
116
+ - config/locales/ru.yml
117
+ - config/locales/tr.yml
118
+ - config/locales/uk.yml
119
+ - config/locales/vi.yml
109
120
  - lib/active_storage_validations.rb
110
121
  - lib/active_storage_validations/aspect_ratio_validator.rb
111
122
  - lib/active_storage_validations/attached_validator.rb
@@ -113,6 +124,11 @@ files:
113
124
  - lib/active_storage_validations/dimension_validator.rb
114
125
  - lib/active_storage_validations/engine.rb
115
126
  - lib/active_storage_validations/limit_validator.rb
127
+ - lib/active_storage_validations/matchers.rb
128
+ - lib/active_storage_validations/matchers/attached_validator_matcher.rb
129
+ - lib/active_storage_validations/matchers/content_type_validator_matcher.rb
130
+ - lib/active_storage_validations/matchers/dimension_validator_matcher.rb
131
+ - lib/active_storage_validations/matchers/size_validator_matcher.rb
116
132
  - lib/active_storage_validations/metadata.rb
117
133
  - lib/active_storage_validations/railtie.rb
118
134
  - lib/active_storage_validations/size_validator.rb
@@ -137,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
153
  - !ruby/object:Gem::Version
138
154
  version: '0'
139
155
  requirements: []
140
- rubygems_version: 3.0.2
156
+ rubygems_version: 3.0.3
141
157
  signing_key:
142
158
  specification_version: 4
143
159
  summary: Validations for Active Storage