Skip to content

Instantly share code, notes, and snippets.

@nicolasblanco
Created May 16, 2013 10:10
Show Gist options
  • Select an option

  • Save nicolasblanco/5590733 to your computer and use it in GitHub Desktop.

Select an option

Save nicolasblanco/5590733 to your computer and use it in GitHub Desktop.
class NullableDateWriter < Virtus::Attribute::Writer::Coercible
def coerce(value)
if value.is_a?(Date)
value
elsif value.blank?
nil
else
Date.parse(value)
end
end
end
class BookingWizard
require "nullable_date_writer"
include Virtus
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
attribute :date_of_birth, Date, writer_class: NullableDateWriter
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment