Skip to content

Instantly share code, notes, and snippets.

View johnathanludwig's full-sized avatar
🖖
It’s chaos, be kind

Johnathan Ludwig johnathanludwig

🖖
It’s chaos, be kind
View GitHub Profile
@johnathanludwig
johnathanludwig / date_range.rb
Last active September 8, 2015 21:26
A custom validator for date ranges. Read the comments for directions on how to use it. Place this file in your initializers directory. I prefer this structure: config/initializers/extensions/active_model/validations/date_range.rb
module ActiveModel
module Validations
class DateRangeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?
pass = true
if comparison_date = options[:before]
if record.send(comparison_date).present?
pass = value < record.send(comparison_date)