active_storage_validations 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 267c083e1e37ff53853d928bf3df0a69b8c9cf0662c3cf744f99cde3d5809125
4
- data.tar.gz: 36554892e5c2df7ad812119b6acfae985e4adbe1dd8f7c29699f8f9ba69f0506
3
+ metadata.gz: fe9e0494c1bf81d0f0a00c649abce9de1ff9c0dc8782a6a39253bdf1a890361d
4
+ data.tar.gz: '0690ff0b6a259b35d074b8c81bad13ac4c699dbb1a3caca2b5b0b2b3d8b143ce'
5
5
  SHA512:
6
- metadata.gz: 6cbe5d7521afc8680a9df584968c4e3da7c5e1602efc30f60dc6890a54bb9d609daf7e7eaa0924d6d7971f13e9c763e4063c228d14645b8c5261f1702c4a21c9
7
- data.tar.gz: 989e058c095e07790f647af2582355dddd715101b6dce10d4b521865362c5857f8df869270ae88e5eb58d20fa698ed470f4b181517ac016439d45f0e30049e21
6
+ metadata.gz: 040ea6fc5660f163ba936b0b384eb515bad557f8d489bc75560e745b03011e97bc699c13e220f48f2e617b72abb70163f86cb7bdeb68522da48acb3508642679
7
+ data.tar.gz: e014d61d14631626179784ce0f7e31a65264103264d0b39666c720e135e6b34b68a64f374157174a0f974644cfdb8b22deeeee365c2d29d6ccb7394e290edab0
data/README.md CHANGED
@@ -6,6 +6,7 @@
6
6
  [![MiniTest](https://github.com/igorkasyanchuk/active_storage_validations/workflows/MiniTest/badge.svg)](https://github.com/igorkasyanchuk/active_storage_validations/actions)
7
7
  [![RailsJazz](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/my_other.svg?raw=true)](https://www.railsjazz.com)
8
8
  [![https://www.patreon.com/igorkasyanchuk](https://github.com/igorkasyanchuk/rails_time_travel/blob/main/docs/patron.svg?raw=true)](https://www.patreon.com/igorkasyanchuk)
9
+ [![Listed on OpenSource-Heroes.com](https://opensource-heroes.com/badge-v1.svg)](https://opensource-heroes.com/r/igorkasyanchuk/active_storage_validations)
9
10
 
10
11
  If you are using `active_storage` gem and you want to add simple validations for it, like presence or content_type you need to write a custom validation method.
11
12
 
@@ -394,12 +395,14 @@ You are welcome to contribute.
394
395
  - https://github.com/NARKOZ
395
396
  - https://github.com/stephensolis
396
397
  - https://github.com/kwent
397
- = https://github.com/Animesh-Ghosh
398
- = https://github.com/gr8bit
399
- = https://github.com/codegeek319
400
- = https://github.com/clwy-cn
401
- = https://github.com/kukicola
402
- = https://github.com/sobrinho
398
+ - https://github.com/Animesh-Ghosh
399
+ - https://github.com/gr8bit
400
+ - https://github.com/codegeek319
401
+ - https://github.com/clwy-cn
402
+ - https://github.com/kukicola
403
+ - https://github.com/sobrinho
404
+ - https://github.com/iainbeeston
405
+ - https://github.com/marckohlbrugge
403
406
 
404
407
  ## License
405
408
 
@@ -12,8 +12,6 @@ module ActiveStorageValidations
12
12
  end
13
13
 
14
14
  def validate_each(record, attribute, _)
15
- return true unless record.send(attribute).attached?
16
-
17
15
  files = Array.wrap(record.send(attribute)).compact.uniq
18
16
  flat_options = unfold_procs(record, self.options, AVAILABLE_CHECKS)
19
17
  errors_options = { min: flat_options[:min], max: flat_options[:max] }
@@ -65,8 +65,8 @@ module ActiveStorageValidations
65
65
  def matches?(subject)
66
66
  @subject = subject.is_a?(Class) ? subject.new : subject
67
67
  responds_to_methods &&
68
- width_smaller_than_min? && width_larger_than_min? && width_smaller_than_max? && width_larger_than_max? && width_equals? &&
69
- height_smaller_than_min? && height_larger_than_min? && height_smaller_than_max? && height_larger_than_max? && height_equals?
68
+ width_not_smaller_than_min? && width_larger_than_min? && width_smaller_than_max? && width_not_larger_than_max? && width_equals? &&
69
+ height_not_smaller_than_min? && height_larger_than_min? && height_smaller_than_max? && height_not_larger_than_max? && height_equals?
70
70
  end
71
71
 
72
72
  def failure_message
@@ -93,7 +93,7 @@ module ActiveStorageValidations
93
93
  ((@height_min || 0) + (@height_max || 2000)) / 2
94
94
  end
95
95
 
96
- def width_smaller_than_min?
96
+ def width_not_smaller_than_min?
97
97
  @width_min.nil? || !passes_validation_with_dimensions(@width_min - 1, valid_height, 'width')
98
98
  end
99
99
 
@@ -105,7 +105,7 @@ module ActiveStorageValidations
105
105
  @width_max.nil? || @width_min == @width_max || passes_validation_with_dimensions(@width_max - 1, valid_height, 'width')
106
106
  end
107
107
 
108
- def width_larger_than_max?
108
+ def width_not_larger_than_max?
109
109
  @width_max.nil? || !passes_validation_with_dimensions(@width_max + 1, valid_height, 'width')
110
110
  end
111
111
 
@@ -113,7 +113,7 @@ module ActiveStorageValidations
113
113
  @width_min.nil? || @width_min != @width_max || passes_validation_with_dimensions(@width_min, valid_height, 'width')
114
114
  end
115
115
 
116
- def height_smaller_than_min?
116
+ def height_not_smaller_than_min?
117
117
  @height_min.nil? || !passes_validation_with_dimensions(valid_width, @height_min - 1, 'height')
118
118
  end
119
119
 
@@ -125,7 +125,7 @@ module ActiveStorageValidations
125
125
  @height_max.nil? || @height_min == @height_max || passes_validation_with_dimensions(valid_width, @height_max - 1, 'height')
126
126
  end
127
127
 
128
- def height_larger_than_max?
128
+ def height_not_larger_than_max?
129
129
  @height_max.nil? || !passes_validation_with_dimensions(valid_width, @height_max + 1, 'height')
130
130
  end
131
131
 
@@ -140,7 +140,11 @@ module ActiveStorageValidations
140
140
  Matchers.mock_metadata(attachment, width, height) do
141
141
  @subject.validate
142
142
  exclude_error_message = @custom_message || "dimension_#{check}"
143
- @subject.errors.details[@attribute_name].all? { |error| error[:error].to_s.exclude?(exclude_error_message) }
143
+ @subject.errors.details[@attribute_name].none? do |error|
144
+ error[:error].to_s.include?(exclude_error_message) ||
145
+ error[:error].to_s.include?("dimension_min") ||
146
+ error[:error].to_s.include?("dimension_max")
147
+ end
144
148
  end
145
149
  end
146
150
 
@@ -75,17 +75,10 @@ module ActiveStorageValidations
75
75
  tempfile.flush
76
76
  tempfile.rewind
77
77
 
78
- image = if image_processor == :vips && defined?(Vips) && Vips::get_suffixes.include?(File.extname(tempfile.path).downcase)
79
- Vips::Image.new_from_file(tempfile.path)
80
- elsif defined?(MiniMagick)
81
- MiniMagick::Image.new(tempfile.path)
82
- end
78
+ image = new_image_from_path(tempfile.path)
83
79
  else
84
- image = if image_processor == :vips && defined?(Vips) && Vips::get_suffixes.include?(File.extname(read_file_path).downcase)
85
- Vips::Image.new_from_file(read_file_path)
86
- elsif defined?(MiniMagick)
87
- MiniMagick::Image.new(read_file_path)
88
- end
80
+ file_path = read_file_path
81
+ image = new_image_from_path(file_path)
89
82
  end
90
83
 
91
84
 
@@ -101,6 +94,14 @@ module ActiveStorageValidations
101
94
  image = nil
102
95
  end
103
96
 
97
+ def new_image_from_path(path)
98
+ if image_processor == :vips && defined?(Vips) && (Vips::get_suffixes.include?(File.extname(path).downcase) || !Vips::respond_to?(:vips_foreign_get_suffixes))
99
+ Vips::Image.new_from_file(path)
100
+ elsif defined?(MiniMagick)
101
+ MiniMagick::Image.new(path)
102
+ end
103
+ end
104
+
104
105
  def valid_image?(image)
105
106
  return false unless image
106
107
 
@@ -125,7 +126,18 @@ module ActiveStorageValidations
125
126
  when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
126
127
  file.path
127
128
  when Hash
128
- File.open(file.fetch(:io)).path
129
+ io = file.fetch(:io)
130
+ if io.is_a?(StringIO)
131
+ tempfile = Tempfile.new([File.basename(file[:filename], '.*'), File.extname(file[:filename])])
132
+ tempfile.binmode
133
+ IO.copy_stream(io, tempfile)
134
+ io.rewind
135
+ tempfile.flush
136
+ tempfile.rewind
137
+ tempfile.path
138
+ else
139
+ File.open(io).path
140
+ end
129
141
  else
130
142
  raise "Something wrong with params."
131
143
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStorageValidations
4
- VERSION = '1.0.3'
4
+ VERSION = '1.0.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: 1.0.3
4
+ version: 1.0.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: 2022-10-28 00:00:00.000000000 Z
11
+ date: 2023-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: globalid
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -240,7 +254,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
240
254
  - !ruby/object:Gem::Version
241
255
  version: '0'
242
256
  requirements: []
243
- rubygems_version: 3.2.3
257
+ rubygems_version: 3.4.13
244
258
  signing_key:
245
259
  specification_version: 4
246
260
  summary: Validations for Active Storage