paperclip-dimension-validator 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 906f02c9a9c4f39db7f1879384f3298a601a85f9
4
- data.tar.gz: e7e839d102a9fa317f581df4c535694b2a969924
3
+ metadata.gz: 3eadc6df06e33aa0236d1b139f63eafb0cb13391
4
+ data.tar.gz: d56a47f5c0ee1dc3d4118653577620f1a812cb04
5
5
  SHA512:
6
- metadata.gz: e8091b1070e2dc328a16af634ad5e27e7e3cff1bb3788768c11e4166b170b481389ed79ac34ee695c9575cbcea806b8b5ac80750fb3b0697b1e1f1fa5851a22f
7
- data.tar.gz: c41e2458e21aa273e0a9a9ffe8ae6b5eae0600a07ae6dc6a4c16e5f1bc8bbb789e838f34873696dc9e2e5801053bac4eb3c8a8a4eec5d8dce5d3558563e82746
6
+ metadata.gz: 3b230d0edef2aaa22c8d5f1711dee58a19e7ce5bcb538cf236931ad87c6bdb16b0e99b6a7eb5f2c0018695f6ffa0afc4a1e215feb65275edf785a23dc1a19e62
7
+ data.tar.gz: c9ba385711d79e7759f4052e22c0d1edbf5ef690fc4738a0bfbcb8451295dbdf41ec4caf46cded6ce4fc384eaca72f3fab83274eaf9ed0d1b60d0709be55f065
@@ -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
@@ -10,12 +10,18 @@ module Paperclip
10
10
  end
11
11
 
12
12
  def validate_each(record, attribute, value)
13
- dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
13
+ return unless value.queued_for_write[:original]
14
14
 
15
- [:height, :width].each do |dimension|
16
- if options[dimension] && dimensions.send(dimension) != options[dimension].to_f
17
- record.errors.add(attribute.to_sym, :dimension, dimension_type: dimension.to_s, dimension: options[dimension], actual_dimension: dimensions.send(dimension).to_i)
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
18
22
  end
23
+ rescue Paperclip::Errors::NotIdentifiedByImageMagickError
24
+ Paperclip.log("cannot validate dimensions on #{attribute}")
19
25
  end
20
26
  end
21
27
  end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "paperclip-dimension-validator"
7
- spec.version = '0.1.0'
7
+ spec.version = '0.1.1'
8
8
  spec.authors = ["Anthony Smith"]
9
9
  spec.email = ["anthony@sticksnleaves.com"]
10
10
  spec.description = %q{Validate image height and width for Paperclip}
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency 'activerecord', '>= 3.1'
21
20
  spec.add_dependency 'paperclip', '>= 4.0.0'
22
21
 
23
22
  spec.add_development_dependency "bundler", "~> 1.3"
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-dimension-validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-23 00:00:00.000000000 Z
11
+ date: 2014-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activerecord
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '3.1'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '3.1'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: paperclip
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +66,7 @@ files:
80
66
  - Rakefile
81
67
  - lib/locales/en.yml
82
68
  - lib/paperclip-dimension-validator.rb
69
+ - lib/paperclip/matchers/validate_attachment_dimensions_matcher.rb
83
70
  - lib/paperclip/validators/attachment_dimensions_validator.rb
84
71
  - paperclip-dimension-validator.gemspec
85
72
  homepage: https://github.com/anthonator/paperclip-dimension-validator