Created
September 30, 2014 08:43
-
-
Save abdulsattar/f45be5ed193e7f5416ef to your computer and use it in GitHub Desktop.
Rails overrides
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 ElfTimeZone | |
extend ActiveSupport::Concern | |
included do | |
def timezone | |
Time.zone_for(self) | |
end | |
def self.inherited(child) | |
child.columns.select{|c| c.type == :datetime}.each do |c| | |
child.class_eval <<-METHOD | |
def #{c.name} | |
read_attribute(:#{c.name}).in_time_zone(timezone) if super.respond_to? :in_time_zone | |
end | |
def #{c.name}=(value) | |
written_value = write_attribute(:#{c.name}, value) | |
binding.pry | |
time_in_zone = Time.use_zone(timezone) { Time.zone.local_to_utc(written_value) }.try(:localtime) | |
write_attribute(:#{c.name}, time_in_zone) | |
end | |
METHOD | |
end | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment