Last active
December 28, 2015 04:39
-
-
Save vigram/7444391 to your computer and use it in GitHub Desktop.
Custom Date Format made easy
This file contains hidden or 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 DateFormatter | |
def self.included(base) | |
date_columns = base.columns.select{|column| ["date","datetime"].include?(column.sql_type) } | |
date_columns.map(&:name).reject{|col_name| ["created_at", "updated_at"].include?(col_name)}.each{|column| | |
base.class_eval do | |
define_method column do | |
read_attribute(column.to_sym).custom_format | |
end | |
define_method "#{column}=" do |val| | |
write_attribute(column.to_sym, val.db_format) | |
end | |
end | |
} | |
end | |
end | |
#Rails.application.eager_load! if Rails.env == 'development' | |
#ActiveRecord::Base.subclasses.each{ |ar_class| ar_class.send(:include, DateFormatter) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment