aspecta 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ .rvmrc
4
+ Gemfile.lock
5
+ pkg/*
6
+ uploads
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
@@ -0,0 +1,72 @@
1
+ # aspecta
2
+
3
+ ``aspect`` is small add-on for the awesome
4
+ [carrierwave](http://github.com/jnicklas/carrierwave) gem. ``aspecta``
5
+ adds image dimensions validator to your ActiveRecords models.
6
+
7
+ ## Usage
8
+
9
+ In your Gemfile:
10
+
11
+ gem "aspecta"
12
+
13
+ Run:
14
+
15
+ bundle install
16
+
17
+ Add validator to your model:
18
+
19
+ class User < ActiveRecord::Base
20
+ mount_uploader :avatar, AvatarUploader
21
+
22
+ validates :avatar, :dimensions => {
23
+ :height => { :minimum => 100, :maximum => 200 }
24
+ :width => { :minimum => 100, :maximum => 200 }
25
+ }
26
+ end
27
+
28
+ Profit!
29
+
30
+ ## I18n
31
+
32
+ Add following translations to your i18n setup:
33
+
34
+ en:
35
+ errors:
36
+ messages:
37
+ image_is_too_narrow: "image is too narrow, minimum is %{size} pixels"
38
+ image_is_too_wide: "image is too wide, maximum is %{size} pixels"
39
+ image_is_too_short: "image is too short, minimum is %{size} pixels"
40
+ image_is_too_tall: "image is too tall, maximum is %{size} pixels"
41
+
42
+ ## Contributing
43
+
44
+ Contributing is easy:
45
+
46
+ * fork & commit,
47
+ * submit issues.
48
+
49
+ Thank you!
50
+
51
+ ## License
52
+
53
+ Copyright (c) 2011 Filip Tepper
54
+
55
+ Permission is hereby granted, free of charge, to any person obtaining
56
+ a copy of this software and associated documentation files (the
57
+ "Software"), to deal in the Software without restriction, including
58
+ without limitation the rights to use, copy, modify, merge, publish,
59
+ distribute, sublicense, and/or sell copies of the Software, and to
60
+ permit persons to whom the Software is furnished to do so, subject to
61
+ the following conditions:
62
+
63
+ The above copyright notice and this permission notice shall be
64
+ included in all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
67
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
68
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
69
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
70
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
71
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
72
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList["test/*_test.rb"]
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => [:test]
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "aspecta/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "aspecta"
8
+ s.version = Aspecta::VERSION
9
+ s.authors = ["Filip Tepper"]
10
+ s.email = ["filip@tepper.pl"]
11
+ s.homepage = "http://github.com/filiptepper/aspecta"
12
+ s.summary = %q{image dimensions ActiveRecord validator for carrierwave gem}
13
+ s.description = %q{image dimensions ActiveRecord validator for carrierwave gem}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "activemodel"
21
+ s.add_development_dependency "carrierwave"
22
+ s.add_dependency "fastimage"
23
+ end
@@ -0,0 +1,49 @@
1
+ require "aspecta/version"
2
+ require "fastimage"
3
+
4
+ class DimensionsValidator < ActiveModel::EachValidator
5
+ DIMENSIONS = [:width, :height]
6
+ ATTRIBUTES = { :minimum => :>=, :maximum => :<= }
7
+ MESSAGES = {
8
+ :width => {
9
+ :minimum => :image_is_too_narrow,
10
+ :maximum => :image_is_too_wide
11
+ },
12
+ :height => {
13
+ :minimum => :image_is_too_short,
14
+ :maximum => :image_is_too_tall
15
+ }
16
+ }
17
+
18
+ def validate_each(record, attribute, value)
19
+ return if value.file.nil?
20
+
21
+ if options.empty?
22
+ raise ArgumentError, "Specify the :width and / or :height option"
23
+ end
24
+
25
+ DIMENSIONS.each do |dimension|
26
+ unless options[dimension].nil?
27
+ if (ATTRIBUTES.keys & options[dimension].keys).empty?
28
+ raise ArgumentError,
29
+ "Specify the :minimum and / or :maximum option for :#{dimension}"
30
+ end
31
+ end
32
+ end
33
+
34
+ dimensions = FastImage.size value.path
35
+
36
+ DIMENSIONS.each_with_index do |dimension, index|
37
+ next if options[dimension].nil?
38
+
39
+ options[dimension].each do |measure, size|
40
+ unless dimensions[index].send(ATTRIBUTES[measure], size)
41
+ record.errors.add(attribute,
42
+ MESSAGES[dimension][measure],
43
+ :size => size
44
+ )
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Aspecta
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,140 @@
1
+ # encoding: utf-8
2
+
3
+ require "test_helper"
4
+
5
+ class AspectaTest < Test::Unit::TestCase
6
+
7
+ # ===========
8
+ # = helpers =
9
+ # ===========
10
+
11
+ def fixture(file)
12
+ File.open(File.dirname(__FILE__) + "/fixtures/#{file}.png")
13
+ end
14
+
15
+ # ===========
16
+ # = general =
17
+ # ===========
18
+
19
+ def test_skips_empty_uploader
20
+ user = UserWithInvalidValidation.new
21
+
22
+ assert user.valid?
23
+ end
24
+
25
+ def test_requires_width_or_height_option
26
+ user = UserWithInvalidValidation.new
27
+ user.avatar = fixture "200x200"
28
+
29
+ exception = assert_raise ArgumentError do
30
+ user.valid?
31
+ end
32
+
33
+ assert_equal "Specify the :width and / or :height option",
34
+ exception.message
35
+ end
36
+
37
+ def test_requires_minimum_or_maximum_for_height_option
38
+ user = UserWithInvalidValitionHeightOptions.new
39
+ user.avatar = fixture "200x200"
40
+
41
+ exception = assert_raise ArgumentError do
42
+ user.valid?
43
+ end
44
+
45
+ assert_equal "Specify the :minimum and / or :maximum option for :height",
46
+ exception.message
47
+ end
48
+
49
+ def test_requires_minimum_or_maximum_for_width_option
50
+ user = UserWithInvalidValitionWidthOptions.new
51
+ user.avatar = fixture "200x200"
52
+
53
+ exception = assert_raise ArgumentError do
54
+ user.valid?
55
+ end
56
+
57
+ assert_equal "Specify the :minimum and / or :maximum option for :width",
58
+ exception.message
59
+ end
60
+
61
+ # =================
62
+ # = minimum width =
63
+ # =================
64
+
65
+ def test_valid_minimum_width
66
+ user = UserMinimumWidth200.new
67
+
68
+ user.avatar = fixture "200x200"
69
+ assert user.valid?
70
+ end
71
+
72
+ def test_invalid_minimum_width
73
+ user = UserMinimumWidth200.new
74
+
75
+ user.avatar = fixture "199x199"
76
+ assert ! user.valid?
77
+ assert user.errors[:avatar].
78
+ include?("image is too narrow, minimum is 200 pixels")
79
+ end
80
+
81
+ # ==================
82
+ # = minimum height =
83
+ # ==================
84
+
85
+ def test_valid_minimum_height
86
+ user = UserMinimumHeight200.new
87
+
88
+ user.avatar = fixture "200x200"
89
+ assert user.valid?
90
+ end
91
+
92
+ def test_invalid_minimum_height
93
+ user = UserMinimumHeight200.new
94
+
95
+ user.avatar = fixture "199x199"
96
+ assert ! user.valid?
97
+ assert user.errors[:avatar].
98
+ include?("image is too short, minimum is 200 pixels")
99
+ end
100
+
101
+ # =================
102
+ # = maximum width =
103
+ # =================
104
+
105
+ def test_valid_maximum_width
106
+ user = UserMaximumWidth200.new
107
+
108
+ user.avatar = fixture "200x200"
109
+ assert user.valid?
110
+ end
111
+
112
+ def test_invalid_maximum_width
113
+ user = UserMaximumWidth200.new
114
+
115
+ user.avatar = fixture "201x201"
116
+ assert ! user.valid?
117
+ assert user.errors[:avatar].
118
+ include?("image is too wide, maximum is 200 pixels")
119
+ end
120
+
121
+ # ==================
122
+ # = maximum height =
123
+ # ==================
124
+
125
+ def test_valid_maximum_height
126
+ user = UserMaximumHeight200.new
127
+
128
+ user.avatar = fixture "200x200"
129
+ assert user.valid?
130
+ end
131
+
132
+ def test_invalid_maximum_height
133
+ user = UserMaximumHeight200.new
134
+
135
+ user.avatar = fixture "201x201"
136
+ assert ! user.valid?
137
+ assert user.errors[:avatar].
138
+ include?("image is too tall, maximum is 200 pixels")
139
+ end
140
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1,4 @@
1
+ class AvatarUploader < CarrierWave::Uploader::Base
2
+ storage :file
3
+ root ""
4
+ end
@@ -0,0 +1,50 @@
1
+ require "lib/avatar_uploader"
2
+
3
+ class User
4
+ include ActiveModel::Validations
5
+ extend CarrierWave::Mount
6
+
7
+ mount_uploader :avatar, AvatarUploader
8
+ end
9
+
10
+ class UserWithInvalidValidation < User
11
+ validates :avatar, :dimensions => true
12
+ end
13
+
14
+ class UserWithInvalidValitionHeightOptions < User
15
+ validates :avatar, :dimensions => {
16
+ :height => {},
17
+ :width => { :minimum => 200, :maximum => 200 }
18
+ }
19
+ end
20
+
21
+ class UserWithInvalidValitionWidthOptions < User
22
+ validates :avatar, :dimensions => {
23
+ :width => {},
24
+ :height => { :minimum => 200, :maximum => 200 }
25
+ }
26
+ end
27
+
28
+ class UserMinimumWidth200 < User
29
+ validates :avatar, :dimensions => {
30
+ :width => { :minimum => 200 }
31
+ }
32
+ end
33
+
34
+ class UserMinimumHeight200 < User
35
+ validates :avatar, :dimensions => {
36
+ :height => { :minimum => 200 }
37
+ }
38
+ end
39
+
40
+ class UserMaximumWidth200 < User
41
+ validates :avatar, :dimensions => {
42
+ :width => { :maximum => 200 }
43
+ }
44
+ end
45
+
46
+ class UserMaximumHeight200 < User
47
+ validates :avatar, :dimensions => {
48
+ :height => { :maximum => 200 }
49
+ }
50
+ end
@@ -0,0 +1,7 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ image_is_too_narrow: "image is too narrow, minimum is %{size} pixels"
5
+ image_is_too_wide: "image is too wide, maximum is %{size} pixels"
6
+ image_is_too_short: "image is too short, minimum is %{size} pixels"
7
+ image_is_too_tall: "image is too tall, maximum is %{size} pixels"
@@ -0,0 +1,14 @@
1
+ $:.push File.dirname(__FILE__)
2
+
3
+ require "test/unit"
4
+
5
+ require "active_model"
6
+ require "carrierwave"
7
+ require "carrierwave/orm/activerecord"
8
+ require "i18n"
9
+
10
+ require "aspecta"
11
+
12
+ require "lib/user"
13
+
14
+ I18n.load_path << File.dirname(__FILE__) + "/locale/en.yml"
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aspecta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Filip Tepper
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &2243656820 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2243656820
25
+ - !ruby/object:Gem::Dependency
26
+ name: carrierwave
27
+ requirement: &2243656400 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2243656400
36
+ - !ruby/object:Gem::Dependency
37
+ name: fastimage
38
+ requirement: &2243655980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2243655980
47
+ description: image dimensions ActiveRecord validator for carrierwave gem
48
+ email:
49
+ - filip@tepper.pl
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - README.md
57
+ - Rakefile
58
+ - aspecta.gemspec
59
+ - lib/aspecta.rb
60
+ - lib/aspecta/version.rb
61
+ - test/aspect_test.rb
62
+ - test/fixtures/199x199.png
63
+ - test/fixtures/200x200.png
64
+ - test/fixtures/201x201.png
65
+ - test/lib/avatar_uploader.rb
66
+ - test/lib/user.rb
67
+ - test/locale/en.yml
68
+ - test/test_helper.rb
69
+ homepage: http://github.com/filiptepper/aspecta
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.6
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: image dimensions ActiveRecord validator for carrierwave gem
93
+ test_files:
94
+ - test/aspect_test.rb
95
+ - test/fixtures/199x199.png
96
+ - test/fixtures/200x200.png
97
+ - test/fixtures/201x201.png
98
+ - test/lib/avatar_uploader.rb
99
+ - test/lib/user.rb
100
+ - test/locale/en.yml
101
+ - test/test_helper.rb