Class: Paperclip::Validators::AttachmentFileNameValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Paperclip::Validators::AttachmentFileNameValidator
- Defined in:
- lib/paperclip/validators/attachment_file_name_validator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #allowed ⇒ Object
- #check_validity! ⇒ Object
- #forbidden ⇒ Object
-
#initialize(options) ⇒ AttachmentFileNameValidator
constructor
A new instance of AttachmentFileNameValidator.
- #mark_invalid(record, attribute, patterns) ⇒ Object
- #validate_blacklist(record, attribute, value) ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
- #validate_whitelist(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ AttachmentFileNameValidator
Returns a new instance of AttachmentFileNameValidator.
4 5 6 7 8 9 10 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 4 def initialize() [:allow_nil] = true unless .key?(:allow_nil) unless .key?(:add_validation_errors_to) [:add_validation_errors_to] = Paperclip.[:add_validation_errors_to] end super end |
Class Method Details
.helper_method_name ⇒ Object
12 13 14 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 12 def self.helper_method_name :validates_attachment_file_name end |
Instance Method Details
#allowed ⇒ Object
49 50 51 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 49 def allowed [[:matches]].flatten.compact end |
#check_validity! ⇒ Object
57 58 59 60 61 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 57 def check_validity! unless .key?(:matches) || .key?(:not) raise ArgumentError, "You must pass in either :matches or :not to the validator" end end |
#forbidden ⇒ Object
53 54 55 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 53 def forbidden [[:not]].flatten.compact end |
#mark_invalid(record, attribute, patterns) ⇒ Object
45 46 47 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 45 def mark_invalid(record, attribute, patterns) record.errors.add attribute, :invalid, .merge(names: patterns.join(", ")) end |
#validate_blacklist(record, attribute, value) ⇒ Object
41 42 43 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 41 def validate_blacklist(record, attribute, value) mark_invalid record, attribute, forbidden if forbidden.present? && forbidden.any? { |type| type === value } end |
#validate_each(record, attribute, value) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 16 def validate_each(record, attribute, value) base_attribute = attribute.to_sym attribute = "#{attribute}_file_name".to_sym value = record.send :read_attribute_for_validation, attribute return if (value.nil? && [:allow_nil]) || (value.blank? && [:allow_blank]) validate_whitelist(record, attribute, value) validate_blacklist(record, attribute, value) if record.errors.include?(attribute) && [:both, :base].include?([:add_validation_errors_to]) record.errors[attribute].each do |error| record.errors.add base_attribute, error end record.errors.delete(attribute) if [:add_validation_errors_to] == :base end end |
#validate_whitelist(record, attribute, value) ⇒ Object
37 38 39 |
# File 'lib/paperclip/validators/attachment_file_name_validator.rb', line 37 def validate_whitelist(record, attribute, value) mark_invalid record, attribute, allowed if allowed.present? && allowed.none? { |type| type === value } end |