validates_timeliness 3.0.12 → 3.0.14

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.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,10 @@
1
+ = 3.0.14 [2012-08-23]
2
+ * Fix for using validates :timeliness => {} form to correctly add attributes to timeliness validated attributes.
3
+
4
+ = 3.0.13 [2012-08-21]
5
+ * Fix ActiveRecord issues with using plugin parser by using old way of caching values.
6
+ * Allow any ActiveRecord non-column attribute to be validated
7
+
1
8
  = 3.0.12 [2012-06-23]
2
9
  * Fix load order issue when relying on Railtie to load ActiveRecord extension
3
10
 
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  = ValidatesTimeliness
2
2
 
3
3
  * Source: http://github.com/adzap/validates_timeliness
4
- * Bugs: http://github.com/adzap/validates_timeliness/issues
4
+ * Issues: http://github.com/adzap/validates_timeliness/issues
5
5
 
6
6
  == Description
7
7
 
@@ -18,24 +18,19 @@ If you a looking for the old version for Rails 2.x go here[http://github.com/adz
18
18
 
19
19
  * Only Rails date/time validation plugin offering complete validation (See ORM/ODM support)
20
20
 
21
- * Adds extensions to fix Rails date/time select issues (See Extensions)
22
-
23
21
  * Uses extensible date/time parser (Using {timeliness gem}[http://github.com/adzap/timeliness]. See Plugin Parser)
24
22
 
25
- * Supports I18n for the error messages
26
-
27
- * Supports Ruby 1.8.x, 1.9.x and Rubinius.
23
+ * Adds extensions to fix Rails date/time select issues (See Extensions)
28
24
 
29
- == Installation
25
+ * Supports I18n for the error messages
30
26
 
31
- As plugin (from master)
27
+ * Supports all the Rubies (that any sane person would be using in production).
32
28
 
33
- rails plugin install git://github.com/adzap/validates_timeliness.git
34
29
 
35
- As gem
30
+ == Installation
36
31
 
37
32
  # in Gemfile
38
- gem 'validates_timeliness', '~> 3.0.2'
33
+ gem 'validates_timeliness', '~> 3.0'
39
34
 
40
35
  # Run bundler
41
36
  $ bundle install
@@ -55,7 +50,7 @@ NOTE: You may wish to enable the plugin parser and the extensions to start. Plea
55
50
  validates_datetime :occurred_at
56
51
 
57
52
  validates_date :date_of_birth, :before => lambda { 18.years.ago },
58
- :before_message => "must be at least 18 years old"
53
+ :before_message => "must be at least 18 years old"
59
54
 
60
55
  validates_datetime :finish_time, :after => :start_time # Method symbol
61
56
 
@@ -79,7 +74,7 @@ validation method
79
74
  validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
80
75
  end
81
76
 
82
- # or even on a specific record, per ActiveModel API.
77
+ or even on a specific record, per ActiveModel API.
83
78
 
84
79
  @person.validates_date :date_of_birth, :on_or_before => lambda { Date.current }
85
80
 
@@ -298,4 +293,4 @@ To see the generous people who have contributed code, take a look at the {contri
298
293
 
299
294
  == License
300
295
 
301
- Copyright (c) 2008-2010 Adam Meehan, released under the MIT license
296
+ Copyright (c) 2008 Adam Meehan, released under the MIT license
data/Rakefile CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'bundler'
2
- Bundler::GemHelper.install_tasks
2
+ require 'bundler/setup'
3
+
4
+ require 'appraisal'
3
5
 
4
- Bundler.setup
6
+ Bundler::GemHelper.install_tasks
5
7
 
6
8
  require 'rake/rdoctask'
7
9
  require 'rspec/core/rake_task'
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.6"
6
+ gem "rspec", "~> 2.8"
7
+ gem "rspec-rails", "~> 2.8"
8
+ gem "timecop"
9
+ gem "rspec_tag_matchers"
10
+ gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
11
+ gem "debugger", :platforms=>[:ruby_19]
12
+ gem "appraisal"
13
+ gem "sqlite3"
14
+ gem "mongoid", "~> 2.1.0"
15
+
16
+ gemspec :path=>"../"
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.6"
6
+ gem "rspec", "~> 2.8"
7
+ gem "rspec-rails", "~> 2.8"
8
+ gem "timecop"
9
+ gem "rspec_tag_matchers"
10
+ gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
11
+ gem "debugger", :platforms=>[:ruby_19]
12
+ gem "appraisal"
13
+ gem "sqlite3"
14
+ gem "mongoid", "~> 2.2.0"
15
+
16
+ gemspec :path=>"../"
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.6"
6
+ gem "rspec", "~> 2.8"
7
+ gem "rspec-rails", "~> 2.8"
8
+ gem "timecop"
9
+ gem "rspec_tag_matchers"
10
+ gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
11
+ gem "debugger", :platforms=>[:ruby_19]
12
+ gem "appraisal"
13
+ gem "sqlite3"
14
+ gem "mongoid", "~> 2.3.0"
15
+
16
+ gemspec :path=>"../"
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.6"
6
+ gem "rspec", "~> 2.8"
7
+ gem "rspec-rails", "~> 2.8"
8
+ gem "timecop"
9
+ gem "rspec_tag_matchers"
10
+ gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
11
+ gem "debugger", :platforms=>[:ruby_19]
12
+ gem "appraisal"
13
+ gem "sqlite3"
14
+ gem "mongoid", "~> 2.4.0"
15
+
16
+ gemspec :path=>"../"
@@ -0,0 +1,15 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rspec", "~> 2.8"
6
+ gem "rspec-rails", "~> 2.8"
7
+ gem "timecop"
8
+ gem "rspec_tag_matchers"
9
+ gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
10
+ gem "debugger", :platforms=>[:ruby_19]
11
+ gem "appraisal"
12
+ gem "sqlite3"
13
+ gem "rails", "~> 3.0.0"
14
+
15
+ gemspec :path=>"../"
@@ -0,0 +1,15 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rspec", "~> 2.8"
6
+ gem "rspec-rails", "~> 2.8"
7
+ gem "timecop"
8
+ gem "rspec_tag_matchers"
9
+ gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
10
+ gem "debugger", :platforms=>[:ruby_19]
11
+ gem "appraisal"
12
+ gem "sqlite3"
13
+ gem "rails", "~> 3.1.0"
14
+
15
+ gemspec :path=>"../"
@@ -0,0 +1,15 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rspec", "~> 2.8"
6
+ gem "rspec-rails", "~> 2.8"
7
+ gem "timecop"
8
+ gem "rspec_tag_matchers"
9
+ gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
10
+ gem "debugger", :platforms=>[:ruby_19]
11
+ gem "appraisal"
12
+ gem "sqlite3"
13
+ gem "rails", "~> 3.2.0"
14
+
15
+ gemspec :path=>"../"
@@ -15,12 +15,7 @@ module ActiveModel
15
15
  end
16
16
 
17
17
  def timeliness_validation_for(attr_names, type)
18
- options = _merge_attributes(attr_names).merge(:type => type)
19
- if respond_to?(:timeliness_validated_attributes)
20
- self.timeliness_validated_attributes ||= []
21
- self.timeliness_validated_attributes += (attr_names - self.timeliness_validated_attributes)
22
- end
23
- validates_with TimelinessValidator, options
18
+ validates_with TimelinessValidator, _merge_attributes(attr_names).merge(:type => type)
24
19
  end
25
20
  end
26
21
 
@@ -3,60 +3,31 @@ module ValidatesTimeliness
3
3
  module ActiveRecord
4
4
  extend ActiveSupport::Concern
5
5
 
6
- def self.use_plugin_cache?
7
- ::ActiveRecord::VERSION::STRING < '3.1.0'
8
- end
9
-
10
- included do
11
- if ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
12
- include Reload
13
- else
14
- # Just use the built-in before_type_cast retrieval
15
- alias_method :_timeliness_raw_value_for, :read_attribute_before_type_cast
16
- end
17
- end
18
-
19
6
  module ClassMethods
20
7
  public
21
8
 
22
9
  def timeliness_attribute_timezone_aware?(attr_name)
23
- attr_name = attr_name.to_s
24
- create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
10
+ create_time_zone_conversion_attribute?(attr_name, timeliness_column_for_attribute(attr_name))
25
11
  end
26
12
 
27
13
  def timeliness_attribute_type(attr_name)
28
- columns_hash[attr_name.to_s].type
14
+ timeliness_column_for_attribute(attr_name).type
29
15
  end
30
16
 
31
- def define_attribute_methods
32
- super.tap do |attribute_methods_generated|
33
- use_before_type_cast = ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache?
34
- define_timeliness_methods use_before_type_cast
17
+ def timeliness_column_for_attribute(attr_name)
18
+ columns_hash.fetch(attr_name.to_s) do |attr_name|
19
+ validation_type = _validators[attr_name.to_sym].find {|v| v.kind == :timeliness }.type
20
+ ::ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, validation_type.to_s)
35
21
  end
36
22
  end
37
23
 
38
- protected
39
-
40
- def define_attribute_timeliness_methods(attr_name, before_type_cast=false)
41
- if before_type_cast
42
- define_timeliness_write_method(attr_name)
43
- define_timeliness_before_type_cast_method(attr_name)
44
- elsif ValidatesTimeliness.use_plugin_parser
45
- define_timeliness_write_method_without_cache(attr_name)
24
+ def define_attribute_methods
25
+ super.tap do |attribute_methods_generated|
26
+ define_timeliness_methods true
46
27
  end
47
28
  end
48
29
 
49
- def define_timeliness_write_method_without_cache(attr_name)
50
- method_body, line = <<-EOV, __LINE__ + 1
51
- def #{attr_name}=(value)
52
- original_value = value
53
- if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend
54
- super(value)
55
- @attributes['#{attr_name}'] = original_value
56
- end
57
- EOV
58
- generated_timeliness_methods.module_eval(method_body, __FILE__, line)
59
- end
30
+ protected
60
31
 
61
32
  def timeliness_type_cast_code(attr_name, var_name)
62
33
  type = timeliness_attribute_type(attr_name)
@@ -67,11 +38,9 @@ module ValidatesTimeliness
67
38
  end
68
39
  end
69
40
 
70
- module Reload
71
- def reload(*args)
72
- _clear_timeliness_cache
73
- super
74
- end
41
+ def reload(*args)
42
+ _clear_timeliness_cache
43
+ super
75
44
  end
76
45
 
77
46
  end
@@ -21,7 +21,7 @@ module ValidatesTimeliness
21
21
  def timeliness_attribute_type(attr_name)
22
22
  {
23
23
  Date => :date,
24
- Time => :datetime,
24
+ Time => :time,
25
25
  DateTime => :datetime
26
26
  }[fields[attr_name.to_s].type] || :datetime
27
27
  end
@@ -22,6 +22,10 @@ module ValidatesTimeliness
22
22
 
23
23
  RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
24
24
 
25
+ def self.kind
26
+ :timeliness
27
+ end
28
+
25
29
  def initialize(options)
26
30
  @type = options.delete(:type) || :datetime
27
31
  @allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
@@ -35,6 +39,13 @@ module ValidatesTimeliness
35
39
  super
36
40
  end
37
41
 
42
+ def setup(model)
43
+ if model.respond_to?(:timeliness_validated_attributes)
44
+ model.timeliness_validated_attributes ||= []
45
+ model.timeliness_validated_attributes |= @attributes
46
+ end
47
+ end
48
+
38
49
  def validate_each(record, attr_name, value)
39
50
  raw_value = attribute_raw_value(record, attr_name) || value
40
51
  return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?)
@@ -1,3 +1,3 @@
1
1
  module ValidatesTimeliness
2
- VERSION = '3.0.12'
2
+ VERSION = '3.0.14'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -92,10 +92,7 @@ RSpec.configure do |c|
92
92
  reset_validation_setup_for(PersonWithShim)
93
93
  end
94
94
 
95
- RSpec.configure do |c|
96
- c.filter_run_excluding :active_record => lambda {|version|
97
- !(::ActiveRecord::VERSION::STRING.to_s =~ /^#{version.to_s}/)
98
- }
99
- end
100
-
95
+ c.filter_run_excluding :active_record => lambda {|version|
96
+ !(::ActiveRecord::VERSION::STRING.to_s =~ /^#{version.to_s}/)
97
+ }
101
98
  end
@@ -41,13 +41,44 @@ describe ValidatesTimeliness, 'ActiveRecord' do
41
41
  end
42
42
 
43
43
  it 'should determine type for attribute' do
44
- Employee.timeliness_attribute_type(:birth_date).should == :date
44
+ Employee.timeliness_attribute_type(:birth_date).should eq :date
45
+ end
46
+
47
+ context 'attribute timezone awareness' do
48
+ let(:klass) {
49
+ Class.new(ActiveRecord::Base) do
50
+ self.table_name = 'employees'
51
+ attr_accessor :some_date
52
+ attr_accessor :some_time
53
+ attr_accessor :some_datetime
54
+ validates_date :some_date
55
+ validates_time :some_time
56
+ validates_datetime :some_datetime
57
+ end
58
+ }
59
+
60
+ context 'for column attribute' do
61
+ it 'should be detected from column type' do
62
+ klass.timeliness_attribute_timezone_aware?(:birth_date).should be_false
63
+ klass.timeliness_attribute_timezone_aware?(:birth_time).should be_false
64
+ klass.timeliness_attribute_timezone_aware?(:birth_datetime).should be_true
65
+ end
66
+ end
67
+
68
+ context 'for non-column attribute' do
69
+ it 'should be detected from the validation type' do
70
+ klass.timeliness_attribute_timezone_aware?(:some_date).should be_false
71
+ klass.timeliness_attribute_timezone_aware?(:some_time).should be_false
72
+ klass.timeliness_attribute_timezone_aware?(:some_datetime).should be_true
73
+ end
74
+ end
45
75
  end
46
76
 
47
77
  context "attribute write method" do
48
78
  class EmployeeWithCache < ActiveRecord::Base
49
- set_table_name 'employees'
79
+ self.table_name = 'employees'
50
80
  validates_date :birth_date, :allow_blank => true
81
+ validates_time :birth_time, :allow_blank => true
51
82
  validates_datetime :birth_datetime, :allow_blank => true
52
83
  end
53
84
 
@@ -55,8 +86,9 @@ describe ValidatesTimeliness, 'ActiveRecord' do
55
86
  context 'for datetime column' do
56
87
  it 'should store raw value' do
57
88
  r = EmployeeWithCache.new
58
- r.birth_datetime = date_string = '2010-01-01'
59
- r._timeliness_raw_value_for('birth_datetime').should == date_string
89
+ r.birth_datetime = datetime_string = '2010-01-01 12:30'
90
+
91
+ r._timeliness_raw_value_for('birth_datetime').should eq datetime_string
60
92
  end
61
93
  end
62
94
 
@@ -64,7 +96,17 @@ describe ValidatesTimeliness, 'ActiveRecord' do
64
96
  it 'should store raw value' do
65
97
  r = EmployeeWithCache.new
66
98
  r.birth_date = date_string = '2010-01-01'
67
- r._timeliness_raw_value_for('birth_date').should == date_string
99
+
100
+ r._timeliness_raw_value_for('birth_date').should eq date_string
101
+ end
102
+ end
103
+
104
+ context 'for time column' do
105
+ it 'should store raw value' do
106
+ r = EmployeeWithCache.new
107
+ r.birth_time = time_string = '12:12'
108
+
109
+ r._timeliness_raw_value_for('birth_time').should eq time_string
68
110
  end
69
111
  end
70
112
  end
@@ -73,36 +115,76 @@ describe ValidatesTimeliness, 'ActiveRecord' do
73
115
  with_config(:use_plugin_parser, true)
74
116
 
75
117
  class EmployeeWithParser < ActiveRecord::Base
76
- set_table_name 'employees'
118
+ self.table_name = 'employees'
77
119
  validates_date :birth_date, :allow_blank => true
120
+ validates_time :birth_time, :allow_blank => true
78
121
  validates_datetime :birth_datetime, :allow_blank => true
79
122
  end
80
123
 
81
- it 'should parse a string value' do
82
- Timeliness::Parser.should_receive(:parse)
83
- r = EmployeeWithParser.new
84
- r.birth_date = '2010-01-01'
85
- end
124
+ context "for a date column" do
125
+ it 'should parse a string value' do
126
+ Timeliness::Parser.should_receive(:parse)
86
127
 
87
- it 'should parse a invalid string value as nil' do
88
- Timeliness::Parser.should_receive(:parse)
89
- r = EmployeeWithParser.new
90
- r.birth_date = 'not a date'
91
- end
128
+ r = EmployeeWithParser.new
129
+ r.birth_date = '2010-01-01'
130
+ end
92
131
 
93
- context "for a date column" do
94
- it 'should store a date value after parsing string' do
132
+ it 'should parse a invalid string value as nil' do
133
+ Timeliness::Parser.should_receive(:parse)
134
+ r = EmployeeWithParser.new
135
+ r.birth_date = 'not valid'
136
+ end
137
+
138
+ it 'should store a Date value after parsing string' do
95
139
  r = EmployeeWithParser.new
96
140
  r.birth_date = '2010-01-01'
97
141
 
98
142
  r.birth_date.should be_kind_of(Date)
99
- r.birth_date.should == Date.new(2010, 1, 1)
143
+ r.birth_date.should eq Date.new(2010, 1, 1)
144
+ end
145
+ end
146
+
147
+ context "for a time column" do
148
+ it 'should parse a string value' do
149
+ Timeliness::Parser.should_receive(:parse)
150
+
151
+ r = EmployeeWithParser.new
152
+ r.birth_time = '12:30'
153
+ end
154
+
155
+ it 'should parse a invalid string value as nil' do
156
+ Timeliness::Parser.should_receive(:parse)
157
+
158
+ r = EmployeeWithParser.new
159
+ r.birth_time = 'not valid'
160
+ end
161
+
162
+ it 'should store a Time value after parsing string' do
163
+ r = EmployeeWithParser.new
164
+ r.birth_time = '12:30'
165
+
166
+ r.birth_time.should be_kind_of(Time)
167
+ r.birth_time.should eq Time.utc(2000, 1, 1, 12, 30)
100
168
  end
101
169
  end
102
170
 
103
171
  context "for a datetime column" do
104
172
  with_config(:default_timezone, 'Australia/Melbourne')
105
173
 
174
+ it 'should parse a string value' do
175
+ Timeliness::Parser.should_receive(:parse)
176
+
177
+ r = EmployeeWithParser.new
178
+ r.birth_datetime = '2010-01-01 12:00'
179
+ end
180
+
181
+ it 'should parse a invalid string value as nil' do
182
+ Timeliness::Parser.should_receive(:parse)
183
+
184
+ r = EmployeeWithParser.new
185
+ r.birth_datetime = 'not valid'
186
+ end
187
+
106
188
  it 'should parse string into Time value' do
107
189
  r = EmployeeWithParser.new
108
190
  r.birth_datetime = '2010-01-01 12:00'
@@ -114,7 +196,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
114
196
  r = EmployeeWithParser.new
115
197
  r.birth_datetime = '2010-06-01 12:00'
116
198
 
117
- r.birth_datetime.utc_offset.should == Time.zone.utc_offset
199
+ r.birth_datetime.utc_offset.should eq Time.zone.utc_offset
118
200
  end
119
201
  end
120
202
  end
@@ -140,7 +222,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
140
222
  r = Employee.new
141
223
  r.birth_datetime = date_string = '2010-01-01'
142
224
 
143
- r.birth_datetime_before_type_cast.should == date_string
225
+ r.birth_datetime_before_type_cast.should eq date_string
144
226
  end
145
227
 
146
228
  it 'should return attribute if no attribute assignment has been made' do
@@ -158,7 +240,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
158
240
  r = Employee.new
159
241
  r.birth_datetime = date_string = '2010-01-31'
160
242
 
161
- r.birth_datetime_before_type_cast.should == date_string
243
+ r.birth_datetime_before_type_cast.should eq date_string
162
244
  end
163
245
  end
164
246
 
@@ -5,6 +5,7 @@ begin
5
5
 
6
6
  require 'mongoid'
7
7
  require 'validates_timeliness/orm/mongoid'
8
+
8
9
  Mongoid.configure do |config|
9
10
  name = "validates_timeliness_test"
10
11
  host = "localhost"
@@ -16,17 +17,17 @@ describe ValidatesTimeliness, 'Mongoid' do
16
17
 
17
18
  class Article
18
19
  include Mongoid::Document
19
- ValidatesTimeliness.use_plugin_parser = true
20
20
  field :publish_date, :type => Date
21
21
  field :publish_time, :type => Time
22
22
  field :publish_datetime, :type => DateTime
23
23
  validates_date :publish_date, :allow_nil => true
24
24
  validates_time :publish_time, :allow_nil => true
25
25
  validates_datetime :publish_datetime, :allow_nil => true
26
- ValidatesTimeliness.use_plugin_parser = false
27
26
  end
28
27
 
29
28
  context "validation methods" do
29
+ let(:record) { Article.new }
30
+
30
31
  it 'should be defined on the class' do
31
32
  Article.should respond_to(:validates_date)
32
33
  Article.should respond_to(:validates_time)
@@ -34,33 +35,33 @@ describe ValidatesTimeliness, 'Mongoid' do
34
35
  end
35
36
 
36
37
  it 'should be defined on the instance' do
37
- Article.new.should respond_to(:validates_date)
38
- Article.new.should respond_to(:validates_time)
39
- Article.new.should respond_to(:validates_datetime)
38
+ record.should respond_to(:validates_date)
39
+ record.should respond_to(:validates_time)
40
+ record.should respond_to(:validates_datetime)
40
41
  end
41
42
 
42
43
  it "should validate a valid value string" do
43
- r = Article.new
44
- r.publish_date = '2012-01-01'
44
+ record.publish_date = '2012-01-01'
45
45
 
46
- r.valid?
47
- r.errors[:publish_date].should be_empty
46
+ record.valid?
47
+ record.errors[:publish_date].should be_empty
48
48
  end
49
49
 
50
50
  it "should validate a invalid value string" do
51
- r = Article.new
52
- r.publish_date = 'not a date'
51
+ begin
52
+ record.publish_date = 'not a date'
53
+ rescue
54
+ end
53
55
 
54
- r.valid?
55
- r.errors[:publish_date].should_not be_empty
56
+ record.valid?
57
+ record.errors[:publish_date].should_not be_empty
56
58
  end
57
59
 
58
60
  it "should validate a nil value" do
59
- r = Article.new
60
- r.publish_date = nil
61
+ record.publish_date = nil
61
62
 
62
- r.valid?
63
- r.errors[:publish_date].should be_empty
63
+ record.valid?
64
+ record.errors[:publish_date].should be_empty
64
65
  end
65
66
  end
66
67
 
@@ -69,46 +70,97 @@ describe ValidatesTimeliness, 'Mongoid' do
69
70
  end
70
71
 
71
72
  context "attribute write method" do
73
+ let(:record) { Article.new }
74
+
72
75
  it 'should cache attribute raw value' do
73
- r = Article.new
74
- r.publish_datetime = date_string = '2010-01-01'
75
- r._timeliness_raw_value_for('publish_datetime').should == date_string
76
+ record.publish_datetime = date_string = '2010-01-01'
77
+
78
+ record._timeliness_raw_value_for('publish_datetime').should == date_string
76
79
  end
77
80
 
78
81
  context "with plugin parser" do
79
- with_config(:use_plugin_parser, true)
80
-
81
- it 'should parse a string value' do
82
- Timeliness::Parser.should_receive(:parse)
83
- r = Article.new
84
- r.publish_date = '2010-01-01'
82
+ let(:record) { ArticleWithParser.new }
83
+
84
+ class ArticleWithParser
85
+ include Mongoid::Document
86
+ field :publish_date, :type => Date
87
+ field :publish_time, :type => Time
88
+ field :publish_datetime, :type => DateTime
89
+
90
+ ValidatesTimeliness.use_plugin_parser = true
91
+ validates_date :publish_date, :allow_nil => true
92
+ validates_time :publish_time, :allow_nil => true
93
+ validates_datetime :publish_datetime, :allow_nil => true
94
+ ValidatesTimeliness.use_plugin_parser = false
85
95
  end
86
96
 
87
- it 'should parse an invalid value as nil' do
88
- Timeliness::Parser.should_receive(:parse)
89
- r = Article.new
90
- r.publish_date = 'bad value'
97
+ context "for a date column" do
98
+ it 'should parse a string value' do
99
+ Timeliness::Parser.should_receive(:parse)
91
100
 
92
- r.publish_date.should be_nil
93
- end
101
+ record.publish_date = '2010-01-01'
102
+ end
103
+
104
+ it 'should parse a invalid string value as nil' do
105
+ Timeliness::Parser.should_receive(:parse)
106
+
107
+ record.publish_date = 'not valid'
108
+ end
94
109
 
95
- context "for a date column" do
96
110
  it 'should store a Date value after parsing string' do
97
- r = Article.new
98
- r.publish_date = '2010-01-01'
111
+ record.publish_date = '2010-01-01'
99
112
 
100
- r.publish_date.should be_kind_of(Date)
101
- r.publish_date.should == Date.new(2010, 1, 1)
113
+ record.publish_date.should be_kind_of(Date)
114
+ record.publish_date.should eq Date.new(2010, 1, 1)
115
+ end
116
+ end
117
+
118
+ context "for a time column" do
119
+ it 'should parse a string value' do
120
+ Timeliness::Parser.should_receive(:parse)
121
+
122
+ record.publish_time = '12:30'
123
+ end
124
+
125
+ it 'should parse a invalid string value as nil' do
126
+ Timeliness::Parser.should_receive(:parse)
127
+
128
+ record.publish_time = 'not valid'
129
+ end
130
+
131
+ it 'should store a Time value after parsing string' do
132
+ record.publish_time = '12:30'
133
+
134
+ record.publish_time.should be_kind_of(Time)
135
+ record.publish_time.should eq Time.utc(2000, 1, 1, 12, 30)
102
136
  end
103
137
  end
104
138
 
105
139
  context "for a datetime column" do
140
+ with_config(:default_timezone, 'Australia/Melbourne')
141
+
142
+ it 'should parse a string value' do
143
+ Timeliness::Parser.should_receive(:parse)
144
+
145
+ record.publish_datetime = '2010-01-01 12:00'
146
+ end
147
+
148
+ it 'should parse a invalid string value as nil' do
149
+ Timeliness::Parser.should_receive(:parse)
150
+
151
+ record.publish_datetime = 'not valid'
152
+ end
153
+
106
154
  it 'should parse string into DateTime value' do
107
- r = Article.new
108
- r.publish_datetime = '2010-01-01 12:00'
155
+ record.publish_datetime = '2010-01-01 12:00'
156
+
157
+ record.publish_datetime.should be_kind_of(DateTime)
158
+ end
159
+
160
+ pending 'should parse string as current timezone' do
161
+ record.publish_datetime = '2010-06-01 12:00'
109
162
 
110
- r.publish_datetime.should be_kind_of(DateTime)
111
- r.publish_datetime.should == DateTime.new(2010,1,1,12,0)
163
+ record.publish_datetime.utc_offset.should eq Time.zone.utc_offset
112
164
  end
113
165
  end
114
166
  end
@@ -116,10 +168,10 @@ describe ValidatesTimeliness, 'Mongoid' do
116
168
 
117
169
  context "cached value" do
118
170
  it 'should be cleared on reload' do
119
- r = Article.create!
120
- r.publish_date = '2010-01-01'
121
- r.reload
122
- r._timeliness_raw_value_for('publish_date').should be_nil
171
+ record = Article.create!
172
+ record.publish_date = '2010-01-01'
173
+ record.reload
174
+ record._timeliness_raw_value_for('publish_date').should be_nil
123
175
  end
124
176
  end
125
177
 
@@ -19,6 +19,14 @@ describe ValidatesTimeliness::Validator do
19
19
  Person.validates :birth_datetime, :timeliness => {:is_at => Time.mktime(2010,1,1)}
20
20
  Person.validators.first.type.should == :datetime
21
21
  end
22
+
23
+ it 'should add attribute to timeliness attributes set' do
24
+ PersonWithShim.timeliness_validated_attributes.should_not include(:birth_time)
25
+
26
+ PersonWithShim.validates :birth_time, :timeliness => {:is_at => "12:30"}
27
+
28
+ PersonWithShim.timeliness_validated_attributes.should include(:birth_time)
29
+ end
22
30
  end
23
31
 
24
32
  it 'should not be valid for value which not valid date or time value' do
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.homepage = %q{http://github.com/adzap/validates_timeliness}
13
13
 
14
14
  s.require_paths = ["lib"]
15
- s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb }
15
+ s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb Appraisals Travis.yml } - Dir['gemsfiles/*']
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
18
18
 
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: 31
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 0
9
- - 12
10
- version: 3.0.12
9
+ - 14
10
+ version: 3.0.14
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: 2012-06-23 00:00:00 +10:00
18
+ date: 2012-08-23 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -49,6 +49,13 @@ files:
49
49
  - LICENSE
50
50
  - README.rdoc
51
51
  - Rakefile
52
+ - gemfiles/mongoid_2_1.gemfile
53
+ - gemfiles/mongoid_2_2.gemfile
54
+ - gemfiles/mongoid_2_3.gemfile
55
+ - gemfiles/mongoid_2_4.gemfile
56
+ - gemfiles/rails_3_0.gemfile
57
+ - gemfiles/rails_3_1.gemfile
58
+ - gemfiles/rails_3_2.gemfile
52
59
  - init.rb
53
60
  - lib/generators/validates_timeliness/install_generator.rb
54
61
  - lib/generators/validates_timeliness/templates/en.yml