Class: Document::DateValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/document/date_validator.rb

Overview

DateValidator

Instance Method Summary collapse

Instance Method Details

#proper_date(record, element, date, valid_date) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/document/date_validator.rb', line 34

def proper_date(record, element, date, valid_date)
  if date.blank?
    valid_date = true
  elsif date.class != Date
    valid_date = false
    record.errors.add(GeoblacklightAdmin::Schema.instance.solr_fields[element.to_sym], "Bad date field type.")
  end

  Rails.logger.debug("#{date} - #{valid_date}")
  valid_date
end

#validate(record) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/document/date_validator.rb', line 11

def validate(record)
  # Assume true
  valid_date = true

  date_elements = Element.where(field_type: "date")

  # Sane date values?
  date_elements.each do |element|
    unless record.send(element.solr_field).nil?
      Rails.logger.debug("Date Validator")
      Rails.logger.debug("Dates: #{record.send(element.solr_field).inspect}")
      record.send(element.solr_field).each do |date|
        Rails.logger.debug("\nDate: #{date}")
        valid_date = proper_date(record, element, date, valid_date)

        break if !valid_date
      end
    end
  end

  valid_date
end