validates_timeliness 3.0.5 → 3.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ = 3.0.6 [2011-05-09]
2
+ * Fix for AR type conversion for date columns when using plugin parser.
3
+ * Add timeliness_type_cast_code for ORM specific type casting after parsing.
4
+
1
5
  = 3.0.5 [2011-01-29]
2
6
  * Fix for Conversion#parse when given nil value (closes issue #34)
3
7
 
data/README.rdoc CHANGED
@@ -54,7 +54,7 @@ NOTE: You may wish to enable the plugin parser and the extensions to start. Plea
54
54
 
55
55
  validates_datetime :occurred_at
56
56
 
57
- validates_date :date_of_birth :before => lambda { 18.years.ago },
57
+ validates_date :date_of_birth, :before => lambda { 18.years.ago },
58
58
  :before_message => "must be at least 18 years old"
59
59
 
60
60
  validates_datetime :finish_time, :after => :start_time # Method symbol
data/Rakefile CHANGED
@@ -1,36 +1,8 @@
1
- require 'rubygems'
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
2
4
  require 'rake/rdoctask'
3
- require 'rake/gempackagetask'
4
- require 'rubygems/specification'
5
5
  require 'rspec/core/rake_task'
6
- require 'lib/validates_timeliness/version'
7
-
8
- GEM_NAME = "validates_timeliness"
9
- GEM_VERSION = ValidatesTimeliness::VERSION
10
-
11
- spec = Gem::Specification.new do |s|
12
- s.name = GEM_NAME
13
- s.version = GEM_VERSION
14
- s.platform = Gem::Platform::RUBY
15
- s.summary = %q{Date and time validation plugin for Rails which allows custom formats}
16
- s.description = s.summary
17
- s.author = "Adam Meehan"
18
- s.email = "adam.meehan@gmail.com"
19
- s.homepage = "http://github.com/adzap/validates_timeliness"
20
- s.require_path = 'lib'
21
-
22
- s.add_runtime_dependency 'timeliness', '~> 0.3.2'
23
-
24
- s.files = `git ls-files`.split("\n") - %w{ .rspec .gitignore autotest/discover.rb Gemfile Gemfile.lock }
25
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
-
28
- s.has_rdoc = true
29
- s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
30
- end
31
-
32
- desc 'Default: run specs.'
33
- task :default => :spec
34
6
 
35
7
  desc "Run specs"
36
8
  RSpec::Core::RakeTask.new(:spec)
@@ -50,18 +22,5 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
50
22
  rdoc.rdoc_files.include('lib/**/*.rb')
51
23
  end
52
24
 
