Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
module DateWithOptionalTime module ActiveRecord module Base def self.included(base) base.extend(ClassMethods) end module ClassMethods def date_with_optional_time(attributes) class_eval <<-EOV attributes.each do |a| a = a.to_s # i think this would work define_method obj_time send "#{a}_time" end define_method obj_date send "#{a}_date" end define_method(a+"_time_display") do hours = (obj_time / 100).floor minutes = obj_time - (hours * 100) sprintf('%02d:%02d', hours, minutes) end define_method(a+"_time_display=") do |value| send(a+"_time_will_change!") if obj_time == 0 && value.blank? obj_time = value.split(':').join end define_method(a+"_datetime") do if obj_date dt_arr = [obj_date.year, obj_date.month, obj_date.day] time = obj_time || 0 hours = (time / 100).floor minutes = time - (hours * 100) dt_arr += [hours,minutes] DateTime.new(*dt_arr) end end end EOV end end end end end
This paste will be private.
From the Design Piracy series on my blog: