Sha256: 6d41c89c85a0967f0d7957c236bc62ac4c5acb690afbd1084604881d18bb2cd7
Contents?: true
Size: 1.35 KB
Versions: 16
Compression:
Stored size: 1.35 KB
Contents
module Dragonfly module ActiveModelExtensions module Validations private def validates_property(property_name, opts) attrs = opts[:of] or raise ArgumentError, "you need to provide the attribute which has the property, using :of => <attribute_name>" attrs = [attrs].flatten #(make sure it's an array) 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| 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) end end end def 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 end end end end
Version data entries
16 entries across 16 versions & 2 rubygems