Skip to content

Instantly share code, notes, and snippets.

@paul-ihnatolia
Created April 23, 2015 13:19
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/edc59fe53527d368ad91 to your computer and use it in GitHub Desktop.
Save paul-ihnatolia/edc59fe53527d368ad91 to your computer and use it in GitHub Desktop.
Custom timestamps in rails
# As an option can be placed
# in models/concerns
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
class SomeModel < ActiveRecord::Base
include CustomTimestamps
custom_timestamp_attrs custom_updated_at: :your_custom_update_attribute_name,
custom_created_at: :your_custom_create_attribute_name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment