Module: ArrayValidator::OrderValidator

Defined in:
lib/array_validator/order_validator.rb

Class Method Summary collapse

Class Method Details

.call(record, attribute, values, options) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/array_validator/order_validator.rb', line 3

def self.call(record, attribute, values, options)
  return if options[:order].nil?
  raise ArgumentError, 'Not a supported order option' unless [:asc, :desc].include?(options[:order])

  ordered = if options[:order] == :asc
              values.sort == values
            else
              values.sort.reverse == values
            end
  return if ordered

  record.errors[attribute].push(
    I18n.t('order', scope: I18N_SCOPE, direction: "#{options[:order]}ending")
  )
end