carrierwave-validators 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +26 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +6 -0
- data/carrierwave-validators.gemspec +29 -0
- data/lib/carrierwave/validators.rb +17 -0
- data/lib/carrierwave/validators/all.rb +9 -0
- data/lib/carrierwave/validators/error.rb +8 -0
- data/lib/carrierwave/validators/locales/en.yml +13 -0
- data/lib/carrierwave/validators/railtie.rb +14 -0
- data/lib/carrierwave/validators/size_validator.rb +98 -0
- data/lib/carrierwave/validators/version.rb +5 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/Gemfile +15 -0
- data/spec/dummy/README.md +5 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +17 -0
- data/spec/dummy/app/assets/javascripts/profiles.js.coffee +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/assets/stylesheets/profiles.css.scss +0 -0
- data/spec/dummy/app/assets/stylesheets/scaffolds.css.scss +69 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/profiles_controller.rb +73 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/profiles_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/profile.rb +9 -0
- data/spec/dummy/app/uploaders/avatar_uploader.rb +58 -0
- data/spec/dummy/app/views/layouts/application.html.erb +12 -0
- data/spec/dummy/app/views/profiles/_form.html.erb +24 -0
- data/spec/dummy/app/views/profiles/edit.html.erb +6 -0
- data/spec/dummy/app/views/profiles/index.html.erb +29 -0
- data/spec/dummy/app/views/profiles/index.json.jbuilder +4 -0
- data/spec/dummy/app/views/profiles/new.html.erb +5 -0
- data/spec/dummy/app/views/profiles/show.html.erb +14 -0
- data/spec/dummy/app/views/profiles/show.json.jbuilder +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +28 -0
- data/spec/dummy/config/boot.rb +4 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +60 -0
- data/spec/dummy/db/migrate/20140330144709_create_profiles.rb +10 -0
- data/spec/dummy/db/schema.rb +23 -0
- data/spec/dummy/db/seeds.rb +7 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/lib/tasks/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/robots.txt +5 -0
- data/spec/dummy/spec/support/avatar.jpg +0 -0
- data/spec/dummy/spec/support/large.jpg +0 -0
- data/spec/dummy/spec/support/small.png +0 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/validators/size_validators/size_validator_spec.rb +165 -0
- metadata +292 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d347172e4e6f3a5ede040d51c5a90b126d1fa2ce
|
4
|
+
data.tar.gz: 36f835afa6820a19ca127ad15012dbc8fa188acb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5a5debf8b3d40c51c4adf2fe8ec3dacb37dac7eeb09a4a86dad85cfa73e6d5dd18f60b572585c91d9ea119174252754e4ef8732bbd63fc9bc73ec803c1108e3
|
7
|
+
data.tar.gz: 2d10dac72b8c6a35761ecbe10377300f641bac79d54cab248b764cf80ded5b6bb91a0d6c758bc7d93cefb38f6aa6ecbb3fc7cf808d1eb5700d2bc446201303cf
|
data/.gitignore
ADDED
@@ -0,0 +1,26 @@
|
|
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
|
+
.DS_Store
|
19
|
+
spec/dummy/.bundle
|
20
|
+
spec/dummy/db/*.sqlite3
|
21
|
+
spec/dummy/db/*.sqlite3-journal
|
22
|
+
spec/dummy/log/*.log
|
23
|
+
spec/dummy/tmp
|
24
|
+
spec/dummy/public/uploads
|
25
|
+
spec/dummy/Gemfile.lock
|
26
|
+
spec/dummy/public/assets
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 kirtithorat
|
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,98 @@
|
|
1
|
+
# CarrierWave-Validators
|
2
|
+
|
3
|
+
CarrierWave extension to validate uploaded files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the latest stable release:
|
8
|
+
|
9
|
+
$[sudo] gem install carrierwave-validators
|
10
|
+
|
11
|
+
In Rails, add it to your Gemfile:
|
12
|
+
|
13
|
+
gem 'carrierwave-validators'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Finally, restart the server to apply the changes.
|
20
|
+
|
21
|
+
## Getting Started
|
22
|
+
|
23
|
+
`Carrierwave-Validators` supports following validators to validate your attachment:
|
24
|
+
|
25
|
+
* `SizeValidator` - Validates the file size of uploaded file
|
26
|
+
|
27
|
+
## SizeValidator
|
28
|
+
|
29
|
+
SizeValidator validates the **file size** of files uploaded using `CarrierWave`.
|
30
|
+
|
31
|
+
Example Usage, in the model `User` with Carrierwave attachment `avatar`:
|
32
|
+
|
33
|
+
class User < ActiveRecord::Base
|
34
|
+
mount_uploader :avatar, AvatarUploader
|
35
|
+
validates :avatar, :size => { :in => 0..50.kilobytes } ## Validates if uploaded file is within 0 Bytes to 50 KiloBytes
|
36
|
+
end
|
37
|
+
|
38
|
+
Alternatively,
|
39
|
+
|
40
|
+
validates :avatar, :size => { :in => 0..50.kilobytes }
|
41
|
+
|
42
|
+
can be specified as
|
43
|
+
|
44
|
+
validates_file_size :avatar, :in => 0..50.kilobytes
|
45
|
+
|
46
|
+
Available options:
|
47
|
+
|
48
|
+
* *in* : in between a Range of bytes , for example
|
49
|
+
|
50
|
+
## uploaded file size is within 0 Bytes to 50 KiloBytes
|
51
|
+
validates :avatar, :size => { :in => 0..50.kilobytes }
|
52
|
+
|
53
|
+
* *less_than* : less than a number in bytes, for example
|
54
|
+
|
55
|
+
## uploaded file size is less than 5 MegaBytes
|
56
|
+
validates :avatar, :size => { :less_than => 5.megabytes }
|
57
|
+
|
58
|
+
* *greater_than* : greater than a number in bytes, for example
|
59
|
+
|
60
|
+
## uploaded file size is greater than 5 KiloBytes
|
61
|
+
validates :avatar, :size => { :greater_than => 5.kilobytes }
|
62
|
+
|
63
|
+
* *less_than_or_equal_to* : less than or equal to a number in bytes, for example
|
64
|
+
|
65
|
+
## uploaded file size is less than or equal to 1 MegaByte
|
66
|
+
validates :avatar, :size => { :less_than_or_equal_to => 1.megabytes }
|
67
|
+
|
68
|
+
* *greater_than_or_equal_to* : greater than or equal to a number in bytes, for example
|
69
|
+
|
70
|
+
## uploaded file size is greater than or equal to 5 KiloBytes
|
71
|
+
validates :avatar, :size => { :greater_than_or_equal_to => 5.kilobytes }
|
72
|
+
|
73
|
+
* *message* : error message to display, for example
|
74
|
+
|
75
|
+
validates :avatar, :size => { :in => 0..50.kilobytes, :message => "Your custom error message" }
|
76
|
+
|
77
|
+
#i18n
|
78
|
+
|
79
|
+
The Active Record validations use the Rails i18n framework. Include the following keys in your translation files:
|
80
|
+
|
81
|
+
en:
|
82
|
+
errors:
|
83
|
+
messages:
|
84
|
+
in: "file size must be in between %{min} %{min_unit} and %{max} %{max_unit}"
|
85
|
+
greater_than: "file size must be greater than %{count} %{unit}"
|
86
|
+
greater_than_or_equal_to: "file size must be greater than or equal to %{count} %{unit}"
|
87
|
+
less_than: "file size must be less than %{count} %{unit}"
|
88
|
+
less_than_or_equal_to: "file size must be less than or equal to %{count} %{unit}"
|
89
|
+
|
90
|
+
size:
|
91
|
+
units:
|
92
|
+
one: "Byte"
|
93
|
+
other: "Bytes"
|
94
|
+
|
95
|
+
## TODO
|
96
|
+
|
97
|
+
* Add content-type validation
|
98
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'carrierwave/validators/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "carrierwave-validators"
|
8
|
+
spec.version = CarrierWave::Validators::VERSION
|
9
|
+
spec.authors = ["Kirti Thorat"]
|
10
|
+
spec.email = ["kirti.brenz@gmail.com"]
|
11
|
+
spec.summary = %q{CarrierWave extension for validating uploaded files.}
|
12
|
+
spec.description = %q{CarrierWave extension to validate files uploaded using carrierwave.}
|
13
|
+
spec.homepage = "https://github.com/kirtithorat/carrierwave-validators"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "rails", ">= 3.2.0"
|
22
|
+
spec.add_dependency "carrierwave", [">= 0.8.0", "< 0.11.0"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.2.2"
|
26
|
+
spec.add_development_dependency "sqlite3", "~> 1.3.9"
|
27
|
+
spec.add_development_dependency "rspec", "~> 2.14.1"
|
28
|
+
spec.add_development_dependency "database_cleaner", "~> 1.2.0"
|
29
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'carrierwave/validators/all'
|
4
|
+
require 'active_support/concern'
|
5
|
+
|
6
|
+
module CarrierWave
|
7
|
+
module Validators
|
8
|
+
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
extend HelperMethods
|
13
|
+
include HelperMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
en:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
in: "file size must be in between %{min} %{min_unit} and %{max} %{max_unit}"
|
5
|
+
greater_than: "file size must be greater than %{count} %{unit}"
|
6
|
+
greater_than_or_equal_to: "file size must be greater than or equal to %{count} %{unit}"
|
7
|
+
less_than: "file size must be less than %{count} %{unit}"
|
8
|
+
less_than_or_equal_to: "file size must be less than or equal to %{count} %{unit}"
|
9
|
+
|
10
|
+
size:
|
11
|
+
units:
|
12
|
+
one: "Byte"
|
13
|
+
other: "Bytes"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer 'carrierwave.validators' do
|
6
|
+
ActiveSupport.on_load :active_record do
|
7
|
+
include Validators
|
8
|
+
end
|
9
|
+
|
10
|
+
locale_path = Dir.glob(File.dirname(__FILE__) + "/locales/*.{rb,yml}")
|
11
|
+
I18n.load_path += locale_path unless I18n.load_path.include?(locale_path)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'active_model/validations/numericality'
|
4
|
+
|
5
|
+
module CarrierWave
|
6
|
+
module Validators
|
7
|
+
|
8
|
+
class SizeValidator < ActiveModel::Validations::NumericalityValidator
|
9
|
+
AVAILABLE_CHECKS = [:less_than, :less_than_or_equal_to, :greater_than, :greater_than_or_equal_to].freeze
|
10
|
+
|
11
|
+
def initialize(options)
|
12
|
+
extract_options(options)
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate_each(record, attribute, value)
|
17
|
+
unless value.blank?
|
18
|
+
raise(CarrierWave::Validators::InvalidAttributeError, "A CarrierWave::Uploader::Base object was expected") unless value.kind_of? CarrierWave::Uploader::Base
|
19
|
+
value = value.respond_to?(:length) ? value.length : value.to_s.length
|
20
|
+
|
21
|
+
options.slice(*AVAILABLE_CHECKS).each do |option, option_value|
|
22
|
+
unless value.send(CHECKS[option], option_value)
|
23
|
+
error_message_key = options[:in] ? :in : option
|
24
|
+
record.errors.add(attribute, error_message_key, filtered_options(value).merge!(detect_error_options(option, option_value)))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def check_validity!
|
31
|
+
unless (AVAILABLE_CHECKS + [:in]).any? { |argument| options.has_key?(argument) }
|
32
|
+
raise CarrierWave::Validators::InvalidOptionError, "List of allowed options - [:in, :less_than, :greater_than, :less_than_or_equal_to, :greater_than_or_equal_to]"
|
33
|
+
end
|
34
|
+
|
35
|
+
options.slice(*AVAILABLE_CHECKS).each do |option, value|
|
36
|
+
isNumeric = value.is_a?(Numeric)
|
37
|
+
|
38
|
+
unless isNumeric && value >= 0
|
39
|
+
error_message = options[:in] ? ":in value must be in a non-negative Range" : ":#{option} value must be a non-negative Number"
|
40
|
+
raise CarrierWave::Validators::InvalidOptionError, error_message
|
41
|
+
end
|
42
|
+
|
43
|
+
if isNumeric && value == 0 && ([:less_than, :less_than_or_equal_to].include? option)
|
44
|
+
error_message = options[:in] ? "Invalid value for :in option. Lower bound must be greater than 0." : "Invalid value for :#{option} option. It must be greater than 0."
|
45
|
+
raise CarrierWave::Validators::InvalidOptionError, error_message
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def extract_options(options)
|
54
|
+
if range = options[:in]
|
55
|
+
raise CarrierWave::Validators::InvalidOptionError, ":in must be a Range" unless range.is_a?(Range)
|
56
|
+
clear_options(options)
|
57
|
+
options[:less_than_or_equal_to], options[:greater_than_or_equal_to] = range.max, range.min
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def clear_options(options)
|
62
|
+
AVAILABLE_CHECKS.each do |option|
|
63
|
+
options.delete(option)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def detect_error_options(option, option_value)
|
68
|
+
if options[:in]
|
69
|
+
max = options[:less_than_or_equal_to]
|
70
|
+
min = options[:greater_than_or_equal_to]
|
71
|
+
error_options = { min: min, max: max, min_unit: detect_unit(min), max_unit: detect_unit(max) }
|
72
|
+
else
|
73
|
+
error_options = { count: option_value, unit: detect_unit(option_value) }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def detect_unit(option_value)
|
78
|
+
I18n.translate(:"size.units", count: option_value, :raise => true )
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
module HelperMethods
|
84
|
+
# Places ActiveModel validations on the size of the file assigned. The
|
85
|
+
# possible options are:
|
86
|
+
# * +in+: in between a Range of bytes
|
87
|
+
# * +less_than+: less than a number in bytes
|
88
|
+
# * +greater_than+: greater than a number in bytes
|
89
|
+
# * +less_than_or_equal_to+: less than or equal to a number in bytes
|
90
|
+
# * +greater_than_or_equal_to+: greater than or equal to a number in bytes
|
91
|
+
# * +message+: error message to display, use :min and :max as replacements
|
92
|
+
def validates_file_size(*attr_names)
|
93
|
+
validates_with SizeValidator, _merge_attributes(attr_names)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
data/spec/dummy/.rspec
ADDED
data/spec/dummy/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '4.0.2'
|
4
|
+
gem 'sass-rails', '~> 4.0.0'
|
5
|
+
gem 'uglifier', '>= 1.3.0'
|
6
|
+
gem 'coffee-rails', '~> 4.0.0'
|
7
|
+
gem 'jquery-rails'
|
8
|
+
gem 'turbolinks'
|
9
|
+
gem 'jbuilder', '~> 1.2'
|
10
|
+
gem 'carrierwave'
|
11
|
+
gem 'carrierwave-validators', path: "../..", :require => 'carrierwave/validators'
|
12
|
+
group :development, :test do
|
13
|
+
gem 'sqlite3', "~> 1.3.9"
|
14
|
+
gem 'rspec-rails', '~> 2.14.2'
|
15
|
+
end
|
data/spec/dummy/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require jquery
|
14
|
+
//= require jquery_ujs
|
15
|
+
//= require turbolinks
|
16
|
+
//= require_tree .
|
17
|
+
|