Class: ActsAsSpan::WithinParentDateSpanValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/acts_as_span/within_parent_date_span_validator.rb

Instance Method Summary collapse

Instance Method Details

#outside_of_parent_date_span?(record, parent_sym) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
# File 'lib/acts_as_span/within_parent_date_span_validator.rb', line 11

def outside_of_parent_date_span?(record, parent_sym)
  parent = record.send(parent_sym)

  return false if parent.nil?

    child_record_without_start_date(record, parent) ||
    child_record_without_end_date(record, parent) ||
    child_record_started_before_parent_record(record, parent) ||
      child_record_ended_after_parent_record(record, parent)
end

#validate(record) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/acts_as_span/within_parent_date_span_validator.rb', line 3

def validate(record)
  parents = options[:parent] || options[:parents]

  Array(parents).each do |parent|
    record.errors.add(:base, :not_within_parent_date_span, parent: record.class.human_attribute_name(parent)) if outside_of_parent_date_span?(record, parent)
  end
end