Created
April 23, 2015 13:13
-
-
Save paul-ihnatolia/a4b164f91c8f1cb913bc to your computer and use it in GitHub Desktop.
Custom timestamps in rails
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
module CustomTimestamps | |
extend ActiveSupport::Concern | |
included do | |
class_attribute :custom_updated_at, :custom_created_at | |
before_save :update_custom_updated_at | |
before_create :set_custom_created_at | |
end | |
def update_custom_updated_at | |
self.send("#{self.class.custom_updated_at}=", DateTime.now) | |
end | |
def set_custom_created_at | |
self.send("#{self.class.custom_created_at}=", DateTime.now) | |
end | |
module ClassMethods | |
def custom_timestamp_attrs(hash) | |
self.custom_updated_at = hash[:custom_updated_at] | |
self.custom_created_at = hash[:custom_created_at] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment