validates_timeliness 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,7 @@
1
+ = 3.0.2 [2010-12-04]
2
+ * Fix AR multiparameter extension for Date columns
3
+ * Update to Timeliness 0.3.2 for zone abbreviation and offset support
4
+
1
5
  = 3.0.1 [2010-11-02]
2
6
  * Generate timeliness write methods in an included module to allow overriding in model class (josevalim)
3
7
 
@@ -24,6 +24,7 @@ If you a looking for the old version for Rails 2.x go here[http://github.com/adz
24
24
 
25
25
  * Supports I18n for the error messages
26
26
 
27
+ * Supports MRI 1.8.x and 1.9.x and Rubinius.
27
28
 
28
29
  == Installation
29
30
 
@@ -34,7 +35,7 @@ As plugin (from master)
34
35
  As gem
35
36
 
36
37
  # in Gemfile
37
- gem 'validates_timeliness', '~> 3.0.0'
38
+ gem 'validates_timeliness', '~> 3.0.2'
38
39
 
39
40
  # Run bundler
40
41
  $ bundle install
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ spec = Gem::Specification.new do |s|
22
22
  s.homepage = "http://github.com/adzap/validates_timeliness"
23
23
  s.require_path = 'lib'
24
24
  s.files = %w(validates_timeliness.gemspec LICENSE CHANGELOG.rdoc README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
25
- s.add_runtime_dependency 'timeliness', '~> 0.1.0'
25
+ s.add_runtime_dependency 'timeliness', '~> 0.3.2'
26
26
  end
27
27
 
28
28
  desc 'Default: run specs.'
@@ -10,7 +10,6 @@ module ValidatesTimeliness
10
10
 
11
11
  included do
12
12
  alias_method_chain :datetime_selector, :timeliness
13
- alias_method_chain :value, :timeliness
14
13
  end
15
14
 
16
15
  module InstanceMethods
@@ -22,13 +21,13 @@ module ValidatesTimeliness
22
21
  datetime_selector_without_timeliness(*args)
23
22
  end
24
23
 
25
- def value_with_timeliness(object)
24
+ def value(object)
26
25
  unless @timeliness_date_or_time_tag && @template_object.params[@object_name]
27
- return value_without_timeliness(object)
26
+ return super
28
27
  end
29
28
 
30
29
  pairs = @template_object.params[@object_name].select {|k,v| k =~ /^#{@method_name}\(/ }
31
- return value_without_timeliness(object) if pairs.empty?
30
+ return super if pairs.empty?
32
31
 
33
32
  values = [nil] * 6
34
33
  pairs.map do |(param, value)|
@@ -3,26 +3,64 @@ module ValidatesTimeliness
3
3
  module MultiparameterHandler
4
4
  extend ActiveSupport::Concern
5
5
 
6
+ # Stricter handling of date and time values from multiparameter
7
+ # assignment from the date/time select view helpers
8
+
6
9
  included do
7
10
  alias_method_chain :instantiate_time_object, :timeliness
11
+ alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
8
12
  end
9
13
 
10
14
  private
11
15
 
12
- # Stricter handling of date and time values from multiparameter
13
- # assignment from the date/time select view helpers
14
- #
16
+ def invalid_multiparameter_date_or_time_as_string(values)
17
+ value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-")
18
+ value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty?
19
+ value
20
+ end
21
+
15
22
  def instantiate_time_object_with_timeliness(name, values)
16
- unless Date.valid_civil?(*values[0..2])
17
- value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-")
18
- value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty?
19
- return value
23
+ if Date.valid_civil?(*values[0..2])
24
+ instantiate_time_object_without_timeliness(name, values)
25
+ else
26
+ invalid_multiparameter_date_or_time_as_string(values)
20
27
  end
28
+ end
21
29
 
22
- if self.class.send(:create_time_zone_conversion_attribute?, name, column_for_attribute(name))
23
- Time.zone.local(*values)
24
- else
25
- Time.time_with_datetime_fallback(self.class.default_timezone, *values)
30
+ def instantiate_date_object(name, values)
31
+ values = values.map { |v| v.nil? ? 1 : v }
32
+ Date.new(*values)
33
+ rescue ArgumentError => ex
34
+ invalid_multiparameter_date_or_time_as_string(values)
35
+ end
36
+
37
+ def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
38
+ errors = []
39
+ callstack.each do |name, values_with_empty_parameters|
40
+ begin
41
+ klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
42
+ values = values_with_empty_parameters.reject { |v| v.nil? }
43
+
44
+ if values.empty?
45
+ send(name + "=", nil)
46
+ else
47
+
48
+ value = if Time == klass
49
+ instantiate_time_object(name, values)
50
+ elsif Date == klass
51
+ instantiate_date_object(name, values_with_empty_parameters)
52
+ else
53
+ klass.new(*values)
54
+ end
55
+
56
+ send(name + "=", value)
57
+ end
58
+ rescue => ex
59
+ errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
60
+ end
61
+ end
62
+ unless errors.empty?
63
+ raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
26
64
  end
27
65
  end
28
66
 
@@ -1,3 +1,3 @@
1
1
  module ValidatesTimeliness
2
- VERSION = '3.0.1'
2
+ VERSION = '3.0.2'
3
3
  end
@@ -3,19 +3,31 @@ require 'spec_helper'
3
3
  describe ValidatesTimeliness::Extensions::MultiparameterHandler do
4
4
  let(:employee) { Employee.new }
5
5
 
6
- it 'should return string value for invalid dates' do
7
- instantiate_time_object('birth_date', [2000, 2, 31]).should == '2000-02-31'
6
+ context "time column" do
7
+ it 'should return string value for invalid date portion' do
8
+ multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0])
9
+ employee.birth_datetime_before_type_cast.should == '2000-02-31 12:00:00'
10
+ end
11
+
12
+ it 'should return Time value for valid datetimes' do
13
+ multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
14
+ employee.birth_datetime_before_type_cast.should be_kind_of(Time)
15
+ end
8
16
  end
9
17
 
10
- it 'should return string value for invalid datetimes' do
11
- instantiate_time_object('birth_datetime', [2000, 2, 31, 12, 0, 0]).should == '2000-02-31 12:00:00'
12
- end
13
-
14
- it 'should return Time value for valid datetimes' do
15
- instantiate_time_object('birth_datetime', [2000, 2, 28, 12, 0, 0]).should be_kind_of(Time)
18
+ context "date column" do
19
+ it 'should return string value for invalid date' do
20
+ multiparameter_attribute(:birth_date, [2000, 2, 31])
21
+ employee.birth_date_before_type_cast.should == '2000-02-31'
22
+ end
23
+
24
+ it 'should return Date value for valid date' do
25
+ multiparameter_attribute(:birth_date, [2000, 2, 28])
26
+ employee.birth_date_before_type_cast.should be_kind_of(Date)
27
+ end
16
28
  end
17
29
 
18
- def instantiate_time_object(name, values)
19
- employee.send(:instantiate_time_object, name, values)
30
+ def multiparameter_attribute(name, values)
31
+ employee.send(:execute_callstack_for_multiparameter_attributes, name.to_s => values)
20
32
  end
21
33
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{validates_timeliness}
5
- s.version = "3.0.1"
5
+ s.version = "3.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Adam Meehan"]
9
- s.date = %q{2010-11-02}
9
+ s.date = %q{2010-12-04}
10
10
  s.description = %q{Date and time validation plugin for Rails which allows custom formats}
