Created
March 22, 2012 16:29
-
-
Save bwillis/2159383 to your computer and use it in GitHub Desktop.
Fix unprecise MySQL dates for active record
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveRecord::Base | |
DATE_FIELDS = [:created_at, :updated_at] | |
after_save :make_dates_less_precise | |
def make_dates_less_precise | |
DATE_FIELDS.each do |date_field| | |
if respond_to? date_field | |
time = self.send(date_field) | |
write_attribute(date_field, Time.at(time.to_i)) unless time.nil? | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe "MonkeyPatch MySQL imprecise dates" do | |
context "given a newly saved model" do | |
before do | |
@object = Factory :user | |
end | |
it "has dates that are equal to the looked up object" do | |
User.find(@object.to_param).created_at == @object.created_at | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment