zhaoyan1117/PeakDemand

View on GitHub
app/models/date_range_validation.rb

Summary

Maintainability
A
35 mins
Test Coverage
module DateRangeValidation
  
  extend ActiveSupport::Concern

  included do
    validate :validate_start_cannot_be_later_than_end
  end

  private

  def validate_start_cannot_be_later_than_end
    if end_at
      unless start_at
        errors[:date_range] << "Nil columns!" 
        return
      end
        
      if start_at > end_at
        errors[:date_range] << "Start date cannot be later than end date."
      end
    elsif self.class == Demand
      errors[:date_range] << "Nil columns!" 
    end
  end

end