Skip to content

Instantly share code, notes, and snippets.

@abdulsattar
Created September 30, 2014 08:43
Show Gist options
  • Save abdulsattar/f45be5ed193e7f5416ef to your computer and use it in GitHub Desktop.
Save abdulsattar/f45be5ed193e7f5416ef to your computer and use it in GitHub Desktop.
Rails overrides
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