carrierwave_image_validate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6d4e6687cb57405b03e4502457665c3def7191da03bdbe108f8c0d8741d1ca3d
4
+ data.tar.gz: cd6220b2222b442b73e37b48d2bd9a439d655653bd61b19da610645a172f116a
5
+ SHA512:
6
+ metadata.gz: 5a323f1dd395a81a1755ba045f620c9f72e26ce91af7a25b2f523868dcb66391a31a3bcae46b8ef24ad475d580df09fe588e5fbab77050119659b76f73bf5764
7
+ data.tar.gz: d908b20c18110ea857c176fcd86e235e923150e979e58ee5809137ded7fb475222e6bb6da14cd320e7215135c1a363408139e588ec5bd743ae2de3147131a2a9
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Carrierwave Image Validate
2
+
3
+ [![Gem Version](http://img.shields.io/gem/v/gems.svg)][gem]
4
+ [![Build Status](https://github.com/rubygems/gems/workflows/ubuntu/badge.svg)][gh-actions]
5
+ [![Code Climate](https://api.codeclimate.com/v1/badges/45ff982a29d7a000ee84/maintainability)][codeclimate]
6
+
7
+ [gem]: https://rubygems.org/gems/gems
8
+ [gh-actions]: https://github.com/rubygems/gems/actions
9
+ [codeclimate]: https://codeclimate.com/github/rubygems/gems/maintainability
10
+
11
+ If you are using carrierwave gem to upload file and you want to add ratio validations for image
12
+ In the future, we will add more validations
13
+
14
+
15
+ ## What it can do
16
+
17
+ - Validate ratio image by range
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class User < ApplicationRecord
23
+ mount_uploader :avatar, AvatarUploader
24
+ validates :avatar, image: { ratio_range: 1..1 }
25
+ end
26
+ ```
27
+
28
+ - avatar is your field def
29
+ - ratio_range is a option of validate.
30
+
31
+ ## Internationalization
32
+
33
+ Carrierwave Image Validate uses I18n for error messages
34
+ For this, add these keys in your translation file:
35
+
36
+ ```yml
37
+ en:
38
+ errors:
39
+ messages:
40
+ aspect_ratio_is_not: "must have an aspect ratio of %{aspect_ratio}"
41
+ aspect_ratio_unknown: "has an unknown aspect ratio"
42
+ ```
43
+
44
+ ## Installation
45
+
46
+ Add this line to your application's Gemfile:
47
+
48
+ ```ruby
49
+ // require to handle image
50
+ gem 'mini_magick', '>= 4.9.5'
51
+ // validate
52
+ gem 'carrierwave_image_validate'
53
+ ```
54
+
55
+ And then execute
56
+
57
+ ```sh
58
+ $ bundle
59
+ ```
60
+
61
+ ## License
62
+ Released under the MIT License. See the [LICENSE](https://github.com/rails/thor/blob/main/LICENSE.md) file for further details.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,79 @@
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
+ RatioValidatorMatcher.new(name)
9
+ end
10
+
11
+ class RatioValidatorMatcher
12
+ def initialize(attribute_name)
13
+ @attribute_name = attribute_name
14
+ @ratio_range = nil
15
+ end
16
+
17
+ def description
18
+ "validate image ratio of #{@attribute_name}"
19
+ end
20
+
21
+ def is_ratio_in range_ratio
22
+ @range_ratio = range_ratio
23
+ self
24
+ end
25
+
26
+ def matches?(subject)
27
+ @subject = subject.is_a?(Class) ? subject.new : subject
28
+ responds_to_methods && valid_ratio?
29
+ end
30
+
31
+ def failure_message
32
+ "is expected to validate file size of #{@attribute_name} to be between #{@low} and #{@high} bytes"
33
+ end
34
+
35
+ def failure_message_when_negated
36
+ "is expected to not validate file size of #{@attribute_name} to be between #{@low} and #{@high} bytes"
37
+ end
38
+
39
+ protected
40
+
41
+ def responds_to_methods
42
+ @subject.respond_to?(@attribute_name) &&
43
+ @subject.public_send(@attribute_name).respond_to?(:attach) &&
44
+ @subject.public_send(@attribute_name).respond_to?(:detach)
45
+ end
46
+
47
+ def valid_ratio?
48
+ @range_ratio.nil? || valid_with_ratio
49
+ end
50
+
51
+ def valid_with_ratio
52
+ end
53
+ # def lower_than_low?
54
+ # @low.nil? || !passes_validation_with_size(@low - 1)
55
+ # end
56
+
57
+ # def higher_than_low?
58
+ # @low.nil? || passes_validation_with_size(@low + 1)
59
+ # end
60
+
61
+ # def lower_than_high?
62
+ # @high.nil? || @high == Float::INFINITY || passes_validation_with_size(@high - 1)
63
+ # end
64
+
65
+ # def higher_than_high?
66
+ # @high.nil? || @high == Float::INFINITY || !passes_validation_with_size(@high + 1)
67
+ # end
68
+
69
+ def passes_validation_with_size(new_size)
70
+ io = Tempfile.new('Hello world!')
71
+ Matchers.stub_method(io, :size, new_size) do
72
+ @subject.public_send(@attribute_name).attach(io: io, filename: 'test.png', content_type: 'image/pg')
73
+ @subject.validate
74
+ @subject.errors.details[@attribute_name].all? { |error| error[:error] != :file_size_out_of_range }
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_model'
2
+ require 'active_support/i18n'
3
+ I18n.load_path += Dir[File.dirname(__FILE__) + "/locale/*.yml"]
4
+ I18n.default_locale = :en
5
+
6
+ module ActiveModel
7
+ module Validations
8
+ class ImageValidator < ActiveModel::EachValidator
9
+ def validate_each(record, attribute, value)
10
+ @record = record
11
+ @attribute = attribute
12
+ @value = value
13
+ @options = options
14
+
15
+ ratio_valid? if options[:ratio_range]
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :record, :attribute, :value, :options
21
+
22
+ def ratio_valid?
23
+ return if value.blank?
24
+
25
+ if ratio_range = options[:ratio_range]
26
+ x = ratio_range.first
27
+ y = ratio_range.last
28
+
29
+ width = value.width
30
+ height = value.height
31
+ ratio = width.to_f / height
32
+
33
+ return if ratio_range.member?(ratio)
34
+
35
+ add_error(record, attribute, :aspect_ratio_is_not, "#{x}x#{y}")
36
+ else
37
+ add_error(record, attribute, :aspect_ratio_unknown)
38
+ end
39
+ end
40
+
41
+
42
+ def add_error(record, attribute, type, interpolate = options[:with])
43
+ key = options[:message].presence || type
44
+ return if record.errors.added?(attribute, key)
45
+
46
+ record.errors.add(attribute, key, aspect_ratio: interpolate)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ aspect_ratio_is_not: "must have an aspect ratio of %{aspect_ratio}"
5
+ aspect_ratio_unknown: "has an unknown aspect ratio"
@@ -0,0 +1,5 @@
1
+ ja:
2
+ errors:
3
+ messages:
4
+ aspect_ratio_is_not: "のアスペクト比は %{aspect_ratio} にしてください"
5
+ aspect_ratio_unknown: "のアスペクト比を取得できませんでした"
@@ -0,0 +1,5 @@
1
+ vi:
2
+ errors:
3
+ messages:
4
+ aspect_ratio_is_not: "phải có tỉ lệ ảnh %{aspect_ratio}"
5
+ aspect_ratio_unknown: "tỉ lệ ảnh không xác định"
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+ require 'lupca/carrierwave_image_validation'
3
+
4
+ class User::Test < ActiveSupport::TestCase
5
+ test 'aspect ratio valid' do
6
+ e = User.new
7
+ e.avatar = fixture_file_upload("image_150x150.png")
8
+ assert_equal e.valid?, true
9
+ end
10
+
11
+ test 'aspect ratio invalid width 600 x 800' do
12
+ e = User.new
13
+ e.avatar = fixture_file_upload('image_600x800.png')
14
+ assert !e.valid?
15
+ assert_equal e.errors.full_messages, ["Avatar must have an aspect ratio of 1x1"]
16
+ end
17
+
18
+ test 'aspect ratio invalid width 800 x 600' do
19
+ u = User.new
20
+ u.avatar = fixture_file_upload('image_800x600.png')
21
+ assert !u.valid?
22
+ assert_equal u.errors.full_messages, ["Avatar must have an aspect ratio of 1x1"]
23
+ end
24
+
25
+ # test 'aspect ratio invalid: not is image' do
26
+ # e = User.new
27
+ # e.avatar = fixture_file_upload('test.html')
28
+ # assert !e.valid?
29
+ # assert_equal e.errors.full_messages, ["Avatar is not a valid image"]
30
+ # end
31
+ end
32
+
33
+ def image_150x150_file
34
+ { io: File.open(Rails.root.join('public', 'image_150x150.png')), filename: 'image_150x150_file.png', content_type: 'image/png' }
35
+ end
36
+
37
+ def image_800x600_file
38
+ { io: File.open(Rails.root.join('public', 'image_800x600.png')), filename: 'image_800x600_file.png', content_type: 'image/png' }
39
+ end
40
+
41
+ def image_600x800_file
42
+ { io: File.open(Rails.root.join('public', 'image_600x800.png')), filename: 'image_600x800_file.png', content_type: 'image/png' }
43
+ end
44
+
45
+ def html_file
46
+ { io: File.open(Rails.root.join('public', 'test.html')), filename: 'test.html', content_type: 'text/html' }
47
+ end
metadata ADDED
@@ -0,0 +1,219 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave_image_validate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dang Thanh Tung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 7.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 7.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 7.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 7.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: combustion
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mini_magick
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 4.9.5
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 4.9.5
83
+ - !ruby/object:Gem::Dependency
84
+ name: ruby-vips
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.1.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.1.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sqlite3
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: marcel
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: carrierwave
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rmagick
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'
181
+ description: A validate image ratio gem for carrierwave
182
+ email: dangthanhtung.open@gmail
183
+ executables: []
184
+ extensions: []
185
+ extra_rdoc_files: []
186
+ files:
187
+ - README.md
188
+ - Rakefile
189
+ - lib/lupca/carrierwave_image_ratio_validation/matchers/#ratio_validator_matcher.rb
190
+ - lib/lupca/carrierwave_image_validation.rb
191
+ - lib/lupca/locale/en.yml
192
+ - lib/lupca/locale/ja.yml
193
+ - lib/lupca/locale/vi.yml
194
+ - test/test_carrierwave_image_ratio_validation.rb
195
+ homepage: http://rubygems.org/gems/carrierwave_image_validate
196
+ licenses:
197
+ - MIT
198
+ metadata: {}
199
+ post_install_message:
200
+ rdoc_options: []
201
+ require_paths:
202
+ - lib
203
+ required_ruby_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ required_rubygems_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ requirements: []
214
+ rubygems_version: 3.2.3
215
+ signing_key:
216
+ specification_version: 4
217
+ summary: Image ratio validate!
218
+ test_files:
219
+ - test/test_carrierwave_image_ratio_validation.rb