53
- Rake::GemPackageTask.new(spec) do |pkg|
54
- pkg.gem_spec = spec
55
- end
56
-
57
- desc "Install the gem locally"
58
- task :install => [:package] do
59
- sh %{gem install pkg/#{GEM_NAME}-#{GEM_VERSION}}
60
- end
61
-
62
- desc "Create a gemspec file"
63
- task :make_spec do
64
- File.open("#{GEM_NAME}.gemspec", "w") do |file|
65
- file.puts spec.to_ruby
66
- end
67
- end
25
+ desc 'Default: run specs.'
26
+ task :default => :spec
@@ -31,14 +31,12 @@ module ValidatesTimeliness
31
31
  end
32
32
 
33
33
  def define_timeliness_write_method(attr_name)
34
- type = timeliness_attribute_type(attr_name)
35
- timezone_aware = timeliness_attribute_timezone_aware?(attr_name)
36
-
37
34
  method_body, line = <<-EOV, __LINE__ + 1
38
35
  def #{attr_name}=(value)
39
36
  @timeliness_cache ||= {}
40
37
  @timeliness_cache["#{attr_name}"] = value
41
- #{ "value = Timeliness::Parser.parse(value, :#{type}, :zone => (:current if #{timezone_aware})) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
38
+
39
+ #{ "if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend" if ValidatesTimeliness.use_plugin_parser }
42
40
  super
43
41
  end
44
42
  EOV
@@ -54,6 +52,14 @@ module ValidatesTimeliness
54
52
  generated_timeliness_methods.module_eval(method_body, __FILE__, line)
55
53
  end
56
54
 
55
+ def timeliness_type_cast_code(attr_name, var_name)
56
+ type = timeliness_attribute_type(attr_name)
57
+ timezone_aware = timeliness_attribute_timezone_aware?(attr_name)
58
+ timezone = :current if timezone_aware
59
+
60
+ "#{var_name} = Timeliness::Parser.parse(#{var_name}, :#{type}, :zone => #{timezone.inspect})"
61
+ end
62
+
57
63
  def generated_timeliness_methods
58
64
  @generated_timeliness_methods ||= Module.new.tap { |m| include(m) }
59
65
  end
@@ -20,7 +20,7 @@ module ActiveModel
20
20
  self.timeliness_validated_attributes ||= []
21
21
  self.timeliness_validated_attributes += (attr_names - self.timeliness_validated_attributes)
22
22
  end
23
- validates_with ValidatesTimeliness::Validator, options
23
+ validates_with TimelinessValidator, options
24
24
  end
25
25
  end
26
26
 
@@ -18,6 +18,15 @@ module ValidatesTimeliness
18
18
  def timeliness_attribute_type(attr_name)
19
19
  columns_hash[attr_name.to_s].type
20
20
  end
21
+
22
+ def timeliness_type_cast_code(attr_name, var_name)
23
+ type = timeliness_attribute_type(attr_name)
24
+
25
+ <<-END
26
+ #{super}
27
+ #{var_name} = #{var_name}.to_date if #{var_name} && :#{type} == :date
28
+ END
29
+ end
21
30
  end
22
31
 
23
32
  module InstanceMethods
@@ -16,17 +16,10 @@ module ValidatesTimeliness
16
16
  attr_names.each { |attr_name| define_timeliness_write_method(attr_name) }
17
17
  end
18
18
 
19
- def define_timeliness_write_method(attr_name)
19
+ def timeliness_type_cast_code(attr_name, var_name)
20
20
  type = timeliness_attribute_type(attr_name)
21
- method_body, line = <<-EOV, __LINE__ + 1
22
- def #{attr_name}=(value)
23
- @timeliness_cache ||= {}
24
- @timeliness_cache["#{attr_name}"] = value
25
- #{ "value = Timeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
26
- write_attribute(:#{attr_name}, value)
27
- end
28
- EOV
29
- class_eval(method_body, __FILE__, line)
21
+
22
+ "#{var_name} = Timeliness::Parser.parse(value, :#{type})"
30
23
  end
31
24
 
32
25
  def timeliness_attribute_type(attr_name)
@@ -20,9 +20,7 @@ module ValidatesTimeliness
20
20
  :datetime => '%Y-%m-%d %H:%M:%S'
21
21
  }.freeze
22
22
 
23
- def self.kind
24
- :timeliness
25
- end
23
+ RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
26
24
 
27
25
  def initialize(options)
28
26
  @type = options.delete(:type) || :datetime
@@ -45,17 +43,22 @@ module ValidatesTimeliness
45
43
  value = parse(raw_value) if value.is_a?(String) || options[:format]
46
44
  value = type_cast_value(value, @type)
47
45
 
48
- return add_error(record, attr_name, :"invalid_#{@type}") if value.blank?
46
+ add_error(record, attr_name, :"invalid_#{@type}") and return if value.blank?
47
+
48
+ validate_restrictions(record, attr_name, value)
49
+ end
49
50
 
51
+ def validate_restrictions(record, attr_name, value)
50
52
  @restrictions_to_check.each do |restriction|
51
53
  begin
52
54
  restriction_value = type_cast_value(evaluate_option_value(options[restriction], record), @type)
53
55
  unless value.send(RESTRICTIONS[restriction], restriction_value)
54
- return add_error(record, attr_name, restriction, restriction_value)
56
+ add_error(record, attr_name, restriction, restriction_value) and break
55
57
  end
56
58
  rescue => e
57
59
  unless ValidatesTimeliness.ignore_restriction_errors
58
- add_error(record, attr_name, "Error occurred validating #{attr_name} for #{restriction.inspect} restriction:\n#{e.message}")
60
+ message = RESTRICTION_ERROR_MESSAGE % [ attr_name, restriction.inspect, e.message ]
61
+ add_error(record, attr_name, message) and break
59
62
  end
60
63
  end
61
64
  end
@@ -1,3 +1,3 @@
1
1
  module ValidatesTimeliness
2
- VERSION = '3.0.5'
2
+ VERSION = '3.0.6'
3
3
  end
@@ -60,11 +60,6 @@ describe ValidatesTimeliness::AttributeMethods do
60
60
  r.birth_date = '2010-01-01'
61
61
  end
62
62
 
63
- it 'should parse string as current timezone' do
64
- r = PersonWithParser.new
65
- r.birth_datetime = '2010-01-01 12:00'
66
- r.birth_datetime.zone == Time.zone.name
67
- end
68
63
  end
69
64
  end
70
65
 
@@ -13,7 +13,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
13
13
 
14
14
  describe "datetime_select" do
15
15
  it "should use param values when attribute is nil" do
16
- params["person"] = {
16
+ @params["person"] = {
17
17
  "birth_datetime(1i)" => 2009,
18
18
  "birth_datetime(2i)" => 2,
19
19
  "birth_datetime(3i)" => 29,
@@ -22,17 +22,12 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
22
22
  "birth_datetime(6i)" => 14,
23
23
  }
24
24
  person.birth_datetime = nil
25
- output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
26
- output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009')
27
- output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'February')
28
- output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '29')
29
- output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12')
30
- output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13')
31
- output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14')
25
+ @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
26
+ should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'February', :day => 29, :hour => 12, :min => 13, :sec => 14)
32
27
  end
