validates_timeliness 3.0.0.beta.2 → 3.0.0.beta.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,7 +32,7 @@ As plugin (from master)
32
32
  As gem (in beta)
33
33
 
34
34
  # in Gemfile
35
- gem 'validates_timeliness', '3.0.0.beta'
35
+ gem 'validates_timeliness', '>= 3.0.0.beta'
36
36
 
37
37
  # Run bundler
38
38
  $ bundle install
data/Rakefile CHANGED
@@ -20,9 +20,7 @@ spec = Gem::Specification.new do |s|
20
20
  s.author = "Adam Meehan"
21
21
  s.email = "adam.meehan@gmail.com"
22
22
  s.homepage = "http://github.com/adzap/validates_timeliness"
23
-
24
23
  s.require_path = 'lib'
25
- s.autorequire = GEM_NAME
26
24
  s.files = %w(validates_timeliness.gemspec LICENSE CHANGELOG README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
27
25
  end
28
26
 
@@ -2,6 +2,9 @@ ValidatesTimeliness.setup do |config|
2
2
  # Add plugin to supported ORMs (only :active_record for now)
3
3
  # config.extend_orms = [ :active_record ]
4
4
  #
5
+ # User the plugin date/time parser which is stricter and extendable
6
+ # config.use_plugin_parser = false
7
+ #
5
8
  # Set the dummy date part for a time type values.
6
9
  # config.dummy_date_for_time_type = [ 2000, 1, 1 ]
7
10
  #
@@ -42,7 +42,7 @@ module ValidatesTimeliness
42
42
  # Setup method for plugin configuration
43
43
  def self.setup
44
44
  yield self
45
- extend_orms.each {|orm| require "validates_timeliness/orms/#{orm}" }
45
+ extend_orms.each {|orm| require "validates_timeliness/orm/#{orm}" }
46
46
  end
47
47
  end
48
48
 
@@ -5,6 +5,7 @@ module ValidatesTimeliness
5
5
  module ClassMethods
6
6
 
7
7
  def define_timeliness_methods(before_type_cast=false)
8
+ return if timeliness_validated_attributes.blank?
8
9
  timeliness_validated_attributes.each do |attr_name, type|
9
10
  define_timeliness_write_method(attr_name, type, timeliness_attribute_timezone_aware?(attr_name))
10
11
  define_timeliness_before_type_cast_method(attr_name) if before_type_cast
@@ -18,7 +19,7 @@ module ValidatesTimeliness
18
19
  def #{attr_name}=(value)
19
20
  @attributes_cache ||= {}
20
21
  @attributes_cache["_#{attr_name}_before_type_cast"] = value
21
- #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
22
+ #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}, :timezone_aware => #{timezone_aware}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
22
23
  super
23
24
  end
24
25
  EOV
@@ -34,6 +35,8 @@ module ValidatesTimeliness
34
35
  class_eval(method_body, __FILE__, line)
35
36
  end
36
37
 
38
+ public
39
+
37
40
  def timeliness_attribute_timezone_aware?(attr_name)
38
41
  false
39
42
  end
@@ -0,0 +1,35 @@
1
+ module ValidatesTimeliness
2
+ module ORM
3
+ module Mongoid
4
+ extend ActiveSupport::Concern
5
+ # You need define the fields before you define the validations.
6
+ # It is best to use the plugin parser to avoid errors on a bad
7
+ # field value in Mongoid. Parser will return nil rather than error.
8
+
9
+ module ClassMethods
10
+ def timeliness_validation_for(attr_names, type)
11
+ super
12
+ attr_names.each { |attr_name| define_timeliness_write_method(attr_name, type, false) }
13
+ end
14
+
15
+ def define_timeliness_write_method(attr_name, type, timezone_aware)
16
+ method_body, line = <<-EOV, __LINE__ + 1
17
+ def #{attr_name}=(value)
18
+ @attributes_cache ||= {}
19
+ @attributes_cache["_#{attr_name}_before_type_cast"] = value
20
+ #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser }
21
+ write_attribute(:#{attr_name}, value)
22
+ end
23
+ EOV
24
+ class_eval(method_body, __FILE__, line)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ module Mongoid::Document
32
+ include ValidatesTimeliness::HelperMethods
33
+ include ValidatesTimeliness::AttributeMethods
34
+ include ValidatesTimeliness::ORM::Mongoid
35
+ end
@@ -35,7 +35,7 @@ module ValidatesTimeliness
35
35
  return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?)
36
36
 
37
37
  @timezone_aware = record.class.timeliness_attribute_timezone_aware?(attr_name)
38
- value = parse(value) if value.is_a?(String)
38
+ value = parse(raw_value) if value.is_a?(String) || options[:format]
39
39
  value = type_cast_value(value, @type)
40
40
 
41
41
  return record.errors.add(attr_name, :"invalid_#{@type}") if value.blank?
@@ -1,3 +1,3 @@
1
1
  module ValidatesTimeliness
2
- VERSION = '3.0.0.beta.2'
2
+ VERSION = '3.0.0.beta.3'
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'rspec'
2
- require 'rspec/autorun'
3
2
 
4
3
  require 'active_model'
5
4
  require 'active_model/validations'
@@ -14,12 +14,17 @@ module TestModel
14
14
 
15
15
  module ClassMethods
16
16
  def define_method_attribute=(attr_name)
17
- generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=new_value ; end", __FILE__, __LINE__)
17
+ generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=self.class.type_cast(new_value); end", __FILE__, __LINE__)
18
18
  end
19
19
 
20
20
  def define_method_attribute(attr_name)
21
21
  generated_attribute_methods.module_eval("def #{attr_name}; @attributes['#{attr_name}']; end", __FILE__, __LINE__)
22
22
  end
23
+
24
+ def type_cast(value)
25
+ return value unless value.is_a?(String)
26
+ value.to_time rescue nil
27
+ end
23
28
  end
24
29
 
25
30
  module DynamicMethods
@@ -6,21 +6,27 @@ describe ValidatesTimeliness::AttributeMethods do
6
6
  end
7
7
 
8
8
  context "attribute write method" do
9
- class EmployeeWithCache < ActiveRecord::Base
10
- set_table_name 'employees'
9
+ class PersonWithCache
10
+ include TestModel
11
+ self.model_attributes = :birth_date, :birth_time, :birth_datetime
12
+ validates_date :birth_date
13
+ validates_time :birth_time
11
14
  validates_datetime :birth_datetime
12
15
  end
13
16
 
14
17
  it 'should cache attribute raw value' do
15
- r = EmployeeWithCache.new
18
+ r = PersonWithCache.new
16
19
  r.birth_datetime = date_string = '2010-01-01'
17
20
  r._timeliness_raw_value_for(:birth_datetime).should == date_string
18
21
  end
19
22
 
20
23
  context "with plugin parser" do
21
- class EmployeeWithParser < ActiveRecord::Base
22
- set_table_name 'employees'
23
- validates_datetime :birth_date
24
+ class PersonWithParser
25
+ include TestModel
26
+ self.model_attributes = :birth_date, :birth_time, :birth_datetime
27
+ validates_date :birth_date
28
+ validates_time :birth_time
29
+ validates_datetime :birth_datetime
24
30
  end
25
31
 
26
32
  before :all do
@@ -29,14 +35,14 @@ describe ValidatesTimeliness::AttributeMethods do
29
35
 
30
36
  it 'should parse a string value' do
31
37
  ValidatesTimeliness::Parser.should_receive(:parse)
32
- r = EmployeeWithParser.new
38
+ r = PersonWithParser.new
33
39
  r.birth_date = '2010-01-01'
34
40
  end
35
41
 
36
- it 'should be strict on day values' do
37
- r = EmployeeWithParser.new
38
- r.birth_date = '2010-02-31'
39
- r.birth_date.should be_nil
42
+ it 'should parse string as current timezone' do
43
+ r = PersonWithParser.new
44
+ r.birth_datetime = '2010-01-01 12:00'
45
+ r.birth_datetime.zone == Time.zone.name
40
46
  end
41
47
 
42
48
  after :all do
@@ -46,18 +52,8 @@ describe ValidatesTimeliness::AttributeMethods do
46
52
  end
47
53
 
48
54
  context "before_type_cast method" do
49
- it 'should be defined on class if ORM supports it' do
50
- Employee.instance_methods(false).should include("birth_datetime_before_type_cast")
51
- end
52
-
53
55
  it 'should not be defined if ORM does not support it' do
54
56
  Person.instance_methods(false).should_not include("birth_datetime_before_type_cast")
55
57
  end
56
-
57
- it 'should return original value' do
58
- r = Employee.new
59
- r.birth_datetime = date_string = '2010-01-01'
60
- r.birth_datetime_before_type_cast.should == date_string
61
- end
62
58
  end
63
59
  end
@@ -168,19 +168,12 @@ describe ValidatesTimeliness::Conversion do
168
168
  evaluate_option_value(lambda { value }, person).should == Time.utc(2010,1,1,12,0,0)
169
169
  end
170
170
 
171
- it 'should return Time value in default zone for attribute method symbol which returns string time value' do
171
+ it 'should return Time value for attribute method symbol which returns string time value' do
172
172
  value = '2010-01-01 12:00:00'
173
173
  person.birth_time = value
174
174
  evaluate_option_value(:birth_time, person).should == Time.utc(2010,1,1,12,0,0)
175
175
  end
176
176
 
177
- it 'should return Time value in current zone attribute method symbol which returns string time value if timezone aware' do
178
- @timezone_aware = true
179
- value = '2010-01-01 12:00:00'
180
- person.birth_time = value
181
- evaluate_option_value(:birth_time, person).should == Time.zone.local(2010,1,1,12,0,0)
182
- end
183
-
184
177
  context "restriction shorthand" do
185
178
  before do
186
179
  Timecop.freeze(Time.mktime(2010, 1, 1, 0, 0, 0))
@@ -2,17 +2,20 @@ require 'spec_helper'
2
2
 
3
3
  describe ValidatesTimeliness::Extensions::DateTimeSelect do
4
4
  include ActionView::Helpers::DateHelper
5
+ attr_reader :person, :params
5
6
 
6
- attr_reader :employee, :params
7
+ before :all do
8
+ ValidatesTimeliness.use_plugin_parser = true
9
+ end
7
10
 
8
11
  before do
9
- @employee = Employee.new
12
+ @person = Person.new
10
13
  @params = {}
11
14
  end
12
15
 
13
16
  describe "datetime_select" do
14
17
  it "should use param values when attribute is nil" do
15
- params["employee"] = {
18
+ params["person"] = {
16
19
  "birth_datetime(1i)" => 2009,
17
20
  "birth_datetime(2i)" => 2,
18
21
  "birth_datetime(3i)" => 29,
@@ -20,18 +23,18 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
20
23
  "birth_datetime(5i)" => 13,
21
24
  "birth_datetime(6i)" => 14,
22
25
  }
23
- employee.birth_datetime = nil
24
- output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
25
- output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009')
26
- output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'February')
27
- output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '29')
28
- output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12')
29
- output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13')
30
- output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14')
26
+ person.birth_datetime = nil
27
+ output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
28
+ output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009')
29
+ output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'February')
30
+ output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '29')
31
+ output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12')
32
+ output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13')
33
+ output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14')
31
34
  end
32
35
 
33
36
  it "should override object values and use params if present" do
34
- params["employee"] = {
37
+ params["person"] = {
35
38
  "birth_datetime(1i)" => 2009,
36
39
  "birth_datetime(2i)" => 2,
37
40
  "birth_datetime(3i)" => 29,
@@ -39,101 +42,101 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
39
42
  "birth_datetime(5i)" => 13,
40
43
  "birth_datetime(6i)" => 14,
41
44
  }
42
- employee.birth_datetime = "2010-01-01 15:16:17"
43
- output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
44
- output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009')
45
- output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'February')
46
- output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '29')
47
- output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12')
48
- output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13')
49
- output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14')
45
+ person.birth_datetime = "2010-01-01 15:16:17"
46
+ output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
47
+ output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009')
48
+ output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'February')
49
+ output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '29')
50
+ output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12')
51
+ output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13')
52
+ output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14')
50
53
  end
51
54
 
52
55
  it "should use attribute values from object if no params" do
53
- employee.birth_datetime = "2009-01-02 12:13:14"
54
- output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
55
- output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009')
56
- output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'January')
57
- output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '2')
58
- output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12')
59
- output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13')
60
- output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14')
56
+ person.birth_datetime = "2009-01-02 12:13:14"
57
+ output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
58
+ output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009')
59
+ output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'January')
60
+ output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '2')
61
+ output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12')
62
+ output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13')
63
+ output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14')
61
64
  end
62
65
 
63
66
  it "should use attribute values if params does not contain attribute params" do
64
- employee.birth_datetime = "2009-01-02 12:13:14"
65
- params["employee"] = { }
66
- output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
67
- output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009')
68
- output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'January')
69
- output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '2')
70
- output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12')
71
- output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13')
72
- output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14')
67
+ person.birth_datetime = "2009-01-02 12:13:14"
68
+ params["person"] = { }
69
+ output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
70
+ output.should have_tag('select[id=person_birth_datetime_1i] option[selected=selected]', '2009')
71
+ output.should have_tag('select[id=person_birth_datetime_2i] option[selected=selected]', 'January')
72
+ output.should have_tag('select[id=person_birth_datetime_3i] option[selected=selected]', '2')
73
+ output.should have_tag('select[id=person_birth_datetime_4i] option[selected=selected]', '12')
74
+ output.should have_tag('select[id=person_birth_datetime_5i] option[selected=selected]', '13')
75
+ output.should have_tag('select[id=person_birth_datetime_6i] option[selected=selected]', '14')
73
76
  end
74
77
 
75
78
  it "should not select values when attribute value is nil and has no param values" do
76
- employee.birth_datetime = nil
77
- output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
78
- output.should_not have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]')
79
- output.should_not have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]')
80
- output.should_not have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]')
81
- output.should_not have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]')
82
- output.should_not have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]')
83
- output.should_not have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]')
79
+ person.birth_datetime = nil
80
+ output = datetime_select(:person, :birth_datetime, :include_blank => true, :include_seconds => true)
81
+ output.should_not have_tag('select[id=person_birth_datetime_1i] option[selected=selected]')
82
+ output.should_not have_tag('select[id=person_birth_datetime_2i] option[selected=selected]')
83
+ output.should_not have_tag('select[id=person_birth_datetime_3i] option[selected=selected]')
84
+ output.should_not have_tag('select[id=person_birth_datetime_4i] option[selected=selected]')
85
+ output.should_not have_tag('select[id=person_birth_datetime_5i] option[selected=selected]')
86
+ output.should_not have_tag('select[id=person_birth_datetime_6i] option[selected=selected]')
84
87
  end
