This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
validators.rb
46 lines (40 loc) · 1.49 KB
/
validators.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'active_model'
require 'active_support/concern'
require 'paperclip/validators/attachment_content_type_validator'
require 'paperclip/validators/attachment_presence_validator'
require 'paperclip/validators/attachment_size_validator'
module Paperclip
module Validators
extend ActiveSupport::Concern
included do
extend HelperMethods
include HelperMethods
end
module ClassMethods
# This method is a shortcut to validator classes that is in
# "Attachment...Validator" format. It is almost the same thing as the
# +validates+ method that shipped with Rails, but this is customized to
# be using with attachment validators. This is helpful when you're using
# multiple attachment validators on a single attachment.
#
# Example of using the validator:
#
# validates_attachment :avatar, :presence => true,
# :content_type => { :content_type => "image/jpg" },
# :size => { :in => 0..10.kilobytes }
#
def validates_attachment(*attributes)
options = attributes.extract_options!.dup
Paperclip::Validators.constants.each do |constant|
if constant.to_s =~ /^Attachment(.+)Validator$/
validator_kind = $1.underscore.to_sym
if options.has_key?(validator_kind)
options[:"attachment_#{validator_kind}"] = options.delete(validator_kind)
end
end
end
validates(*attributes + [options])
end
end
end
end