Skip to content

Instantly share code, notes, and snippets.

@paul-ihnatolia
Created April 23, 2015 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul-ihnatolia/a4b164f91c8f1cb913bc to your computer and use it in GitHub Desktop.
Save paul-ihnatolia/a4b164f91c8f1cb913bc to your computer and use it in GitHub Desktop.
Custom timestamps in rails
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