85
88
  end
86
89
 
87
90
  describe "date_select" do
88
91
  it "should use param values when attribute is nil" do
89
- params["employee"] = {
92
+ params["person"] = {
90
93
  "birth_date(1i)" => 2009,
91
94
  "birth_date(2i)" => 2,
92
95
  "birth_date(3i)" => 29,
93
96
  }
94
- employee.birth_date = nil
95
- output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
96
- output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009')
97
- output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'February')
98
- output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '29')
97
+ person.birth_date = nil
98
+ output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
99
+ output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
100
+ output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February')
101
+ output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29')
99
102
  end
100
103
 
101
104
  it "should override object values and use params if present" do
102
- params["employee"] = {
105
+ params["person"] = {
103
106
  "birth_date(1i)" => 2009,
104
107
  "birth_date(2i)" => 2,
105
108
  "birth_date(3i)" => 29,
106
109
  }
107
- employee.birth_date = "2009-03-01"
108
- output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
109
- output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009')
110
- output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'February')
111
- output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '29')
110
+ person.birth_date = "2009-03-01"
111
+ output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
112
+ output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
113
+ output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February')
114
+ output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29')
112
115
  end
113
116
 
114
117
  it "should select attribute values from object if no params" do
115
- employee.birth_date = "2009-01-02"
116
- output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
117
- output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009')
118
- output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'January')
119
- output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '2')
118
+ person.birth_date = "2009-01-02"
119
+ output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
120
+ output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
121
+ output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January')
122
+ output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2')
120
123
  end
