Module: UploadDocumentsTool::Validators::ClassMethods
- Defined in:
- lib/upload_documents_tool/validators.rb
Instance Method Summary collapse
- #create_validating_before_filter(attribute, validator_class, options) ⇒ Object
- #validate_before_processing(validator_class, options) ⇒ Object
-
#validates_attachment(*attributes) ⇒ Object
This method is a shortcut to validator classes that is in “Attachment…Validator” format.
Instance Method Details
#create_validating_before_filter(attribute, validator_class, options) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/upload_documents_tool/validators.rb', line 59 def create_validating_before_filter(attribute, validator_class, ) if_clause = .delete(:if) unless_clause = .delete(:unless) send(:"before_#{attribute}_post_process", :if => if_clause, :unless => unless_clause) do |*args| validator_class.new(.dup).validate(self) end end |
#validate_before_processing(validator_class, options) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/upload_documents_tool/validators.rb', line 50 def validate_before_processing(validator_class, ) = .dup attributes = .delete(:attributes) attributes.each do |attribute| [:attributes] = [attribute] create_validating_before_filter(attribute, validator_class, ) end end |
#validates_attachment(*attributes) ⇒ Object
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:
:avatar, :presence => true,
:content_type => { :content_type => "image/jpg" },
:size => { :in => 0..10.kilobytes }
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/upload_documents_tool/validators.rb', line 30 def (*attributes) = attributes..dup UploadDocumentsTool::Validators.constants.each do |constant| if constant.to_s =~ /\AAttachment(.+)Validator\z/ validator_kind = $1.underscore.to_sym if .has_key?(validator_kind) = .delete(validator_kind) = {} if == true = .slice(:if, :unless) Array.wrap().each do || method_name = UploadDocumentsTool::Validators.const_get(constant.to_s).helper_method_name send(method_name, attributes, .merge()) end end end end end |