lib/dragonfly/active_model_extensions/validations.rb in dragonfly-0.8.6 vs lib/dragonfly/active_model_extensions/validations.rb in dragonfly-0.9.0

- old
+ new

@@ -10,22 +10,26 @@ raise ArgumentError, "you must provide either :in => [<value1>, <value2>..] or :as => <value>" unless opts[:in] || opts[:as] allowed_values = opts[:in] || [opts[:as]] args = attrs + [opts] - validates_each(*args) do |record, attr, attachment| + validates_each(*args) do |model, attr, attachment| if attachment property = attachment.send(property_name) - record.errors.add(attr, - opts[:message] || - "#{property_name.to_s.humanize.downcase} is incorrect. It needs to be #{expected_values_string(allowed_values)}, but was '#{property}'" - ) unless allowed_values.include?(property) + unless allowed_values.include?(property) + message = opts[:message] || + "#{property_name.to_s.humanize.downcase} is incorrect. "+ + "It needs to be #{Validations.expected_values_string(allowed_values)}"+ + (property ? ", but was '#{property}'" : "") + message = message.call(property, model) if message.respond_to?(:call) + model.errors.add(attr, message) + end end end end - def expected_values_string(allowed_values) + def self.expected_values_string(allowed_values) 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 \ No newline at end of file