121
124
 
122
125
  it "should select attribute values if params does not contain attribute params" do
123
- employee.birth_date = "2009-01-02"
124
- params["employee"] = { }
125
- output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
126
- output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009')
127
- output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'January')
128
- output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '2')
126
+ person.birth_date = "2009-01-02"
127
+ params["person"] = { }
128
+ output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
129
+ output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
130
+ output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January')
131
+ output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2')
129
132
  end
130
133
 
131
134
  it "should not select values when attribute value is nil and has no param values" do
132
- employee.birth_date = nil
133
- output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
134
- output.should_not have_tag('select[id=employee_birth_date_1i] option[selected=selected]')
135
- output.should_not have_tag('select[id=employee_birth_date_2i] option[selected=selected]')
136
- output.should_not have_tag('select[id=employee_birth_date_3i] option[selected=selected]')
135
+ person.birth_date = nil
136
+ output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
137
+ output.should_not have_tag('select[id=person_birth_date_1i] option[selected=selected]')
138
+ output.should_not have_tag('select[id=person_birth_date_2i] option[selected=selected]')
139
+ output.should_not have_tag('select[id=person_birth_date_3i] option[selected=selected]')
137
140
  end
138
141
  end
139
142
 
@@ -143,7 +146,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
143
146
  end
144
147
 
145
148
  it "should use param values when attribute is nil" do
146
- params["employee"] = {
149
+ params["person"] = {
147
150
  "birth_time(1i)" => 2000,
148
151
  "birth_time(2i)" => 1,
149
152
  "birth_time(3i)" => 1,
@@ -151,28 +154,31 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
151
154
  "birth_time(5i)" => 13,
152
155
  "birth_time(6i)" => 14,
153
156
  }
154
- employee.birth_time = nil
155
- output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true)
156
- output.should have_tag('select[id=employee_birth_time_4i] option[selected=selected]', '12')
157
- output.should have_tag('select[id=employee_birth_time_5i] option[selected=selected]', '13')
158
- output.should have_tag('select[id=employee_birth_time_6i] option[selected=selected]', '14')
157
+ person.birth_time = nil
158
+ output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
159
+ output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12')
160
+ output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13')
161
+ output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14')
159
162
  end
160
163
 
161
164
  it "should select attribute values from object if no params" do
162
- employee.birth_time = "2000-01-01 12:13:14"
163
- output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true)
164
- output.should have_tag('select[id=employee_birth_time_4i] option[selected=selected]', '12')
165
- output.should have_tag('select[id=employee_birth_time_5i] option[selected=selected]', '13')
166
- output.should have_tag('select[id=employee_birth_time_6i] option[selected=selected]', '14')
165
+ person.birth_time = "2000-01-01 12:13:14"
166
+ output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
167
+ output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12')
168
+ output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13')
169
+ output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14')
167
170
  end
168
171
 
169
172
  it "should not select values when attribute value is nil and has no param values" do
170
- employee.birth_time = nil
171
- output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true)
172
- output.should_not have_tag('select[id=employee_birth_time_4i] option[selected=selected]')
173
- output.should_not have_tag('select[id=employee_birth_time_5i] option[selected=selected]')
174
- output.should_not have_tag('select[id=employee_birth_time_6i] option[selected=selected]')
173
+ person.birth_time = nil
174
+ output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
175
+ output.should_not have_tag('select[id=person_birth_time_4i] option[selected=selected]')
176
+ output.should_not have_tag('select[id=person_birth_time_5i] option[selected=selected]')
177
+ output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]')
175
178
  end
176
179
  end
177
180
 
181
+ after :all do
182
+ ValidatesTimeliness.use_plugin_parser = false
183
+ end
178
184
  end
@@ -14,7 +14,7 @@ describe ValidatesTimeliness::HelperMethods do
14
14
  end
15
15
 
16
16
  it 'should validate instance when validation method called' do
17
- r = Employee.new
17
+ r = Person.new
18
18
  r.validates_date :birth_date
19
19
  r.errors[:birth_date].should_not be_empty
20
20
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness, 'ActiveRecord' do
4
+ it 'should define _timeliness_raw_value_for instance method' do
5
+ Employee.instance_methods.should include('_timeliness_raw_value_for')
6
+ end
7
+
8
+ context "attribute write method" do
9
+ class EmployeeWithCache < ActiveRecord::Base
10
+ set_table_name 'employees'
11
+ validates_datetime :birth_datetime
12
+ end
13
+
14
+ it 'should cache attribute raw value' do
15
+ r = EmployeeWithCache.new
16
+ r.birth_datetime = date_string = '2010-01-01'
17
+ r._timeliness_raw_value_for(:birth_datetime).should == date_string
18
+ end
19
+
20
+ context "with plugin parser" do
21
+ class EmployeeWithParser < ActiveRecord::Base
22
+ set_table_name 'employees'
23
+ validates_date :birth_date
24
+ validates_datetime :birth_datetime
25
+ end
26
+
27
+ before :all do
28
+ ValidatesTimeliness.use_plugin_parser = true
29
+ end
30
+
31
+ it 'should parse a string value' do
32
+ ValidatesTimeliness::Parser.should_receive(:parse)
33
+ r = EmployeeWithParser.new
34
+ r.birth_date = '2010-01-01'
35
+ end
36
+
37
+ it 'should parse string as current timezone' do
38
+ r = EmployeeWithParser.new
39
+ r.birth_datetime = '2010-01-01 12:00'
40
+ r.birth_datetime.zone == Time.zone.name
41
+ end
42
+
43
+ after :all do
44
+ ValidatesTimeliness.use_plugin_parser = false
45
+ end
46
+ end
47
+ end
48
+
49
+ context "before_type_cast method" do
50
+ it 'should be defined on class if ORM supports it' do
51
+ Employee.instance_methods(false).should include("birth_datetime_before_type_cast")
52
+ end
53
+
54
+ it 'should return original value' do
55
+ r = Employee.new
56
+ r.birth_datetime = date_string = '2010-01-01'
57
+ r.birth_datetime_before_type_cast.should == date_string
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ require 'mongoid'
4
+ require 'validates_timeliness/orm/mongoid'
5
+
6
+ Mongoid.configure do |config|
7
+ name = "validates_timeliness_test"
8
+ host = "localhost"
9
+ config.master = Mongo::Connection.new.db(name)
10
+ config.persist_in_safe_mode = false
11
+ end
12
+
13
+ describe ValidatesTimeliness, 'Mongoid' do
14
+
15
+ class Article
16
+ ::ValidatesTimeliness.use_plugin_parser = true
17
+ include Mongoid::Document
18
+ field :publish_date, :type => Date
19
+ field :publish_time, :type => Time
20
+ field :publish_datetime, :type => DateTime
21
+ validates_date :publish_date
22
+ validates_time :publish_time
23
+ validates_datetime :publish_datetime
24
+ ::ValidatesTimeliness.use_plugin_parser = false
25
+ end
26
+
27
+ it 'should define _timeliness_raw_value_for instance method' do
28
+ Article.instance_methods.should include('_timeliness_raw_value_for')
29
+ end
30
+
31
+ context "attribute write method" do
32
+ it 'should cache attribute raw value' do
33
+ r = Article.new
34
+ r.publish_datetime = date_string = '2010-01-01'
35
+ r._timeliness_raw_value_for(:publish_datetime).should == date_string
36
+ end
37
+
38
+ context "with plugin parser" do
39
+ before :all do
40
+ ValidatesTimeliness.use_plugin_parser = true
41
+ end
42
+
43
+ it 'should parse a string value' do
44
+ ValidatesTimeliness::Parser.should_receive(:parse)
45
+ r = Article.new
46
+ r.publish_date = '2010-01-01'
47
+ end
48
+
49
+ it 'should parse string into Time value' do
50
+ r = Article.new
51
+ r.publish_datetime = '2010-01-01 12:00'
52
+ r.publish_datetime.should == Time.utc(2010,1,1,12,0)
53
+ end
54
+
55
+ after :all do
56
+ ValidatesTimeliness.use_plugin_parser = false
57
+ end
58
+ end
59
+ end
60
+
61
+ context "before_type_cast method" do
62
+ it 'should not be defined if ORM does not support it' do
63
+ Article.instance_methods(false).should_not include("birth_datetime_before_type_cast")
64
+ end
65
+ end
66
+ end
@@ -99,18 +99,28 @@ describe ValidatesTimeliness::Validator do
99
99
  end
