Skip to content

Instantly share code, notes, and snippets.

@romaimperator
Created December 30, 2015 00:20
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 romaimperator/9860eed69ea98fab35ea to your computer and use it in GitHub Desktop.
Save romaimperator/9860eed69ea98fab35ea to your computer and use it in GitHub Desktop.
module VirtualDate
extend ActiveSupport::Concern
class_methods do
def virtual_date(name)
attr_reader name
# Incoming forms pass dates as a hash like {1 => 2014, 2 => 4, 3 => 30}
# This converts that to a date class instance
define_method("#{name}=") do |new_value|
if new_value.is_a?(Hash)
new_value = Date.new(*(new_value.sort.map(&:last)))
end
self.instance_variable_set("@#{name}", new_value)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment