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(*args) class_eval do args.each do |a| a = a.to_s define_method "#{a}_time_obj" do send "#{a}_time" end define_method "#{a}_date_obj" do send "#{a}_date" end define_method(a+"_time_display") do hours = (send(a+"_time_obj") / 100).floor minutes = send(a+"_time_obj") - (hours * 100) sprintf('%02d:%02d', hours, minutes) end define_method(a+"_time_display=") do |value| send(a+"_time_will_change!") if send(a+"_time_obj") == 0 && value.blank? send(a+"_time=", value.split(':').join) end define_method(a+"_datetime") do if send(a+"_date_obj") dt_arr = [send(a+"_date_obj").year, send(a+"_date_obj").month, send(a+"_date_obj").day] time = send(a+"_time_obj") || 0 hours = (time / 100).floor minutes = time - (hours * 100) dt_arr += [hours,minutes] DateTime.new(*dt_arr) end end end end end end end end end
This paste will be private.
From the Design Piracy series on my blog: