From 5edfa5f2fcf757e6c7325669fcd99ebd92027aac Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Mon, 7 Jul 2008 09:50:29 +1000 Subject: [PATCH] renamed module DateHelper to InstanceTag --- lib/validates_timeliness/date_helper.rb | 46 ------------------------ lib/validates_timeliness/instance_tag.rb | 36 +++++++++++++++++++ spec/date_helper_spec.rb | 11 ------ spec/instance_tag_spec.rb | 18 ++++++++++ 4 files changed, 54 insertions(+), 57 deletions(-) delete mode 100644 lib/validates_timeliness/date_helper.rb create mode 100644 lib/validates_timeliness/instance_tag.rb delete mode 100644 spec/date_helper_spec.rb create mode 100644 spec/instance_tag_spec.rb diff --git a/lib/validates_timeliness/date_helper.rb b/lib/validates_timeliness/date_helper.rb deleted file mode 100644 index 9e86463..0000000 --- a/lib/validates_timeliness/date_helper.rb +++ /dev/null @@ -1,46 +0,0 @@ -# This module intercepts the date and time select helpers to allow the -# attribute value before type cast to be used as in the select helpers. -# This means that an invalid date or time will be redisplayed rather than the -# implicitly converted value which occurs by default with Rails. -module ValidatesTimeliness - module DateHelper - - class InstanceTag - TimelinessDateTime = Struct.new(:year, :day, :month, :hour, :min, :sec) - - def to_date_select_tag_with_timeliness(options = {}, html_options = {}) - @timeliness_date_or_time_tag = true - date_or_time_select_without_timeliness(options.merge(:discard_hour => true), html_options) - end - alias_method_chain :to_date_select_tag, :timeliness - - def to_time_select_tag_with_timeliness(options = {}, html_options = {}) - @timeliness_date_or_time_tag = true - date_or_time_select_without_timeliness(options.merge(:discard_year => true, :discard_month => true), html_options) - end - alias_method_chain :to_time_select_tag, :timeliness - - def to_datetime_select_tag_with_timeliness(options = {}, html_options = {}) - @timeliness_date_or_time_tag = true - date_or_time_select_without_timeliness(options, html_options) - end - alias_method_chain :to_date_select_tag, :timeliness - - def value_with_timeliness(object) - return value_without_timeliness(object) unless @timeliness_date_or_time_tag - - raw_value = value_before_type_case(object) - - if raw_value.acts_as?(:time) || raw_value.is_a?(Date) - return value_without_timeliness(object) - end - - time_array = ParseDate.parsedate(raw_value) - - TimelinessDateTime.new(time_array) - end - alias_method_chain :value, :timeliness - - end - end -end diff --git a/lib/validates_timeliness/instance_tag.rb b/lib/validates_timeliness/instance_tag.rb new file mode 100644 index 0000000..28e3109 --- /dev/null +++ b/lib/validates_timeliness/instance_tag.rb @@ -0,0 +1,36 @@ +module ValidatesTimeliness + + # Intercepts the date and time select helpers to allow the + # attribute value before type cast to be used as in the select helpers. + # This means that an invalid date or time will be redisplayed rather than the + # type cast value which would be nil if invalid. + module InstanceTag + + def self.included(base) + base.alias_method_chain :date_or_time_select, :timeliness + base.alias_method_chain :value, :timeliness + end + + TimelinessDateTime = Struct.new(:year, :day, :month, :hour, :min, :sec) + + def date_or_time_select_with_timeliness(*args) + @timeliness_date_or_time_tag = true + date_or_time_select_without_timeliness(*args) + end + + def value_with_timeliness(object) + return value_without_timeliness(object) unless @timeliness_date_or_time_tag + + raw_value = value_before_type_cast(object) + + if raw_value.acts_as?(:time) || raw_value.is_a?(Date) + return value_without_timeliness(object) + end + + time_array = ParseDate.parsedate(raw_value) + + TimelinessDateTime.new(time_array) + end + + end +end diff --git a/spec/date_helper_spec.rb b/spec/date_helper_spec.rb deleted file mode 100644 index dc19bf8..0000000 --- a/spec/date_helper_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -#require File.dirname(__FILE__) + '/spec_helper' - -#describe ValidatesTimeliness::DateHelper::InstanceTag do - -# it "should return struct for time string" do -# object = mock("Model", :birth_date_and_time_before_type_cast => "2008-01-01 12:00:00") -# struct = value_with_timeliness(object) -# struct.should be_kind_of(DateTimeStruct) -# end - -#end diff --git a/spec/instance_tag_spec.rb b/spec/instance_tag_spec.rb new file mode 100644 index 0000000..f86ceb8 --- /dev/null +++ b/spec/instance_tag_spec.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/spec_helper' + +describe ValidatesTimeliness::InstanceTag, :type => :helper do + + before do + @person = Person.new + end + + it "should return struct for time string" do + @person.birth_date_and_time = "2008-02-30 12:00:00" + output = date_select(:person, :birth_date_and_time, :include_blank => true) + puts output + output.should have_tag('select') do + with_tag('input[type=text]', 3) + end + end + +end