33
28
 
34
29
  it "should override object values and use params if present" do
35
- params["person"] = {
30
+ @params["person"] = {
36
31
  "birth_datetime(1i)" => 2009,
37
32
  "birth_datetime(2i)" => 2,
38
33
  "birth_datetime(3i)" => 29,
@@ -41,100 +36,70 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
41
36
  "birth_datetime(6i)" => 14,
42
37
  }
43
38
  person.birth_datetime = "2010-01-01 15:16:17"
44
- output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
45
- output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009')
46
- output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'February')
47
- output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '29')
48
- output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12')
49
- output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13')
50
- output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14')
39
+ @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
40
+ should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'February', :day => 29, :hour => 12, :min => 13, :sec => 14)
51
41
  end
52
42
 
53
43
  it "should use attribute values from object if no params" do
54
44
  person.birth_datetime = "2009-01-02 12:13:14"
55
- output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
56
- output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009')
57
- output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'January')
58
- output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '2')
59
- output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12')
60
- output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13')
61
- output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14')
45
+ @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
46
+ should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'January', :day => 2, :hour => 12, :min => 13, :sec => 14)
62
47
  end
63
48
 
64
49
  it "should use attribute values if params does not contain attribute params" do
65
50
  person.birth_datetime = "2009-01-02 12:13:14"
66
- params["person"] = { }
67
- output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
68
- output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009')
69
- output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'January')
70
- output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '2')
71
- output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12')
72
- output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13')
73
- output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14')
51
+ @params["person"] = { }
52
+ @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
53
+ should_have_datetime_selected(:birth_datetime, :year => 2009, :month => 'January', :day => 2, :hour => 12, :min => 13, :sec => 14)
74
54
  end
75
55
 
76
56
  it "should not select values when attribute value is nil and has no param values" do
