Class: ActiveModel::Validations::StateHistoryValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/active_model/validations/state_history_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ StateHistoryValidator

Returns a new instance of StateHistoryValidator.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/active_model/validations/state_history_validator.rb', line 5

def initialize(options = {})
  super

  @association = options[:association]

  @start = options[:start] || :start
  @end   = options[:end]   || :end
  @order = options[:order] || "#{@start.to_s} ASC, ISNULL(#{@end}) ASC, #{@end} ASC"
  @allow = options[:allow] || []
  @match = options[:self_transition_match] || []
end

Instance Method Details

#validate(record) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_model/validations/state_history_validator.rb', line 17

def validate(record)
  entries = record.send(@association)
  sorted_history = entries.order(@order)

  return if sorted_history.blank?

  if !@allow.include? :inactive_end_state
    valid = last_end_nil?(sorted_history, record.errors)
    return unless valid
  end

  no_self_trans = no_gaps = no_overlaps = no_nils = true
  sorted_history.each_cons(2) do |a|
    this_entry = a[0]
    next_entry = a[1]

    if !@allow.include? :self_transitions
      no_self_trans = no_self_transitions?(this_entry, next_entry, record.errors)
    end

    if !@allow.include? :gaps
      no_gaps = no_gaps?(this_entry, next_entry, record.errors)
    end

    if !@allow.include? :overlaps
      no_overlaps = no_overlaps?(this_entry, next_entry, record.errors)
    end

    if !@allow.include? :active_middle_states
      no_nils = no_intervening_nils?(this_entry, record.errors)
    end

    return unless no_self_trans && no_gaps && no_overlaps && no_nils
  end
end