taelor (owner)

Revisions

gist: 181213 Download_button fork
public
Public Clone URL: git://gist.github.com/181213.git
Embed All Files: show embed
american_date_monkey_patch.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#
# © 2009 Andrew Coleman
# Released under MIT license.
# http://www.opensource.org/licenses/mit-license.php
#
# total hack to allow american style date parsing.
# does not allow european-ish date parsing, sorry.
#
# some credit from:
# http://talklikeaduck.denhaven2.com/2009/04/26/ruby-1-9-compatibility-for-ri_cal-what-it-took-and-some-side-thoughts
#
module AmericanDateMonkeyPatch
  def to_date
    if self.blank?
      nil
    elsif self =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/
      ::Date.civil($3.to_i, $1.to_i, $2.to_i)
    else
      ::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
    end
  end
end
 
if RUBY_VERSION >= '1.9'
  String.send :include, AmericanDateMonkeyPatch
  # active record will call something else on dates on access
  class ActiveRecord::ConnectionAdapters::Column
    def self.fallback_string_to_date(string)
      string.to_date
    end
  end
end