paperclip-dimension-validator-a-it 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7054bdf8723e38fdb0dea1a88b8dd7917af16240
4
+ data.tar.gz: 1254d75af6d1126d94bf53f3d2ae2114211e589b
5
+ SHA512:
6
+ metadata.gz: f69e4a0363b222a62e5e38816ad33990c8c28d3db71c64833abac276e3d5feadc33782db07dd3170151336d32fc14fd90383ec9fb35b91433968a6f8a0b8ebbc
7
+ data.tar.gz: 032bdaba4691e923d9ccb8599ae415cf267cf70a9a9b95ff5d536971b517272ca6683359ef5df7d41aad4ad3f416fc435aff11263e04128160b0111ff0ca57a5
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rbenv*
19
+ .rvmrc
20
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip-dimension-validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Anthony Smith
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # paperclip-dimension-validator
2
+
3
+ Validate them dimensions!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'paperclip-dimension-validator-a-it'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install paperclip-dimension-validator-a-it
18
+
19
+ ## Usage
20
+
21
+ This gem introduces the ```dimensions``` validator for Paperclip's
22
+ ```validates_attachment```. ```dimensions``` accepts a hash of ```height``` and
23
+ ``` width``` integer pixel values.
24
+
25
+ **Example**
26
+ ```ruby
27
+ class Image < ActiveRecord::Base
28
+ has_attached_file :avatar
29
+
30
+ validates_attachment :avatar, dimensions: { height: 30, width: 30 }
31
+ end
32
+ ```
33
+
34
+ ## Testing
35
+
36
+ paperclip-dimension-validator includes rspec-compatible matchers for testing.
37
+
38
+ **Note** In order to use these matchers make sure to include either [chunky_png](https://github.com/wvanbergen/chunky_png) or
39
+ [oily_png](https://github.com/wvanbergen/oily_png) as a dependency in your Gemfile.
40
+
41
+ **Gemfile**
42
+
43
+ ```ruby
44
+ group :test do
45
+ gem 'chunky_png'
46
+ end
47
+ ```
48
+
49
+ **RSpec**
50
+
51
+ In spec_helper.rb, you'll need to require the matchers:
52
+
53
+ ```ruby
54
+ require 'paperclip/matchers/validate_attachment_dimesions_matcher'
55
+ ```
56
+
57
+ And include the paperclip matchers module:
58
+
59
+ ```ruby
60
+ RSpec.configure do |config|
61
+ config.include Paperclip::Shoulda::Matchers
62
+ end
63
+ ```
64
+
65
+ **Example**
66
+
67
+ ```ruby
68
+ it { should validate_attachment_dimensions(:avatar).height(30).width(30) }
69
+ ```
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ dimension: "must have a %{dimension_type} of %{dimension}px was %{actual_dimension}px"
@@ -0,0 +1,6 @@
1
+ require 'active_model'
2
+ require 'paperclip/validators'
3
+ require 'paperclip/validators/attachment_dimensions_validator'
4
+
5
+ locale_path = Dir.glob(File.dirname(__FILE__) + "/locales/*.{rb,yml}")
6
+ I18n.load_path += locale_path unless I18n.load_path.include?(locale_path)
@@ -0,0 +1,92 @@
1
+ require 'tempfile'
2
+
3
+ module Paperclip
4
+ module Shoulda
5
+ module Matchers
6
+ def validate_attachment_dimensions(name)
7
+ ValidateAttachmentDimensionsMatcher.new(name)
8
+ end
9
+
10
+ class ValidateAttachmentDimensionsMatcher
11
+ def initialize(attachment_name)
12
+ unless defined?(ChunkyPNG)
13
+ puts 'WARNING: Add chunky_png or oily_png to your Gemfile to use paperclip dimension matchers'
14
+ end
15
+
16
+ @attachment_name = attachment_name
17
+ end
18
+
19
+ def height(height)
20
+ @height = height
21
+ self
22
+ end
23
+
24
+ def width(width)
25
+ @width = width
26
+ self
27
+ end
28
+
29
+ def matches?(subject)
30
+ @subject = subject
31
+ @subject = @subject.new if @subject.class == Class
32
+ shorter_than_height? && taller_than_height? && smaller_than_width? && larger_than_width?
33
+ end
34
+
35
+ def failure_message
36
+ message = "Attachment #{@attachment_name} must have"
37
+ message += " a height of #{@height}px" if @height
38
+ message += " and" if @height && @width
39
+ message += " a width of #{@width}px" if @width
40
+ end
41
+
42
+ def failure_message_when_negated
43
+ message = "Attachment #{@attachment_name} cannot have"
44
+ message += " a height of #{@height}px" if @height
45
+ message += " and" if @height && @width
46
+ message += " a width of #{@width}px" if @width
47
+ end
48
+ alias negative_failure_message failure_message_when_negated
49
+
50
+ def description
51
+ "validate width and height in pixels for attachment #{@attachment_name}"
52
+ end
53
+
54
+ protected
55
+ def validation_with_height(height)
56
+ @subject.send(@attachment_name).assign(generate_png(height, @width))
57
+ @subject.valid?
58
+ @subject.errors[@attachment_name].include?("must have a height of #{@height}px was #{height}px")
59
+ end
60
+
61
+ def validation_with_width(width)
62
+ @subject.send(@attachment_name).assign(generate_png(@height, width))
63
+ @subject.valid?
64
+ @subject.errors[@attachment_name].include?("must have a width of #{@width}px was #{width}px")
65
+ end
66
+
67
+ def shorter_than_height?
68
+ @height.nil? || validation_with_height(@height - 1)
69
+ end
70
+
71
+ def taller_than_height?
72
+ @height.nil? || validation_with_height(@height + 1)
73
+ end
74
+
75
+ def smaller_than_width?
76
+ @width.nil? || validation_with_width(@width - 1)
77
+ end
78
+
79
+ def larger_than_width?
80
+ @width.nil? || validation_with_width(@width + 1)
81
+ end
82
+
83
+ def generate_png(height, width)
84
+ file = Tempfile.new([Time.now.to_i.to_s, '.png'])
85
+ file.binmode
86
+ ChunkyPNG::Image.new(width || 1, height || 1, ChunkyPNG::Color('black')).save(file.path)
87
+ file
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,37 @@
1
+ module Paperclip
2
+ module Validators
3
+ class AttachmentDimensionsValidator < ActiveModel::EachValidator
4
+ def initialize(options)
5
+ super
6
+ end
7
+
8
+ def self.helper_method_name
9
+ :validates_attachment_dimensions
10
+ end
11
+
12
+ def validate_each(record, attribute, value)
13
+ return unless value.queued_for_write[:original]
14
+
15
+ begin
16
+ dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
17
+
18
+ [:height, :width].each do |dimension|
19
+ if options[dimension] && dimensions.send(dimension) != options[dimension].to_f
20
+ record.errors.add(attribute.to_sym, :dimension, dimension_type: dimension.to_s, dimension: options[dimension], actual_dimension: dimensions.send(dimension).to_i)
21
+ end
22
+ end
23
+ rescue Paperclip::Errors::NotIdentifiedByImageMagickError
24
+ Paperclip.log("cannot validate dimensions on #{attribute}")
25
+ end
26
+ end
27
+ end
28
+
29
+ module HelperMethods
30
+ def validates_attachment_dimensions(*attr_names)
31
+ options = _merge_attributes(attr_names)
32
+ validates_with(AttachmentDimensionsValidator, options.dup)
33
+ validate_before_processing(AttachmentDimensionsValidator, options.dup)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "paperclip-dimension-validator-a-it"
7
+ spec.version = '0.0.2'
8
+ spec.authors = ["xuzh"]
9
+ spec.email = ["weilaixiang@126.com"]
10
+ spec.description = %q{Validate image height and width for Paperclip}
11
+ spec.summary = %q{Validate them dimensions!}
12
+ spec.homepage = "https://github.com/xuzh86/paperclip-dimension-validator"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'paperclip','>= 3'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake", '~> 0'
24
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-dimension-validator-a-it
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - xuzh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: paperclip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Validate image height and width for Paperclip
56
+ email:
57
+ - weilaixiang@126.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/locales/en.yml
68
+ - lib/paperclip-dimension-validator.rb
69
+ - lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb
70
+ - lib/paperclip/validators/attachment_dimensions_validator.rb
71
+ - paperclip-dimension-validator.gemspec
72
+ homepage: https://github.com/xuzh86/paperclip-dimension-validator
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.6.14
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Validate them dimensions!
96
+ test_files: []