No commit activity in last 3 years
No release in over 3 years
American date parsing for ActiveRecord and ActiveModel
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0

Runtime

 Project Readme

AmericanDateParsing

American-style (e.g 12/25/2014) date parsing for ActiveRecord and ActiveModel.

Installation

Add this line to your application's Gemfile:

gem 'american_date_parsing'

And then execute:

$ bundle

Usage

Invoke parse_as_americanized_date in a model.

class MyModel < ActiveRecord::Base
  parse_as_americanized_date :activates_on, :expires_on
end

instance = MyModel.new

instance.activates_on = "12/25/2012"
instance.activates_on
# => Tue, 25 Dec 2012

instance.expires_on = Date.new(2012, 12, 25)
instance.expires_on
# => Tue, 25 Dec 2012

Validation

Presence and format validations are added by passing options to parse_as_americanized_date. Note: make sure to include ActiveModel::Validations for ActiveModel subclasses.

class MyModel < ActiveRecord::Base
  parse_as_americanized_date :activates_on, validate: {
    presence: true,
    format: true
  }
end

instance = MyModel.new

instance.valid?
instance.errors[:activates_on]
# => ["can't be blank"]

instance.activates_on = "1225-2012"
instance.valid?
instance.errors[:activates_on]
# => ["is invalid"]

instance.activates_on = "12-25-2012"
instance.valid?
instance.errors[:activates_on]
# => []

Custom validation messages

Just as with standard validations, :validate accepts a :message option.

class MyModel < ActiveRecord::Base
  parse_as_americanized_date :activates_on, validate: {
    presence: {message: "is required"},
    format: {message: "needs to be formatted as MM/DD/YYYY"}
  }
end

instance = MyModel.new

instance.valid?
instance.errors[:activates_on]
# => ["is required"]

instance.activates_on = "1225-2012"
instance.valid?
instance.errors[:activates_on]
# => ["needs to be formatted as MM/DD/YYYY"]

Alternatively, custom messaging can be acheived via i18n.

en:
  errors:
    attributes:
      activates_on:
        blank: is required
        invalid: needs to be formatted as MM/DD/YYYY

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About Foraker Labs

Foraker Logo

Foraker Labs builds exciting web and mobile apps in Boulder, CO. Our work powers a wide variety of businesses with many different needs. We love open source software, and we're proud to contribute where we can. Interested to learn more? Contact us today.

This project is maintained by Foraker Labs. The names and logos of Foraker Labs are fully owned and copyright Foraker Design, LLC.