class Dragonfly::Model::Validations::PropertyValidator

Public Instance Methods

validate_each(model, attribute, attachment) click to toggle source
# File lib/dragonfly/model/validations.rb, line 9
def validate_each(model, attribute, attachment)
  if attachment
    property = attachment.send(property_name)
    model.errors.add(attribute, message(property, model)) unless matches?(property)
  end
rescue RuntimeError => e
  Dragonfly.warn("validation of property #{property_name} of #{attribute} failed with error #{e}")
  model.errors.add(attribute, message(nil, model))
end

Private Instance Methods

allowed_values() click to toggle source
# File lib/dragonfly/model/validations.rb, line 50
def allowed_values
  @allowed_values ||= options[:in] || [options[:as]]
end
case_insensitive?() click to toggle source
# File lib/dragonfly/model/validations.rb, line 46
def case_insensitive?
  options[:case_sensitive] == false
end
check_validity!() click to toggle source
# File lib/dragonfly/model/validations.rb, line 38
def check_validity!
  raise ArgumentError, "you must provide either :in => [<value1>, <value2>..] or :as => <value>" unless options[:in] || options[:as]
end
expected_values_string() click to toggle source
# File lib/dragonfly/model/validations.rb, line 54
def expected_values_string
  if allowed_values.is_a?(Range)
    "between #{allowed_values.first} and #{allowed_values.last}"
  else
    allowed_values.length > 1 ? "one of '#{allowed_values.join('\', \'')}'" : "'#{allowed_values.first.to_s}'"
  end
end
matches?(property) click to toggle source
# File lib/dragonfly/model/validations.rb, line 21
def matches?(property)
  if case_insensitive?
    prop = property.to_s.downcase
    allowed_values.any?{|v| v.to_s.downcase == prop }
  else
    allowed_values.include?(property)
  end
end
message(property, model) click to toggle source
# File lib/dragonfly/model/validations.rb, line 30
def message(property, model)
  message = options[:message] ||
    "#{property_name.to_s.humanize.downcase} is incorrect. " +
    "It needs to be #{expected_values_string}" +
    (property ? ", but was '#{property}'" : "")
  message.respond_to?(:call) ? message.call(property, model) : message
end
property_name() click to toggle source
# File lib/dragonfly/model/validations.rb, line 42
def property_name
  options[:property_name]
end