100
100
 
101
101
  describe ":format option" do
102
+ class PersonWithFormatOption
103
+ include TestModel
104
+ self.model_attributes = :birth_date, :birth_time, :birth_datetime
105
+ validates_date :birth_date, :format => 'dd-mm-yyyy'
106
+ end
107
+
108
+ let(:person) { PersonWithFormatOption.new }
109
+
102
110
  before(:all) do
103
111
  ValidatesTimeliness.use_plugin_parser = true
104
112
  end
105
113
 
106
114
  it "should be valid when value matches format" do
107
- Person.validates_date :birth_date, :format => 'dd-mm-yyyy'
108
- valid!(:birth_date, '11-12-1913')
115
+ person.birth_date = '11-12-1913'
116
+ person.valid?
117
+ person.errors[:birth_date].should be_empty
109
118
  end
110
119
 
111
120
  it "should not be valid when value does not match format" do
112
- Person.validates_date :birth_date, :format => 'dd/mm/yyyy'
113
- invalid!(:birth_date, '1913-12-11', 'is not a valid date')
121
+ person.birth_date = '1913-12-11'
122
+ person.valid?
123
+ person.errors[:birth_date].should include('is not a valid date')
114
124
  end
115
125
 
116
126
  after(:all) do
@@ -2,16 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{validates_timeliness}
5
- s.version = "3.0.0.beta.2"
5
+ s.version = "3.0.0.beta.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Adam Meehan"]
9
- s.autorequire = %q{validates_timeliness}
10
- s.date = %q{2010-09-21}
9
+ s.date = %q{2010-09-22}
11
10
  s.description = %q{Date and time validation plugin for Rails which allows custom formats}
12
11
  s.email = %q{adam.meehan@gmail.com}
13
12
  s.extra_rdoc_files = ["README.rdoc", "LICENSE", "CHANGELOG"]