77
57
  person.birth_datetime = nil
78
- output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
79
- output.should_not have_tag('select[id=person_birth_datetime_1i] option[selected=selected]')
80
- output.should_not have_tag('select[id=person_birth_datetime_2i] option[selected=selected]')
81
- output.should_not have_tag('select[id=person_birth_datetime_3i] option[selected=selected]')
82
- output.should_not have_tag('select[id=person_birth_datetime_4i] option[selected=selected]')
83
- output.should_not have_tag('select[id=person_birth_datetime_5i] option[selected=selected]')
84
- output.should_not have_tag('select[id=person_birth_datetime_6i] option[selected=selected]')
58
+ @output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
59
+ should_not_have_datetime_selected(:birth_datetime, :year, :month, :day, :hour, :min, :sec)
85
60
  end
86
61
  end
87
62
 
88
63
  describe "date_select" do
89
64
  it "should use param values when attribute is nil" do
90
- params["person"] = {
65
+ @params["person"] = {
91
66
  "birth_date(1i)" => 2009,
92
67
  "birth_date(2i)" => 2,
93
68
  "birth_date(3i)" => 29,
94
69
  }
95
70
  person.birth_date = nil
96
- output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
97
- output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
98
- output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February')
99
- output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29')
71
+ @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
72
+ should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February', :day => 29)
100
73
  end
101
74
 
102
75
  it "should override object values and use params if present" do
103
- params["person"] = {
76
+ @params["person"] = {
104
77
  "birth_date(1i)" => 2009,
105
78
  "birth_date(2i)" => 2,
106
79
  "birth_date(3i)" => 29,
107
80
  }
108
81
  person.birth_date = "2009-03-01"
109
- output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
110
- output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
111
- output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February')
112
- output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29')
82
+ @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
83
+ should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February', :day => 29)
113
84
  end
114
85
 
115
86
  it "should select attribute values from object if no params" do
116
87
  person.birth_date = "2009-01-02"
117
- output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
118
- output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
119
- output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January')
120
- output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2')
88
+ @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
89
+ should_have_datetime_selected(:birth_date, :year => 2009, :month => 'January', :day => 2)
121
90
  end
122
91
 
123
92
  it "should select attribute values if params does not contain attribute params" do
124
93
  person.birth_date = "2009-01-02"
125
- params["person"] = { }
126
- output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
127
- output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
128
- output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January')
129
- output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2')
94
+ @params["person"] = { }
95
+ @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
96
+ should_have_datetime_selected(:birth_date, :year => 2009, :month => 'January', :day => 2)
130
97
  end
131
98
 
132
99
  it "should not select values when attribute value is nil and has no param values" do
133
100
  person.birth_date = nil
134
- output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
135
- output.should_not have_tag('select[id=person_birth_date_1i] option[selected=selected]')
136
- output.should_not have_tag('select[id=person_birth_date_2i] option[selected=selected]')
137
- output.should_not have_tag('select[id=person_birth_date_3i] option[selected=selected]')
101
+ @output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
102
+ should_not_have_datetime_selected(:birth_time, :year, :month, :day)
138
103
  end
139
104
  end
140
105
 
@@ -144,7 +109,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
144
109
  end
145
110
 
146
111
  it "should use param values when attribute is nil" do
147
- params["person"] = {
112
+ @params["person"] = {
148
113
  "birth_time(1i)" => 2000,
149
114
  "birth_time(2i)" => 1,
150
115
  "birth_time(3i)" => 1,
@@ -153,26 +118,34 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
153
118
  "birth_time(6i)" => 14,
154
119
  }
155
120
  person.birth_time = nil
156
- output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
157
- output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12')
158
- output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13')
159
- output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14')
121
+ @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
122
+ should_have_datetime_selected(:birth_time, :hour => 12, :min => 13, :sec => 14)
160
123
  end
161
124
 
162
125
  it "should select attribute values from object if no params" do
163
126
  person.birth_time = "2000-01-01 12:13:14"
164
- output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
165
- output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12')
166
- output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13')
167
- output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14')
127
+ @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
128
+ should_have_datetime_selected(:birth_time, :hour => 12, :min => 13, :sec => 14)
168
129
  end
169
130
 
170
131
  it "should not select values when attribute value is nil and has no param values" do
171
132
  person.birth_time = nil
172
- output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
173
- output.should_not have_tag('select[id=person_birth_time_4i] option[selected=selected]')
174
- output.should_not have_tag('select[id=person_birth_time_5i] option[selected=selected]')
175
- output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]')
133
+ @output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
134
+ should_not_have_datetime_selected(:birth_time, :hour, :min, :sec)
135
+ end
136
+ end
137
+
138
+ def should_have_datetime_selected(field, datetime_hash)
139
+ datetime_hash.each do |key, value|
140
+ index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[key]
141
+ @output.should have_tag("select[id=person_#{field}_#{index}i] option[selected=selected]", value.to_s)
142
+ end
143
+ end
144
+
145
+ def should_not_have_datetime_selected(field, *attributes)
146
+ attributes.each do |attribute|
147
+ index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[attribute]
148
+ @output.should_not have_tag("select[id=person_#{attribute}_#{index}i] option[selected=selected]")
176
149
  end
177
150
  end
178
151
  end
@@ -47,10 +47,32 @@ describe ValidatesTimeliness, 'ActiveRecord' do
47
47
  r.birth_date = '2010-01-01'
48
48
  end
49
49
 
50
- it 'should parse string as current timezone' do
51
- r = EmployeeWithParser.new
52
- r.birth_datetime = '2010-06-01 12:00'
53
- r.birth_datetime.utc_offset.should == 10.hours
50
+ context "for a date column" do
51
+ it 'should store a date value after parsing string' do
52
+ r = EmployeeWithParser.new
53
+ r.birth_date = '2010-01-01'
54
+
55
+ r.birth_date.should be_kind_of(Date)
56
+ r.birth_date.should == Date.new(2010, 1, 1)
57
+ end
58
+ end
59
+
60
+ context "for a datetime column" do
61
+ with_config(:default_timezone, 'Australia/Melbourne')
62
+
63
+ it 'should parse string into Time value' do
64
+ r = EmployeeWithParser.new
65
+ r.birth_datetime = '2010-01-01 12:00'
66
+
67
+ r.birth_datetime.should be_kind_of(Time)
68
+ end
69
+
70
+ it 'should parse string as current timezone' do
71
+ r = EmployeeWithParser.new
72
+ r.birth_datetime = '2010-06-01 12:00'
73
+
74
+ r.birth_datetime.utc_offset.should == Time.zone.utc_offset
75
+ end
54
76
  end
55
77
  end
56
78
  end
@@ -60,10 +60,24 @@ describe ValidatesTimeliness, 'Mongoid' do
60
60
  r.publish_date = '2010-01-01'
61
61
  end
62
62
 
63
- it 'should parse string into Time value' do
64
- r = Article.new
65
- r.publish_datetime = '2010-01-01 12:00'
66
- r.publish_datetime.should == Time.utc(2010,1,1,12,0)
63
+ context "for a date column" do
64
+ it 'should store a date value after parsing string' do
65
+ r = Article.new
66
+ r.publish_date = '2010-01-01'
67
+
68
+ r.publish_date.should be_kind_of(Date)
69
+ r.publish_date.should == Date.new(2010, 1, 1)
70
+ end
71
+ end
72
+
73
+ context "for a datetime column" do
74
+ it 'should parse string into Time value' do
75
+ r = Article.new
76
+ r.publish_datetime = '2010-01-01 12:00'
77
+
78
+ r.publish_datetime.should be_kind_of(Time)
79
+ r.publish_datetime.should == Time.utc(2010,1,1,12,0)
80
+ end
67
81
  end
68
82
  end
69
83
  end
@@ -79,7 +93,7 @@ describe ValidatesTimeliness, 'Mongoid' do
79
93
 
80
94
  context "before_type_cast method" do
81
95
  it 'should not be defined if ORM does not support it' do
82
- Article.new.should_not respond_to(:birth_datetime_before_type_cast)
96
+ Article.new.should_not respond_to(:publish_datetime_before_type_cast)
83
97
  end
84
98
  end
85
99
  end
@@ -7,10 +7,6 @@ describe ValidatesTimeliness::Validator do
7
7
  Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
8
8
  end
9
9
 
10
- it 'should return validator kind as :timeliness' do
11
- ValidatesTimeliness::Validator.kind.should == :timeliness
12
- end
13
-
14
10
  describe "Model.validates with :timeliness option" do
15
11
  it 'should use plugin validator class' do
16
12
  Person.validates :birth_date, :timeliness => {:is_at => Date.new(2010,1,1), :type => :date}
@@ -136,7 +132,7 @@ describe ValidatesTimeliness::Validator do
136
132
  let(:person) { Person.new(:birth_date => Date.today) }
137
133
 
138
134
  before do
139
- Person.validates_time :birth_date, :is_at => lambda { raise }
135
+ Person.validates_time :birth_date, :is_at => lambda { raise }, :before => lambda { raise }
140
136
  end
141
137
 
142
138
  it "should be added when ignore_restriction_errors is false" do
@@ -152,6 +148,13 @@ describe ValidatesTimeliness::Validator do
152
148
  person.errors[:birth_date].should be_empty
153
149
  end
154
150
  end
151
+
152
+ it 'should exit on first error' do
153
+ with_config(:ignore_restriction_errors, false) do
154
+ person.valid?
155
+ person.errors[:birth_date].should have(1).items
156
+ end
157
+ end
155
158
  end
156
159
 
157
160
  describe "#format_error_value" do
@@ -1,32 +1,20 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validates_timeliness/version"
2
4
 
3
5
  Gem::Specification.new do |s|
4
- s.name = %q{validates_timeliness}
5
- s.version = "3.0.5"
6
+ s.name = "validates_timeliness"
7
+ s.version = ValidatesTimeliness::VERSION
8
+ s.authors = ["Adam Meehan"]
9
+ s.summary = %q{Date and time validation plugin for Rails which allows custom formats}
10
+ s.description = %q{Adds validation methods to ActiveModel for validating dates and times. Works with multiple ORMS.}
11
+ s.email = %q{adam.meehan@gmail.com}
12
+ s.homepage = %q{http://github.com/adzap/validates_timeliness}
6
13
 
7
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Adam Meehan"]
9
- s.date = %q{2011-01-29}
10
- s.description = %q{Date and time validation plugin for Rails which allows custom formats}
11
- s.email = %q{adam.meehan@gmail.com}
14
+ s.require_paths = ["lib"]
15
+ s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb }
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
12
17
  s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
13
- s.files = ["CHANGELOG.rdoc", "LICENSE", "README.rdoc", "Rakefile", "init.rb", "lib/generators/validates_timeliness/install_generator.rb", "lib/generators/validates_timeliness/templates/en.yml", "lib/generators/validates_timeliness/templates/validates_timeliness.rb", "lib/validates_timeliness.rb", "lib/validates_timeliness/attribute_methods.rb", "lib/validates_timeliness/conversion.rb", "lib/validates_timeliness/extensions.rb", "lib/validates_timeliness/extensions/date_time_select.rb", "lib/validates_timeliness/extensions/multiparameter_handler.rb", "lib/validates_timeliness/helper_methods.rb", "lib/validates_timeliness/orm/active_record.rb", "lib/validates_timeliness/orm/mongoid.rb", "lib/validates_timeliness/railtie.rb", "lib/validates_timeliness/validator.rb", "lib/validates_timeliness/version.rb", "spec/spec_helper.rb", "spec/support/config_helper.rb", "spec/support/model_helpers.rb", "spec/support/test_model.rb", "spec/validates_timeliness/attribute_methods_spec.rb", "spec/validates_timeliness/conversion_spec.rb", "spec/validates_timeliness/extensions/date_time_select_spec.rb", "spec/validates_timeliness/extensions/multiparameter_handler_spec.rb", "spec/validates_timeliness/helper_methods_spec.rb", "spec/validates_timeliness/orm/active_record_spec.rb", "spec/validates_timeliness/orm/mongoid_spec.rb", "spec/validates_timeliness/validator/after_spec.rb", "spec/validates_timeliness/validator/before_spec.rb", "spec/validates_timeliness/validator/is_at_spec.rb", "spec/validates_timeliness/validator/on_or_after_spec.rb", "spec/validates_timeliness/validator/on_or_before_spec.rb", "spec/validates_timeliness/validator_spec.rb", "spec/validates_timeliness_spec.rb", "validates_timeliness.gemspec"]
14
- s.homepage = %q{http://github.com/adzap/validates_timeliness}
15
- s.require_paths = ["lib"]
16
- s.rubygems_version = %q{1.3.7}
17
- s.summary = %q{Date and time validation plugin for Rails which allows custom formats}
18
- s.test_files = ["spec/spec_helper.rb", "spec/support/config_helper.rb", "spec/support/model_helpers.rb", "spec/support/test_model.rb", "spec/validates_timeliness/attribute_methods_spec.rb", "spec/validates_timeliness/conversion_spec.rb", "spec/validates_timeliness/extensions/date_time_select_spec.rb", "spec/validates_timeliness/extensions/multiparameter_handler_spec.rb", "spec/validates_timeliness/helper_methods_spec.rb", "spec/validates_timeliness/orm/active_record_spec.rb", "spec/validates_timeliness/orm/mongoid_spec.rb", "spec/validates_timeliness/validator/after_spec.rb", "spec/validates_timeliness/validator/before_spec.rb", "spec/validates_timeliness/validator/is_at_spec.rb", "spec/validates_timeliness/validator/on_or_after_spec.rb", "spec/validates_timeliness/validator/on_or_before_spec.rb", "spec/validates_timeliness/validator_spec.rb", "spec/validates_timeliness_spec.rb"]
19
18
 
20
- if s.respond_to? :specification_version then
21
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
- s.specification_version = 3
23
-
24
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.2"])
26
- else
27
- s.add_dependency(%q<timeliness>, ["~> 0.3.2"])
28
- end
29
- else
30
- s.add_dependency(%q<timeliness>, ["~> 0.3.2"])
31
- end
19
+ s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.3"])
32
20
  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: 13
5
- prerelease: false
4
+ hash: 11
5
+ prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 0
9
- - 5
10
- version: 3.0.5
9
+ - 6
10
+ version: 3.0.6
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: 2011-01-29 00:00:00 +11:00
18
+ date: 2011-05-09 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,15 +26,15 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- hash: 23
29
+ hash: 21
30
30
  segments:
31
31
  - 0
32
32
  - 3
33
- - 2
34
- version: 0.3.2
33
+ - 3
34
+ version: 0.3.3
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
- description: Date and time validation plugin for Rails which allows custom formats
37
+ description: Adds validation methods to ActiveModel for validating dates and times. Works with multiple ORMS.
38
38
  email: adam.meehan@gmail.com
39
39
  executables: []
40
40
 
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  requirements: []
115
115
 
116
116
  rubyforge_project:
117
- rubygems_version: 1.3.7
117
+ rubygems_version: 1.5.2
118
118
  signing_key:
119
119
  specification_version: 3
120
120
  summary: Date and time validation plugin for Rails which allows custom formats