11
11
  s.email = %q{adam.meehan@gmail.com}
12
12
  s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
@@ -22,11 +22,11 @@ Gem::Specification.new do |s|
22
22
  s.specification_version = 3
23
23
 
24
24
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- s.add_runtime_dependency(%q<timeliness>, ["~> 0.1.0"])
25
+ s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.2"])
26
26
  else
27
- s.add_dependency(%q<timeliness>, ["~> 0.1.0"])
27
+ s.add_dependency(%q<timeliness>, ["~> 0.3.2"])
28
28
  end
29
29
  else
30
- s.add_dependency(%q<timeliness>, ["~> 0.1.0"])
30
+ s.add_dependency(%q<timeliness>, ["~> 0.3.2"])
31
31
  end
32
32
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_timeliness
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
8
  - 0
9
- - 1
10
- version: 3.0.1
9
+ - 2
10
+ version: 3.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Meehan
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-02 00:00:00 +11:00
18
+ date: 2010-12-04 00:00:00 +11:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,12 +26,12 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- hash: 27
29
+ hash: 23
30
30
  segments:
31
31
  - 0
32
- - 1
33
- - 0
34
- version: 0.1.0
32
+ - 3
33
+ - 2
34
+ version: 0.3.2
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  description: Date and time validation plugin for Rails which allows custom formats