class ActiveModel::Validations::RangeValidator
Constants
- OPTIONS
Public Instance Methods
check_validity!()
click to toggle source
# File lib/range_validator.rb, line 14 def check_validity! options.each do |option, option_value| next if option_value.is_a?(Symbol) || option_value.is_a?(Proc) raise ArgumentError, ":#{option} must be a symbol or a proc" end end
validate_each(record, attribute, value)
click to toggle source
# File lib/range_validator.rb, line 21 def validate_each(record, attribute, value) unless value.is_a? Range record.errors.add(attribute, :not_a_range) return end options.slice(*OPTIONS).each do |option, option_value| other_records = retrieve_other_records(record, option_value) if option == :overlapping && other_records.blank? record.errors.add(attribute, :no_overlap) end other_records.each do |other_record| overlap = value.overlaps? other_record.send(attribute) if option == :overlapping && !overlap record.errors.add(attribute, :no_overlap) elsif option == :not_overlapping && overlap record.errors.add(attribute, :overlap) end end end end
Protected Instance Methods
retrieve_other_records(record, lookup)
click to toggle source
# File lib/range_validator.rb, line 48 def retrieve_other_records(record, lookup) if lookup.is_a?(Symbol) other_records = record.send(lookup) elsif lookup.is_a?(Proc) other_records = lookup.call(record) end responds_to_key = record.respond_to?(:to_key) && !record.to_key.blank? (other_records || []).reject do |other_record| other_record.equal?(record) || (responds_to_key && other_record.to_key == record.to_key) end end