14
- s.files = ["validates_timeliness.gemspec", "LICENSE", "CHANGELOG", "README.rdoc", "Rakefile", "lib/generators", "lib/generators/validates_timeliness", "lib/generators/validates_timeliness/install_generator.rb", "lib/generators/validates_timeliness/templates", "lib/generators/validates_timeliness/templates/en.yml", "lib/generators/validates_timeliness/templates/validates_timeliness.rb", "lib/validates_timeliness", "lib/validates_timeliness/attribute_methods.rb", "lib/validates_timeliness/conversion.rb", "lib/validates_timeliness/extensions", "lib/validates_timeliness/extensions/date_time_select.rb", "lib/validates_timeliness/extensions/multiparameter_handler.rb", "lib/validates_timeliness/extensions.rb", "lib/validates_timeliness/helper_methods.rb", "lib/validates_timeliness/orms", "lib/validates_timeliness/orms/active_record.rb", "lib/validates_timeliness/parser.rb", "lib/validates_timeliness/validator.rb", "lib/validates_timeliness/version.rb", "lib/validates_timeliness.rb", "spec/model_helpers.rb", "spec/spec_helper.rb", "spec/test_model.rb", "spec/validates_timeliness", "spec/validates_timeliness/attribute_methods_spec.rb", "spec/validates_timeliness/conversion_spec.rb", "spec/validates_timeliness/extensions", "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/parser_spec.rb", "spec/validates_timeliness/validator", "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"]
13
+ s.files = ["validates_timeliness.gemspec", "LICENSE", "CHANGELOG", "README.rdoc", "Rakefile", "lib/generators", "lib/generators/validates_timeliness", "lib/generators/validates_timeliness/install_generator.rb", "lib/generators/validates_timeliness/templates", "lib/generators/validates_timeliness/templates/en.yml", "lib/generators/validates_timeliness/templates/validates_timeliness.rb", "lib/validates_timeliness", "lib/validates_timeliness/attribute_methods.rb", "lib/validates_timeliness/conversion.rb", "lib/validates_timeliness/extensions", "lib/validates_timeliness/extensions/date_time_select.rb", "lib/validates_timeliness/extensions/multiparameter_handler.rb", "lib/validates_timeliness/extensions.rb", "lib/validates_timeliness/helper_methods.rb", "lib/validates_timeliness/orm", "lib/validates_timeliness/orm/active_record.rb", "lib/validates_timeliness/orm/mongoid.rb", "lib/validates_timeliness/parser.rb", "lib/validates_timeliness/validator.rb", "lib/validates_timeliness/version.rb", "lib/validates_timeliness.rb", "spec/model_helpers.rb", "spec/spec_helper.rb", "spec/test_model.rb", "spec/validates_timeliness", "spec/validates_timeliness/attribute_methods_spec.rb", "spec/validates_timeliness/conversion_spec.rb", "spec/validates_timeliness/extensions", "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", "spec/validates_timeliness/orm/active_record_spec.rb", "spec/validates_timeliness/orm/mongoid_spec.rb", "spec/validates_timeliness/parser_spec.rb", "spec/validates_timeliness/validator", "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"]
15
14
  s.homepage = %q{http://github.com/adzap/validates_timeliness}
16
15
  s.require_paths = ["lib"]
17
16
  s.rubyforge_project = %q{validates_timeliness}
metadata CHANGED
@@ -1,23 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_timeliness
3
3
  version: !ruby/object:Gem::Version
4
- hash: 62196423
4
+ hash: 62196421
5
5
  prerelease: true
6
6
  segments:
7
7
  - 3
8
8
  - 0
9
9
  - 0
10
10
  - beta
11
- - 2
12
- version: 3.0.0.beta.2
11
+ - 3
12
+ version: 3.0.0.beta.3
13
13
  platform: ruby
14
14
  authors:
15
15
  - Adam Meehan
16
- autorequire: validates_timeliness
16
+ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-09-21 00:00:00 +10:00
20
+ date: 2010-09-22 00:00:00 +10:00
21
21
  default_executable:
22
22
  dependencies: []
23
23
 
@@ -46,7 +46,8 @@ files:
46
46
  - lib/validates_timeliness/extensions/multiparameter_handler.rb
47
47
  - lib/validates_timeliness/extensions.rb
48
48
  - lib/validates_timeliness/helper_methods.rb
49
- - lib/validates_timeliness/orms/active_record.rb
49
+ - lib/validates_timeliness/orm/active_record.rb
50
+ - lib/validates_timeliness/orm/mongoid.rb
50
51
  - lib/validates_timeliness/parser.rb
51
52
  - lib/validates_timeliness/validator.rb
52
53
  - lib/validates_timeliness/version.rb
@@ -59,6 +60,8 @@ files:
59
60
  - spec/validates_timeliness/extensions/date_time_select_spec.rb
60
61
  - spec/validates_timeliness/extensions/multiparameter_handler_spec.rb
61
62
  - spec/validates_timeliness/helper_methods_spec.rb
63
+ - spec/validates_timeliness/orm/active_record_spec.rb
64
+ - spec/validates_timeliness/orm/mongoid_spec.rb
62
65
  - spec/validates_timeliness/parser_spec.rb
63
66
  - spec/validates_timeliness/validator/after_spec.rb
64
67
  - spec/validates_timeliness/validator/before_spec.rb