From f0a5414d6095afe15b82f823b346b6c8331dba59 Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Wed, 19 Jun 2013 13:35:32 +0200 Subject: [PATCH 01/12] Initial tests --- Appraisals | 4 ++++ gemfiles/rails_4_0.gemfile | 15 +++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 gemfiles/rails_4_0.gemfile diff --git a/Appraisals b/Appraisals index 55f6d2f..95bb73d 100644 --- a/Appraisals +++ b/Appraisals @@ -10,6 +10,10 @@ appraise "rails_3_2" do gem "rails", "~> 3.2.0" end +appraise "rails_4_0" do + gem "rails", "~> 4.0.0.rc2" +end + appraise "mongoid_2_1" do gem "mongoid", "~> 2.1.0" end diff --git a/gemfiles/rails_4_0.gemfile b/gemfiles/rails_4_0.gemfile new file mode 100644 index 0000000..3a496a0 --- /dev/null +++ b/gemfiles/rails_4_0.gemfile @@ -0,0 +1,15 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +gem "rspec", "~> 2.8" +gem "rspec-rails", "~> 2.8" +gem "timecop" +gem "rspec_tag_matchers" +gem "ruby-debug", :platforms=>[:ruby_18, :jruby] +gem "debugger", :platforms=>[:ruby_19] +gem "appraisal" +gem "sqlite3" +gem "rails", "~> 4.0.0.rc2" + +gemspec :path=>"../" \ No newline at end of file From 1e51a0f2cb1407d009e762206ef70fee6c849d32 Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Fri, 16 Aug 2013 09:05:54 +0200 Subject: [PATCH 02/12] Rails 4.0.0 --- Appraisals | 2 +- gemfiles/rails_4_0.gemfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Appraisals b/Appraisals index 95bb73d..047c79d 100644 --- a/Appraisals +++ b/Appraisals @@ -11,7 +11,7 @@ appraise "rails_3_2" do end appraise "rails_4_0" do - gem "rails", "~> 4.0.0.rc2" + gem "rails", "~> 4.0.0" end appraise "mongoid_2_1" do diff --git a/gemfiles/rails_4_0.gemfile b/gemfiles/rails_4_0.gemfile index 3a496a0..a395318 100644 --- a/gemfiles/rails_4_0.gemfile +++ b/gemfiles/rails_4_0.gemfile @@ -10,6 +10,6 @@ gem "ruby-debug", :platforms=>[:ruby_18, :jruby] gem "debugger", :platforms=>[:ruby_19] gem "appraisal" gem "sqlite3" -gem "rails", "~> 4.0.0.rc2" +gem "rails", "~> 4.0.0" gemspec :path=>"../" \ No newline at end of file From e5076e515e676a47bbf28333bd411b572107c03b Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Fri, 16 Aug 2013 09:13:57 +0200 Subject: [PATCH 03/12] Adjusting view Helpers Working with e70f68fd1ebcb35a9f82b0b3c5a6ea14a4e253cc --- lib/validates_timeliness/extensions.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/validates_timeliness/extensions.rb b/lib/validates_timeliness/extensions.rb index e76485c..1f7e51f 100644 --- a/lib/validates_timeliness/extensions.rb +++ b/lib/validates_timeliness/extensions.rb @@ -5,7 +5,12 @@ module Extensions end def self.enable_date_time_select_extension! - ::ActionView::Helpers::InstanceTag.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect) + + if ActiveRecord::VERSION::MAJOR < 4 + ::ActionView::Helpers::InstanceTag.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect) + else + ::ActionView::Helpers::Tags::DateSelect.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect) + end end def self.enable_multiparameter_extension! From dba6e6ff41f2f2c10a3e1f07986b16685950fc7c Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Thu, 15 Aug 2013 13:50:38 +0200 Subject: [PATCH 04/12] fixing whitespace --- lib/validates_timeliness/extensions/multiparameter_handler.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/validates_timeliness/extensions/multiparameter_handler.rb b/lib/validates_timeliness/extensions/multiparameter_handler.rb index 3da494d..dee3f8a 100644 --- a/lib/validates_timeliness/extensions/multiparameter_handler.rb +++ b/lib/validates_timeliness/extensions/multiparameter_handler.rb @@ -3,7 +3,7 @@ module Extensions module MultiparameterHandler extend ActiveSupport::Concern - # Stricter handling of date and time values from multiparameter + # Stricter handling of date and time values from multiparameter # assignment from the date/time select view helpers included do @@ -66,7 +66,7 @@ def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack) begin send(name + "=", read_value_from_parameter(name, values_with_empty_parameters)) rescue => ex - values = values_with_empty_parameters.is_a?(Hash) ? values_with_empty_parameters.values : values_with_empty_parameters + values = values_with_empty_parameters.is_a?(Hash) ? values_with_empty_parameters.values : values_with_empty_parameters errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name) end end From 138f8b1c2e7c2773875f64714d182056399830db Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Thu, 15 Aug 2013 14:54:20 +0200 Subject: [PATCH 05/12] Fixing deprecation issues --- spec/support/test_model.rb | 1 - spec/validates_timeliness/conversion_spec.rb | 4 ++-- .../extensions/multiparameter_handler_spec.rb | 4 +++- spec/validates_timeliness/validator/after_spec.rb | 6 +++--- spec/validates_timeliness/validator/before_spec.rb | 6 +++--- spec/validates_timeliness/validator/is_at_spec.rb | 8 ++++---- spec/validates_timeliness/validator/on_or_after_spec.rb | 6 +++--- spec/validates_timeliness/validator/on_or_before_spec.rb | 6 +++--- spec/validates_timeliness/validator_spec.rb | 6 +++--- 9 files changed, 24 insertions(+), 23 deletions(-) diff --git a/spec/support/test_model.rb b/spec/support/test_model.rb index 813802f..e594b3c 100644 --- a/spec/support/test_model.rb +++ b/spec/support/test_model.rb @@ -5,7 +5,6 @@ module TestModel include ActiveModel::AttributeMethods included do - attribute_method_suffix "" attribute_method_suffix "=" cattr_accessor :model_attributes end diff --git a/spec/validates_timeliness/conversion_spec.rb b/spec/validates_timeliness/conversion_spec.rb index 88eb693..ebf5c49 100644 --- a/spec/validates_timeliness/conversion_spec.rb +++ b/spec/validates_timeliness/conversion_spec.rb @@ -52,7 +52,7 @@ describe "for datetime type" do it "should return Date as Time value" do - type_cast_value(Date.new(2010, 1, 1), :datetime).should == Time.local_time(2010, 1, 1, 0, 0, 0) + type_cast_value(Date.new(2010, 1, 1), :datetime).should == Time.local(2010, 1, 1, 0, 0, 0) end it "should return same Time value" do @@ -194,7 +194,7 @@ it 'should not use shorthand if symbol if is record method' do time = 1.day.from_now - person.stub!(:now).and_return(time) + person.stub(:now).and_return(time) evaluate_option_value(:now, person).should == time end end diff --git a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb index 3a578c0..fecd353 100644 --- a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +++ b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb @@ -5,9 +5,11 @@ context "time column" do it 'should assign a string value for invalid date portion' do employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0]) + employee.birth_datetime_before_type_cast.class.should eq String + employee.birth_datetime_before_type_cast.should be_a(String) employee.birth_datetime_before_type_cast.should eq '2000-02-31 12:00:00' end - + it 'should assign a Time value for valid datetimes' do employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0]) employee.birth_datetime_before_type_cast.should eq Time.zone.local(2000, 2, 28, 12, 0, 0) diff --git a/spec/validates_timeliness/validator/after_spec.rb b/spec/validates_timeliness/validator/after_spec.rb index e7506de..995b2a6 100644 --- a/spec/validates_timeliness/validator/after_spec.rb +++ b/spec/validates_timeliness/validator/after_spec.rb @@ -25,15 +25,15 @@ end it "should not be valid for same time as restriction" do - invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be after 12:00:00') + invalid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0), 'must be after 12:00:00') end it "should not be valid for time before restriction" do - invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be after 12:00:00') + invalid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59), 'must be after 12:00:00') end it "should be valid for time after restriction" do - valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01)) + valid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01)) end end diff --git a/spec/validates_timeliness/validator/before_spec.rb b/spec/validates_timeliness/validator/before_spec.rb index 5007675..5d81a9c 100644 --- a/spec/validates_timeliness/validator/before_spec.rb +++ b/spec/validates_timeliness/validator/before_spec.rb @@ -25,15 +25,15 @@ end it "should not be valid for time after restriction" do - invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be before 12:00:00') + invalid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01), 'must be before 12:00:00') end it "should not be valid for same time as restriction" do - invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be before 12:00:00') + invalid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0), 'must be before 12:00:00') end it "should be valid for time before restriction" do - valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59)) + valid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59)) end end diff --git a/spec/validates_timeliness/validator/is_at_spec.rb b/spec/validates_timeliness/validator/is_at_spec.rb index 24e7c8f..14532b4 100644 --- a/spec/validates_timeliness/validator/is_at_spec.rb +++ b/spec/validates_timeliness/validator/is_at_spec.rb @@ -2,7 +2,7 @@ describe ValidatesTimeliness::Validator, ":is_at option" do before do - Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0)) + Timecop.freeze(Time.local(2010, 1, 1, 0, 0, 0)) end describe "for date type" do @@ -29,15 +29,15 @@ end it "should not be valid for time before restriction" do - invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be at 12:00:00') + invalid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59), 'must be at 12:00:00') end it "should not be valid for time after restriction" do - invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be at 12:00:00') + invalid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01), 'must be at 12:00:00') end it "should be valid for same time as restriction" do - valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0)) + valid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0)) end end diff --git a/spec/validates_timeliness/validator/on_or_after_spec.rb b/spec/validates_timeliness/validator/on_or_after_spec.rb index 68c91da..82eeb99 100644 --- a/spec/validates_timeliness/validator/on_or_after_spec.rb +++ b/spec/validates_timeliness/validator/on_or_after_spec.rb @@ -25,15 +25,15 @@ end it "should not be valid for time before restriction" do - invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be on or after 12:00:00') + invalid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59), 'must be on or after 12:00:00') end it "should be valid for time after restriction" do - valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01)) + valid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01)) end it "should be valid for same time as restriction" do - valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0)) + valid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0)) end end diff --git a/spec/validates_timeliness/validator/on_or_before_spec.rb b/spec/validates_timeliness/validator/on_or_before_spec.rb index e238aeb..0341d9f 100644 --- a/spec/validates_timeliness/validator/on_or_before_spec.rb +++ b/spec/validates_timeliness/validator/on_or_before_spec.rb @@ -25,15 +25,15 @@ end it "should not be valid for time after restriction" do - invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be on or before 12:00:00') + invalid!(:birth_time, Time.local(2000, 1, 1, 12, 00, 01), 'must be on or before 12:00:00') end it "should be valid for time before restriction" do - valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59)) + valid!(:birth_time, Time.local(2000, 1, 1, 11, 59, 59)) end it "should be valid for same time as restriction" do - valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0)) + valid!(:birth_time, Time.local(2000, 1, 1, 12, 0, 0)) end end diff --git a/spec/validates_timeliness/validator_spec.rb b/spec/validates_timeliness/validator_spec.rb index 6d03ba1..57f1d59 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -2,7 +2,7 @@ describe ValidatesTimeliness::Validator do before do - Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0)) + Timecop.freeze(Time.local(2010, 1, 1, 0, 0, 0)) end describe "Model.validates with :timeliness option" do @@ -35,8 +35,8 @@ it 'should not be valid attribute is type cast to nil but raw value is non-nil invalid value' do Person.validates_date :birth_date, :allow_nil => true record = Person.new - record.stub!(:birth_date).and_return(nil) - record.stub!(:_timeliness_raw_value_for).and_return("Not a date") + record.stub(:birth_date).and_return(nil) + record.stub(:_timeliness_raw_value_for).and_return("Not a date") record.should_not be_valid record.errors[:birth_date].first.should == 'is not a valid date' end From 7178d702ea7e76f81971015bacefa8f6f4156642 Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Fri, 16 Aug 2013 12:45:52 +0200 Subject: [PATCH 06/12] File/Class restructuring to reflect Rails4 structure. --- lib/validates_timeliness/extensions.rb | 16 ++++- .../extensions/attribute_assignment.rb | 32 +++++++++ .../extensions/multiparameter_attribute.rb | 65 +++++++++++++++++++ .../extensions/multiparameter_handler_spec.rb | 2 +- 4 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 lib/validates_timeliness/extensions/attribute_assignment.rb create mode 100644 lib/validates_timeliness/extensions/multiparameter_attribute.rb diff --git a/lib/validates_timeliness/extensions.rb b/lib/validates_timeliness/extensions.rb index 1f7e51f..4c3caff 100644 --- a/lib/validates_timeliness/extensions.rb +++ b/lib/validates_timeliness/extensions.rb @@ -1,7 +1,12 @@ module ValidatesTimeliness module Extensions - autoload :DateTimeSelect, 'validates_timeliness/extensions/date_time_select' - autoload :MultiparameterHandler, 'validates_timeliness/extensions/multiparameter_handler' + autoload :DateTimeSelect, 'validates_timeliness/extensions/date_time_select' + if ActiveRecord::VERSION::MAJOR < 4 + autoload :MultiparameterHandler, 'validates_timeliness/extensions/multiparameter_handler' + else + autoload :AttributeAssignment, 'validates_timeliness/extensions/attribute_assignment' + autoload :MultiparameterAttribute, 'validates_timeliness/extensions/multiparameter_attribute' + end end def self.enable_date_time_select_extension! @@ -14,6 +19,11 @@ def self.enable_date_time_select_extension! end def self.enable_multiparameter_extension! - ::ActiveRecord::Base.send(:include, ValidatesTimeliness::Extensions::MultiparameterHandler) + if ActiveRecord::VERSION::MAJOR < 4 + ::ActiveRecord::Base.send(:include, ValidatesTimeliness::Extensions::MultiparameterHandler) + else + ::ActiveRecord::Base.send(:include, ValidatesTimeliness::Extensions::AttributeAssignment) + ::ActiveRecord::AttributeAssignment::MultiparameterAttribute.send(:include, ValidatesTimeliness::Extensions::MultiparameterAttribute) + end end end diff --git a/lib/validates_timeliness/extensions/attribute_assignment.rb b/lib/validates_timeliness/extensions/attribute_assignment.rb new file mode 100644 index 0000000..13d269a --- /dev/null +++ b/lib/validates_timeliness/extensions/attribute_assignment.rb @@ -0,0 +1,32 @@ +module ValidatesTimeliness + module Extensions + module AttributeAssignment + extend ActiveSupport::Concern + + # Stricter handling of date and time values from multiparameter + # assignment from the date/time select view helpers + + included do + alias_method :execute_callstack_for_multiparameter_attributes, :execute_callstack_for_multiparameter_attributes_with_timeliness + end + + private + + def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack) + errors = [] + callstack.each do |name, values_with_empty_parameters| + begin + send(name + "=", MultipleAttribute.new(self, name, values_with_empty_parameters)) + rescue => ex + values = values_with_empty_parameters.is_a?(Hash) ? values_with_empty_parameters.values : values_with_empty_parameters + errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name) + end + end + unless errors.empty? + raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes" + end + end + + end + end +end diff --git a/lib/validates_timeliness/extensions/multiparameter_attribute.rb b/lib/validates_timeliness/extensions/multiparameter_attribute.rb new file mode 100644 index 0000000..62b7b0b --- /dev/null +++ b/lib/validates_timeliness/extensions/multiparameter_attribute.rb @@ -0,0 +1,65 @@ +module ValidatesTimeliness + module Extensions + module MultiparameterAttribute + extend ActiveSupport::Concern + + # Stricter handling of date and time values from multiparameter + # assignment from the date/time select view helpers + + included do + alias_method_chain :instantiate_time_object, :timeliness + alias_method :read_value, :read_value_with_timeliness + end + + +# private + + def invalid_multiparameter_date_or_time_as_string(values) + value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-") + value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty? + value + end + + def instantiate_time_object_with_timeliness(name, values) + validate_multiparameter_date_values(values) { + instantiate_time_object_without_timeliness(name, values) + } + end + + def instantiate_date_object(name, values) + validate_multiparameter_date_values(values) { + Date.new(*values) + } + end + + # Yield if date values are valid + def validate_multiparameter_date_values(values) + if values[0..2].all?{ |v| v.present? } && Date.valid_civil?(*values[0..2]) + yield + else + invalid_multiparameter_date_or_time_as_string(values) + end + end + + def read_value_with_timeliness(name, values_from_param) + klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass + values = values_from_param.is_a?(Hash) ? values_from_param.to_a.sort_by(&:first).map(&:last) : values_from_param + + if values.empty? || values.all?{ |v| v.nil? } + nil + elsif klass == Time + instantiate_time_object(name, values) + elsif klass == Date + instantiate_date_object(name, values) + else + if respond_to?(:read_other_parameter_value) + read_date_parameter_value(name, values_from_param) + else + klass.new(*values) + end + end + end + + end + end +end diff --git a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb index fecd353..9f41b6b 100644 --- a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +++ b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe ValidatesTimeliness::Extensions::MultiparameterHandler do +describe ValidatesTimeliness::Extensions do context "time column" do it 'should assign a string value for invalid date portion' do From 88bc3e9a6e7f7803a20ac8d72585493c5cd32dc5 Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Thu, 15 Aug 2013 22:25:52 +0200 Subject: [PATCH 07/12] Adapting to Rails4 architecture --- .../extensions/attribute_assignment.rb | 7 ++-- .../extensions/multiparameter_attribute.rb | 41 ++++++++++--------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/validates_timeliness/extensions/attribute_assignment.rb b/lib/validates_timeliness/extensions/attribute_assignment.rb index 13d269a..ed91e18 100644 --- a/lib/validates_timeliness/extensions/attribute_assignment.rb +++ b/lib/validates_timeliness/extensions/attribute_assignment.rb @@ -16,14 +16,15 @@ def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack) errors = [] callstack.each do |name, values_with_empty_parameters| begin - send(name + "=", MultipleAttribute.new(self, name, values_with_empty_parameters)) + send("#{name}=", self.class::MultiparameterAttribute.new(self, name, values_with_empty_parameters).read_value) rescue => ex values = values_with_empty_parameters.is_a?(Hash) ? values_with_empty_parameters.values : values_with_empty_parameters - errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name) + errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name} (#{ex.message})", ex, name) end end unless errors.empty? - raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes" + error_descriptions = errors.map { |ex| ex.message }.join(",") + raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes [#{error_descriptions}]" end end diff --git a/lib/validates_timeliness/extensions/multiparameter_attribute.rb b/lib/validates_timeliness/extensions/multiparameter_attribute.rb index 62b7b0b..b127852 100644 --- a/lib/validates_timeliness/extensions/multiparameter_attribute.rb +++ b/lib/validates_timeliness/extensions/multiparameter_attribute.rb @@ -14,48 +14,49 @@ module MultiparameterAttribute # private - def invalid_multiparameter_date_or_time_as_string(values) - value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-") - value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty? + def invalid_multiparameter_date_or_time_as_string(set_values) + value = [set_values[0], *set_values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-") + value += ' ' + set_values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless set_values[3..5].empty? value end - def instantiate_time_object_with_timeliness(name, values) - validate_multiparameter_date_values(values) { - instantiate_time_object_without_timeliness(name, values) + def instantiate_time_object_with_timeliness(set_values) + validate_multiparameter_date_values(set_values) { + instantiate_time_object_without_timeliness(set_values) } end - def instantiate_date_object(name, values) - validate_multiparameter_date_values(values) { - Date.new(*values) + def instantiate_date_object(set_values) + validate_multiparameter_date_values(set_values) { + Date.new(*set_values) } end # Yield if date values are valid - def validate_multiparameter_date_values(values) - if values[0..2].all?{ |v| v.present? } && Date.valid_civil?(*values[0..2]) + def validate_multiparameter_date_values(set_values) + if set_values[0..2].all?{ |v| v.present? } && Date.valid_civil?(*set_values[0..2]) yield else - invalid_multiparameter_date_or_time_as_string(values) + invalid_multiparameter_date_or_time_as_string(set_values) end end - def read_value_with_timeliness(name, values_from_param) - klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass - values = values_from_param.is_a?(Hash) ? values_from_param.to_a.sort_by(&:first).map(&:last) : values_from_param + def read_value_with_timeliness + @column = object.class.reflect_on_aggregation(name.to_sym) || object.column_for_attribute(name) + klass = column.klass - if values.empty? || values.all?{ |v| v.nil? } + set_values = values.is_a?(Hash) ? values.to_a.sort_by(&:first).map(&:last) : values + if set_values.empty? || set_values.all?{ |v| v.nil? } nil elsif klass == Time - instantiate_time_object(name, values) + instantiate_time_object(set_values) elsif klass == Date - instantiate_date_object(name, values) + instantiate_date_object(set_values) else if respond_to?(:read_other_parameter_value) - read_date_parameter_value(name, values_from_param) + read_date_parameter_value(name, values) else - klass.new(*values) + klass.new(*set_values) end end end From 81c5526259e62ae1b2c157124504b415da5bd51a Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Thu, 15 Aug 2013 22:26:42 +0200 Subject: [PATCH 08/12] In Rails 4 you can no longer assign such invalid dates --- .../extensions/multiparameter_handler_spec.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb index 9f41b6b..dc037de 100644 --- a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +++ b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb @@ -15,9 +15,11 @@ employee.birth_datetime_before_type_cast.should eq Time.zone.local(2000, 2, 28, 12, 0, 0) end - it 'should assign a string value for incomplete time' do - employee = record_with_multiparameter_attribute(:birth_datetime, [2000, nil, nil]) - employee.birth_datetime_before_type_cast.should eq '2000-00-00' + if ActiveRecord::VERSION::MAJOR < 4 + it 'should assign a string value for incomplete time' do + employee = record_with_multiparameter_attribute(:birth_datetime, [2000, nil, nil]) + employee.birth_datetime_before_type_cast.should eq '2000-00-00' + end end end From f83b4e6806cb9a94af91c2c46ad71a20ba73501c Mon Sep 17 00:00:00 2001 From: Jarl Friis Date: Thu, 15 Aug 2013 22:14:49 +0200 Subject: [PATCH 09/12] Default behaviour has changed... --- spec/validates_timeliness/conversion_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/validates_timeliness/conversion_spec.rb b/spec/validates_timeliness/conversion_spec.rb index ebf5c49..6669b91 100644 --- a/spec/validates_timeliness/conversion_spec.rb +++ b/spec/validates_timeliness/conversion_spec.rb @@ -174,7 +174,7 @@ end it 'should return Time value for attribute method symbol which returns string time value' do - value = '2010-01-01 12:00:00' + value = '2010-01-01 12:00:00 UTC' person.birth_time = value evaluate_option_value(:birth_time, person).should == Time.utc(2010,1,1,12,0,0) end From 4412dc790a003e68c0b8338359048a6d42f44022 Mon Sep 17 00:00:00 2001 From: Elika Molayi Date: Thu, 20 Aug 2015 17:27:09 -0400 Subject: [PATCH 10/12] Added multiparameter_handler_spec back and updated it; fixed a conversion spec; re-enabled multiparameter and date_time_select extensions in specs --- spec/spec_helper.rb | 4 ++-- spec/validates_timeliness/conversion_spec.rb | 6 +++--- .../extensions/multiparameter_handler_spec.rb | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c7d8f9c..8bbbd2c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,8 +23,8 @@ ValidatesTimeliness.setup do |c| c.extend_orms = [ :active_record ] - # c.enable_date_time_select_extension! - # c.enable_multiparameter_extension! + c.enable_date_time_select_extension! + c.enable_multiparameter_extension! c.default_timezone = :utc end diff --git a/spec/validates_timeliness/conversion_spec.rb b/spec/validates_timeliness/conversion_spec.rb index 472ea42..ca3b784 100644 --- a/spec/validates_timeliness/conversion_spec.rb +++ b/spec/validates_timeliness/conversion_spec.rb @@ -157,12 +157,12 @@ expect(evaluate_option_value(:birth_time, person)).to eq(value) end - it 'should return Time value is default zone from string time value' do + it 'should return Time value in default zone from string time value' do value = '2010-01-01 12:00:00' expect(evaluate_option_value(value, person)).to eq(Time.utc(2010,1,1,12,0,0)) end - it 'should return Time value is current zone from string time value if timezone aware' do + it 'should return Time value in current zone from string time value if timezone aware' do @timezone_aware = true value = '2010-01-01 12:00:00' expect(evaluate_option_value(value, person)).to eq(Time.zone.local(2010,1,1,12,0,0)) @@ -176,7 +176,7 @@ it 'should return Time value for attribute method symbol which returns string time value' do value = '2010-01-01 12:00:00 UTC' person.birth_time = value - expect(evaluate_option_value(:birth_time, person)).to eq(Time.local(2010,1,1,12,0,0)) + expect(evaluate_option_value(:birth_time, person)).to eq(Time.utc(2010,1,1,12,0,0)) end context "restriction shorthand" do diff --git a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb index 72e0dfa..5040f0d 100644 --- a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +++ b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb @@ -1,35 +1,35 @@ require 'spec_helper' describe ValidatesTimeliness::Extensions do - + context "time column" do it 'should assign a string value for invalid date portion' do employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0]) - employee.birth_datetime_before_type_cast.class.should eq String - employee.birth_datetime_before_type_cast.should be_a(String) - employee.birth_datetime_before_type_cast.should eq '2000-02-31 12:00:00' + expect(employee.birth_datetime_before_type_cast.class).to eq String + expect(employee.birth_datetime_before_type_cast).to be_a(String) + expect(employee.birth_datetime_before_type_cast).to eq '2000-02-31 12:00:00' end it 'should assign a Time value for valid datetimes' do employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0]) - employee.birth_datetime_before_type_cast.should eq Time.zone.local(2000, 2, 28, 12, 0, 0) + expect(employee.birth_datetime_before_type_cast).to eq Time.zone.local(2000, 2, 28, 12, 0, 0) end end context "date column" do it 'should assign a string value for invalid date' do employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 31]) - employee.birth_date_before_type_cast.should eq '2000-02-31' + expect(employee.birth_date_before_type_cast).to eq '2000-02-31' end it 'should assign a Date value for valid date' do employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 28]) - employee.birth_date_before_type_cast.should eq Date.new(2000, 2, 28) + expect(employee.birth_date_before_type_cast).to eq Date.new(2000, 2, 28) end it 'should assign a string value for incomplete date' do employee = record_with_multiparameter_attribute(:birth_date, [2000, nil, nil]) - employee.birth_date_before_type_cast.should eq '2000-00-00' + expect(employee.birth_date_before_type_cast).to eq '2000-00-00' end end From 0db45c6cc2ac51cabf74d25f34438c868925fb63 Mon Sep 17 00:00:00 2001 From: Elika Molayi Date: Fri, 21 Aug 2015 08:31:14 -0400 Subject: [PATCH 11/12] updated specs to use rspec 3 syntax --- spec/validates_timeliness/orm/active_record_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/validates_timeliness/orm/active_record_spec.rb b/spec/validates_timeliness/orm/active_record_spec.rb index f8d3069..94d639c 100644 --- a/spec/validates_timeliness/orm/active_record_spec.rb +++ b/spec/validates_timeliness/orm/active_record_spec.rb @@ -52,21 +52,21 @@ class Janitor < Employee record.birth_date = '2012-01-01' record.valid? - record.errors[:birth_date].should be_empty + expect(record.errors[:birth_date]).to be_empty end it "should validate a invalid value string" do record.birth_date = 'not a date' record.valid? - record.errors[:birth_date].should_not be_empty + expect(record.errors[:birth_date]).not_to be_empty end it "should validate a nil value" do record.birth_date = nil record.valid? - record.errors[:birth_date].should be_empty + expect(record.errors[:birth_date]).to be_empty end end From 5fc06d883857925b140b6f5bab0be2034667b738 Mon Sep 17 00:00:00 2001 From: Elika Molayi Date: Fri, 21 Aug 2015 09:59:22 -0400 Subject: [PATCH 12/12] updated changelog --- CHANGELOG.rdoc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 81990ce..641e064 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,6 @@ += 4.1.0 [2015-08-21] +* Restore extensions for Rails 4 (< 4.2) + = 4.0.0 [2015-07-19] * Better compatibility with Rails 4.x (and drop support < 4.0) * Support Ruby 2.0+