validates_timeliness 2.3.2 → 3.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +12 -4
- data/LICENSE +1 -1
- data/README.rdoc +138 -280
- data/Rakefile +30 -16
- data/lib/generators/validates_timeliness/install_generator.rb +17 -0
- data/lib/generators/validates_timeliness/templates/en.yml +16 -0
- data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +22 -0
- data/lib/validates_timeliness.rb +46 -52
- data/lib/validates_timeliness/attribute_methods.rb +51 -0
- data/lib/validates_timeliness/conversion.rb +69 -0
- data/lib/validates_timeliness/extensions.rb +14 -0
- data/lib/validates_timeliness/extensions/date_time_select.rb +45 -0
- data/lib/validates_timeliness/extensions/multiparameter_handler.rb +31 -0
- data/lib/validates_timeliness/helper_methods.rb +41 -0
- data/lib/validates_timeliness/orms/active_record.rb +14 -0
- data/lib/validates_timeliness/parser.rb +389 -17
- data/lib/validates_timeliness/validator.rb +37 -200
- data/lib/validates_timeliness/version.rb +1 -1
- data/spec/model_helpers.rb +27 -0
- data/spec/spec_helper.rb +74 -43
- data/spec/test_model.rb +56 -0
- data/spec/validates_timeliness/attribute_methods_spec.rb +36 -0
- data/spec/validates_timeliness/conversion_spec.rb +204 -0
- data/spec/validates_timeliness/extensions/date_time_select_spec.rb +178 -0
- data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +21 -0
- data/spec/validates_timeliness/helper_methods_spec.rb +36 -0
- data/spec/{formats_spec.rb → validates_timeliness/parser_spec.rb} +105 -71
- data/spec/validates_timeliness/validator/after_spec.rb +59 -0
- data/spec/validates_timeliness/validator/before_spec.rb +59 -0
- data/spec/validates_timeliness/validator/is_at_spec.rb +63 -0
- data/spec/validates_timeliness/validator/on_or_after_spec.rb +59 -0
- data/spec/validates_timeliness/validator/on_or_before_spec.rb +59 -0
- data/spec/validates_timeliness/validator_spec.rb +172 -0
- data/validates_timeliness.gemspec +30 -0
- metadata +42 -40
- data/TODO +0 -8
- data/lib/validates_timeliness/action_view/instance_tag.rb +0 -52
- data/lib/validates_timeliness/active_record/attribute_methods.rb +0 -77
- data/lib/validates_timeliness/active_record/multiparameter_attributes.rb +0 -69
- data/lib/validates_timeliness/formats.rb +0 -368
- data/lib/validates_timeliness/locale/en.new.yml +0 -18
- data/lib/validates_timeliness/locale/en.old.yml +0 -18
- data/lib/validates_timeliness/matcher.rb +0 -1
- data/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb +0 -162
- data/lib/validates_timeliness/validation_methods.rb +0 -46
- data/spec/action_view/instance_tag_spec.rb +0 -194
- data/spec/active_record/attribute_methods_spec.rb +0 -157
- data/spec/active_record/multiparameter_attributes_spec.rb +0 -118
- data/spec/ginger_scenarios.rb +0 -19
- data/spec/parser_spec.rb +0 -65
- data/spec/resources/application.rb +0 -2
- data/spec/resources/person.rb +0 -3
- data/spec/resources/schema.rb +0 -10
- data/spec/resources/sqlite_patch.rb +0 -19
- data/spec/spec/rails/matchers/validate_timeliness_spec.rb +0 -245
- data/spec/time_travel/MIT-LICENSE +0 -20
- data/spec/time_travel/time_extensions.rb +0 -33
- data/spec/time_travel/time_travel.rb +0 -12
- data/spec/validator_spec.rb +0 -723
@@ -1,18 +0,0 @@
|
|
1
|
-
en:
|
2
|
-
activerecord:
|
3
|
-
errors:
|
4
|
-
messages:
|
5
|
-
invalid_date: "is not a valid date"
|
6
|
-
invalid_time: "is not a valid time"
|
7
|
-
invalid_datetime: "is not a valid datetime"
|
8
|
-
is_at: "must be at %{restriction}"
|
9
|
-
before: "must be before %{restriction}"
|
10
|
-
on_or_before: "must be on or before %{restriction}"
|
11
|
-
after: "must be after %{restriction}"
|
12
|
-
on_or_after: "must be on or after %{restriction}"
|
13
|
-
between: "must be between %{earliest} and %{latest}"
|
14
|
-
validates_timeliness:
|
15
|
-
error_value_formats:
|
16
|
-
date: '%Y-%m-%d'
|
17
|
-
time: '%H:%M:%S'
|
18
|
-
datetime: '%Y-%m-%d %H:%M:%S'
|
@@ -1,18 +0,0 @@
|
|
1
|
-
en:
|
2
|
-
activerecord:
|
3
|
-
errors:
|
4
|
-
messages:
|
5
|
-
invalid_date: "is not a valid date"
|
6
|
-
invalid_time: "is not a valid time"
|
7
|
-
invalid_datetime: "is not a valid datetime"
|
8
|
-
is_at: "must be at {{restriction}}"
|
9
|
-
before: "must be before {{restriction}}"
|
10
|
-
on_or_before: "must be on or before {{restriction}}"
|
11
|
-
after: "must be after {{restriction}}"
|
12
|
-
on_or_after: "must be on or after {{restriction}}"
|
13
|
-
between: "must be between {{earliest}} and {{latest}}"
|
14
|
-
validates_timeliness:
|
15
|
-
error_value_formats:
|
16
|
-
date: '%Y-%m-%d'
|
17
|
-
time: '%H:%M:%S'
|
18
|
-
datetime: '%Y-%m-%d %H:%M:%S'
|
@@ -1 +0,0 @@
|
|
1
|
-
require 'validates_timeliness/spec/rails/matchers/validate_timeliness'
|
@@ -1,162 +0,0 @@
|
|
1
|
-
module Spec
|
2
|
-
module Rails
|
3
|
-
module Matchers
|
4
|
-
class ValidateTimeliness
|
5
|
-
|
6
|
-
VALIDITY_TEST_VALUES = {
|
7
|
-
:date => {:pass => '2000-01-01', :fail => '2000-01-32'},
|
8
|
-
:time => {:pass => '12:00', :fail => '25:00'},
|
9
|
-
:datetime => {:pass => '2000-01-01 00:00:00', :fail => '2000-01-32 00:00:00'}
|
10
|
-
}
|
11
|
-
|
12
|
-
OPTION_TEST_SETTINGS = {
|
13
|
-
:is_at => { :method => :+, :modify_on => :invalid },
|
14
|
-
:before => { :method => :-, :modify_on => :valid },
|
15
|
-
:after => { :method => :+, :modify_on => :valid },
|
16
|
-
:on_or_before => { :method => :+, :modify_on => :invalid },
|
17
|
-
:on_or_after => { :method => :-, :modify_on => :invalid }
|
18
|
-
}
|
19
|
-
|
20
|
-
def initialize(attribute, options)
|
21
|
-
@expected, @options = attribute, options
|
22
|
-
@validator = ValidatesTimeliness::Validator.new(options)
|
23
|
-
end
|
24
|
-
|
25
|
-
def matches?(record)
|
26
|
-
@record = record
|
27
|
-
@type = @options[:type]
|
28
|
-
|
29
|
-
valid = test_validity
|
30
|
-
|
31
|
-
valid = test_option(:is_at) if valid && @options[:is_at]
|
32
|
-
valid = test_option(:before) if valid && @options[:before]
|
33
|
-
valid = test_option(:after) if valid && @options[:after]
|
34
|
-
valid = test_option(:on_or_before) if valid && @options[:on_or_before]
|
35
|
-
valid = test_option(:on_or_after) if valid && @options[:on_or_after]
|
36
|
-
valid = test_between if valid && @options[:between]
|
37
|
-
|
38
|
-
return valid
|
39
|
-
end
|
40
|
-
|
41
|
-
def failure_message
|
42
|
-
"expected model to validate #{@type} attribute #{@expected.inspect} with #{@last_failure}"
|
43
|
-
end
|
44
|
-
|
45
|
-
def negative_failure_message
|
46
|
-
"expected not to validate #{@type} attribute #{@expected.inspect}"
|
47
|
-
end
|
48
|
-
|
49
|
-
def description
|
50
|
-
"have validated #{@type} attribute #{@expected.inspect}"
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def test_validity
|
56
|
-
invalid_value = VALIDITY_TEST_VALUES[@type][:fail]
|
57
|
-
valid_value = parse_and_cast(VALIDITY_TEST_VALUES[@type][:pass])
|
58
|
-
error_matching(invalid_value, "invalid_#{@type}".to_sym) &&
|
59
|
-
no_error_matching(valid_value, "invalid_#{@type}".to_sym)
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_option(option)
|
63
|
-
settings = OPTION_TEST_SETTINGS[option]
|
64
|
-
boundary = parse_and_cast(@options[option])
|
65
|
-
|
66
|
-
method = settings[:method]
|
67
|
-
|
68
|
-
valid_value, invalid_value = if settings[:modify_on] == :valid
|
69
|
-
[ boundary.send(method, 1), boundary ]
|
70
|
-
else
|
71
|
-
[ boundary, boundary.send(method, 1) ]
|
72
|
-
end
|
73
|
-
|
74
|
-
error_matching(invalid_value, option) &&
|
75
|
-
no_error_matching(valid_value, option)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_before
|
79
|
-
before = parse_and_cast(@options[:before])
|
80
|
-
|
81
|
-
error_matching(before - 1, :before) &&
|
82
|
-
no_error_matching(before, :before)
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_between
|
86
|
-
between = parse_and_cast(@options[:between])
|
87
|
-
|
88
|
-
error_matching(between.first - 1, :between) &&
|
89
|
-
error_matching(between.last + 1, :between) &&
|
90
|
-
no_error_matching(between.first, :between) &&
|
91
|
-
no_error_matching(between.last, :between)
|
92
|
-
end
|
93
|
-
|
94
|
-
def parse_and_cast(value)
|
95
|
-
value = @validator.class.send(:evaluate_option_value, value, @type, @record)
|
96
|
-
@validator.class.send(:type_cast_value, value, @type)
|
97
|
-
end
|
98
|
-
|
99
|
-
def error_matching(value, option)
|
100
|
-
match = error_message_for(option)
|
101
|
-
@record.send("#{@expected}=", value)
|
102
|
-
@record.valid?
|
103
|
-
errors = @record.errors.on(@expected)
|
104
|
-
pass = [ errors ].flatten.any? {|error| /#{match}/ === error }
|
105
|
-
@last_failure = "error matching '#{match}' when value is #{format_value(value)}" unless pass
|
106
|
-
pass
|
107
|
-
end
|
108
|
-
|
109
|
-
def no_error_matching(value, option)
|
110
|
-
pass = !error_matching(value, option)
|
111
|
-
unless pass
|
112
|
-
error = error_message_for(option)
|
113
|
-
@last_failure = "no error matching '#{error}' when value is #{format_value(value)}"
|
114
|
-
end
|
115
|
-
pass
|
116
|
-
end
|
117
|
-
|
118
|
-
def error_message_for(message)
|
119
|
-
restriction = @validator.class.send(:evaluate_option_value, @validator.configuration[message], @type, @record)
|
120
|
-
|
121
|
-
if restriction
|
122
|
-
restriction = @validator.class.send(:type_cast_value, restriction, @type)
|
123
|
-
interpolate = @validator.send(:interpolation_values, message, restriction)
|
124
|
-
end
|
125
|
-
|
126
|
-
if defined?(I18n)
|
127
|
-
interpolate ||= {}
|
128
|
-
options = interpolate.merge(:default => @validator.send(:custom_error_messages)[message])
|
129
|
-
if defined?(ActiveRecord::Error)
|
130
|
-
ActiveRecord::Error.new(@record, @expected, message, options).message
|
131
|
-
else
|
132
|
-
@record.errors.generate_message(@expected, message, options)
|
133
|
-
end
|
134
|
-
else
|
135
|
-
interpolate ||= nil
|
136
|
-
@validator.error_messages[message] % interpolate
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def format_value(value)
|
141
|
-
return value if value.is_a?(String)
|
142
|
-
value.strftime(@validator.class.error_value_formats[@type])
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
def validate_date(attribute, options={})
|
147
|
-
options[:type] = :date
|
148
|
-
ValidateTimeliness.new(attribute, options)
|
149
|
-
end
|
150
|
-
|
151
|
-
def validate_time(attribute, options={})
|
152
|
-
options[:type] = :time
|
153
|
-
ValidateTimeliness.new(attribute, options)
|
154
|
-
end
|
155
|
-
|
156
|
-
def validate_datetime(attribute, options={})
|
157
|
-
options[:type] = :datetime
|
158
|
-
ValidateTimeliness.new(attribute, options)
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
module ValidatesTimeliness
|
2
|
-
module ValidationMethods
|
3
|
-
|
4
|
-
def self.included(base)
|
5
|
-
base.extend ClassMethods
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
|
10
|
-
def validates_time(*attr_names)
|
11
|
-
configuration = attr_names.extract_options!
|
12
|
-
configuration[:type] = :time
|
13
|
-
validates_timeliness_of(attr_names, configuration)
|
14
|
-
end
|
15
|
-
|
16
|
-
def validates_date(*attr_names)
|
17
|
-
configuration = attr_names.extract_options!
|
18
|
-
configuration[:type] = :date
|
19
|
-
validates_timeliness_of(attr_names, configuration)
|
20
|
-
end
|
21
|
-
|
22
|
-
def validates_datetime(*attr_names)
|
23
|
-
configuration = attr_names.extract_options!
|
24
|
-
configuration[:type] = :datetime
|
25
|
-
validates_timeliness_of(attr_names, configuration)
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def validates_timeliness_of(attr_names, configuration)
|
31
|
-
validator = ValidatesTimeliness::Validator.new(configuration.symbolize_keys)
|
32
|
-
|
33
|
-
# bypass handling of allow_nil and allow_blank to validate raw value
|
34
|
-
configuration.delete(:allow_nil)
|
35
|
-
configuration.delete(:allow_blank)
|
36
|
-
validates_each(attr_names, configuration) do |record, attr_name, value|
|
37
|
-
validator.call(record, attr_name, value)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
ActiveRecord::Base.send(:include, ValidatesTimeliness::ValidationMethods)
|
@@ -1,194 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe 'ValidatesTimeliness::ActionView::InstanceTag' do
|
4
|
-
include ActionView::Helpers::DateHelper
|
5
|
-
include ActionController::Assertions::SelectorAssertions
|
6
|
-
|
7
|
-
before do
|
8
|
-
@person = Person.new
|
9
|
-
end
|
10
|
-
|
11
|
-
def params
|
12
|
-
@params ||= {}
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "datetime_select" do
|
16
|
-
it "should use param values when attribute is nil" do
|
17
|
-
params["person"] = {
|
18
|
-
"birth_date_and_time(1i)" => 2009,
|
19
|
-
"birth_date_and_time(2i)" => 2,
|
20
|
-
"birth_date_and_time(3i)" => 29,
|
21
|
-
"birth_date_and_time(4i)" => 12,
|
22
|
-
"birth_date_and_time(5i)" => 13,
|
23
|
-
"birth_date_and_time(6i)" => 14,
|
24
|
-
}
|
25
|
-
@person.birth_date_and_time = nil
|
26
|
-
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
27
|
-
output.should have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]', '2009')
|
28
|
-
output.should have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]', 'February')
|
29
|
-
output.should have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]', '29')
|
30
|
-
output.should have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]', '12')
|
31
|
-
output.should have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]', '13')
|
32
|
-
output.should have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]', '14')
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should override object values and use params if present" do
|
36
|
-
params["person"] = {
|
37
|
-
"birth_date_and_time(1i)" => 2009,
|
38
|
-
"birth_date_and_time(2i)" => 2,
|
39
|
-
"birth_date_and_time(3i)" => 29,
|
40
|
-
"birth_date_and_time(4i)" => 13,
|
41
|
-
"birth_date_and_time(5i)" => 14,
|
42
|
-
"birth_date_and_time(6i)" => 15,
|
43
|
-
}
|
44
|
-
@person.birth_date_and_time = "2009-03-01 13:14:15"
|
45
|
-
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
46
|
-
output.should have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]', '2009')
|
47
|
-
output.should have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]', 'February')
|
48
|
-
output.should have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]', '29')
|
49
|
-
output.should have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]', '13')
|
50
|
-
output.should have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]', '14')
|
51
|
-
output.should have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]', '15')
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should select attribute values from object if no params" do
|
55
|
-
@person.birth_date_and_time = "2009-01-02 13:14:15"
|
56
|
-
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
57
|
-
output.should have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]', '2009')
|
58
|
-
output.should have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]', 'January')
|
59
|
-
output.should have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]', '2')
|
60
|
-
output.should have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]', '13')
|
61
|
-
output.should have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]', '14')
|
62
|
-
output.should have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]', '15')
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should select attribute values if params does not contain attribute params" do
|
66
|
-
@person.birth_date_and_time = "2009-01-02 13:14:15"
|
67
|
-
params["person"] = { }
|
68
|
-
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
69
|
-
output.should have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]', '2009')
|
70
|
-
output.should have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]', 'January')
|
71
|
-
output.should have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]', '2')
|
72
|
-
output.should have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]', '13')
|
73
|
-
output.should have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]', '14')
|
74
|
-
output.should have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]', '15')
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should not select values when attribute value is nil and has no param values" do
|
78
|
-
@person.birth_date_and_time = nil
|
79
|
-
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
80
|
-
output.should_not have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]')
|
81
|
-
output.should_not have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]')
|
82
|
-
output.should_not have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]')
|
83
|
-
output.should_not have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]')
|
84
|
-
output.should_not have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]')
|
85
|
-
output.should_not have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]')
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
describe "date_select" do
|
90
|
-
it "should use param values when attribute is nil" do
|
91
|
-
params["person"] = {
|
92
|
-
"birth_date(1i)" => 2009,
|
93
|
-
"birth_date(2i)" => 2,
|
94
|
-
"birth_date(3i)" => 29,
|
95
|
-
}
|
96
|
-
@person.birth_date = nil
|
97
|
-
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
98
|
-
output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
|
99
|
-
output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February')
|
100
|
-
output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29')
|
101
|
-
end
|
102
|
-
|
103
|
-
it "should override object values and use params if present" do
|
104
|
-
params["person"] = {
|
105
|
-
"birth_date(1i)" => 2009,
|
106
|
-
"birth_date(2i)" => 2,
|
107
|
-
"birth_date(3i)" => 29,
|
108
|
-
}
|
109
|
-
@person.birth_date = "2009-03-01"
|
110
|
-
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
111
|
-
output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
|
112
|
-
output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February')
|
113
|
-
output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29')
|
114
|
-
end
|
115
|
-
|
116
|
-
it "should select attribute values from object if no params" do
|
117
|
-
@person.birth_date = "2009-01-02"
|
118
|
-
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
119
|
-
output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
|
120
|
-
output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January')
|
121
|
-
output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2')
|
122
|
-
end
|
123
|
-
|
124
|
-
it "should select attribute values if params does not contain attribute params" do
|
125
|
-
@person.birth_date = "2009-01-02"
|
126
|
-
params["person"] = { }
|
127
|
-
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
128
|
-
output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
|
129
|
-
output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January')
|
130
|
-
output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2')
|
131
|
-
end
|
132
|
-
|
133
|
-
it "should not select values when attribute value is nil and has no param values" do
|
134
|
-
@person.birth_date = nil
|
135
|
-
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
136
|
-
output.should_not have_tag('select[id=person_birth_date_1i] option[selected=selected]')
|
137
|
-
output.should_not have_tag('select[id=person_birth_date_2i] option[selected=selected]')
|
138
|
-
output.should_not have_tag('select[id=person_birth_date_3i] option[selected=selected]')
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
describe "time_select" do
|
143
|
-
before :all do
|
144
|
-
Time.now = Time.mktime(2009,1,1)
|
145
|
-
end
|
146
|
-
|
147
|
-
it "should use param values when attribute is nil" do
|
148
|
-
params["person"] = {
|
149
|
-
"birth_time(1i)" => 2000,
|
150
|
-
"birth_time(2i)" => 1,
|
151
|
-
"birth_time(3i)" => 1,
|
152
|
-
"birth_time(4i)" => 12,
|
153
|
-
"birth_time(5i)" => 13,
|
154
|
-
"birth_time(6i)" => 14,
|
155
|
-
}
|
156
|
-
@person.birth_time = nil
|
157
|
-
output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
|
158
|
-
output.should have_tag('input[id=person_birth_time_1i][value=2000]')
|
159
|
-
output.should have_tag('input[id=person_birth_time_2i][value=1]')
|
160
|
-
output.should have_tag('input[id=person_birth_time_3i][value=1]')
|
161
|
-
output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12')
|
162
|
-
output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13')
|
163
|
-
output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14')
|
164
|
-
end
|
165
|
-
|
166
|
-
it "should select attribute values from object if no params" do
|
167
|
-
@person.birth_time = "13:14:15"
|
168
|
-
output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
|
169
|
-
output.should have_tag('input[id=person_birth_time_1i][value=2000]')
|
170
|
-
output.should have_tag('input[id=person_birth_time_2i][value=1]')
|
171
|
-
output.should have_tag('input[id=person_birth_time_3i][value=1]')
|
172
|
-
output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '13')
|
173
|
-
output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '14')
|
174
|
-
output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '15')
|
175
|
-
end
|
176
|
-
|
177
|
-
it "should not select values when attribute value is nil and has no param values" do
|
178
|
-
@person.birth_time = nil
|
179
|
-
output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
|
180
|
-
output.should have_tag('input[id=person_birth_time_1i][value=""]')
|
181
|
-
# Annoyingly these may or not have value attribute depending on rails version.
|
182
|
-
# output.should have_tag('input[id=person_birth_time_2i][value=""]')
|
183
|
-
# output.should have_tag('input[id=person_birth_time_3i][value=""]')
|
184
|
-
output.should_not have_tag('select[id=person_birth_time_4i] option[selected=selected]')
|
185
|
-
output.should_not have_tag('select[id=person_birth_time_5i] option[selected=selected]')
|
186
|
-
output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]')
|
187
|
-
end
|
188
|
-
|
189
|
-
after :all do
|
190
|
-
Time.now = nil
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
end
|
@@ -1,157 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
4
|
-
before do
|
5
|
-
@person = Person.new
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should define and call write method on first assign" do
|
9
|
-
Person.class_eval { @generated_methods = Set.new; @_defined_class_methods = nil }
|
10
|
-
Person.send(:undef_method, :birth_date=) if Person.instance_methods.include?('birth_date=')
|
11
|
-
person = Person.new
|
12
|
-
person.should_receive(:write_date_time_attribute)
|
13
|
-
person.birth_date = "2000-01-01"
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should call write_date_time_attribute when time attribute assigned value" do
|
17
|
-
@person.should_receive(:write_date_time_attribute)
|
18
|
-
@person.birth_time = "12:00"
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should call write_date_time_attribute when datetime attribute assigned value" do
|
22
|
-
@person.should_receive(:write_date_time_attribute)
|
23
|
-
@person.birth_date_and_time = "2000-01-01 12:00"
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should call parser on write for datetime attribute" do
|
27
|
-
ValidatesTimeliness::Parser.should_receive(:parse).once
|
28
|
-
@person.birth_date_and_time = "2000-01-01 02:03:04"
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should call parser on write for date attribute" do
|
32
|
-
ValidatesTimeliness::Parser.should_receive(:parse).once
|
33
|
-
@person.birth_date = "2000-01-01"
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should call parser on write for time attribute" do
|
37
|
-
ValidatesTimeliness::Parser.should_receive(:parse).once
|
38
|
-
@person.birth_time = "12:00"
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should return raw string value for attribute_before_type_cast when written as string" do
|
42
|
-
time_string = "2000-01-01 02:03:04"
|
43
|
-
@person.birth_date_and_time = time_string
|
44
|
-
@person.birth_date_and_time_before_type_cast.should == time_string
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should return Time object for attribute_before_type_cast when written as Time" do
|
48
|
-
@person.birth_date_and_time = Time.mktime(2000, 1, 1, 2, 3, 4)
|
49
|
-
@person.birth_date_and_time_before_type_cast.should be_kind_of(Time)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should return Time object for datetime attribute read method when assigned Time object" do
|
53
|
-
@person.birth_date_and_time = Time.now
|
54
|
-
@person.birth_date_and_time.should be_kind_of(Time)
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should return Time object for datetime attribute read method when assigned Date object" do
|
58
|
-
@person.birth_date_and_time = Date.today
|
59
|
-
@person.birth_date_and_time.should be_kind_of(Time)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should return Time object for datetime attribute read method when assigned string" do
|
63
|
-
@person.birth_date_and_time = "2000-01-01 02:03:04"
|
64
|
-
@person.birth_date_and_time.should be_kind_of(Time)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should return Date object for date attribute read method when assigned Date object" do
|
68
|
-
@person.birth_date = Date.today
|
69
|
-
@person.birth_date.should be_kind_of(Date)
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should return Date object for date attribute read method when assigned string" do
|
73
|
-
@person.birth_date = '2000-01-01'
|
74
|
-
@person.birth_date.should be_kind_of(Date)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should return nil when time is invalid" do
|
78
|
-
@person.birth_date_and_time = "2000-01-32 02:03:04"
|
79
|
-
@person.birth_date_and_time.should be_nil
|
80
|
-
end
|
81
|
-
|
82
|
-
it "should not save invalid date value to database" do
|
83
|
-
time_string = "2000-01-32 02:03:04"
|
84
|
-
@person.birth_date_and_time = time_string
|
85
|
-
@person.save
|
86
|
-
@person.reload
|
87
|
-
@person.birth_date_and_time_before_type_cast.should be_nil
|
88
|
-
end
|
89
|
-
|
90
|
-
if RAILS_VER < '2.1'
|
91
|
-
|
92
|
-
it "should return time object from database in default timezone" do
|
93
|
-
ActiveRecord::Base.default_timezone = :utc
|
94
|
-
time_string = "2000-01-01 09:00:00"
|
95
|
-
@person.birth_date_and_time = time_string
|
96
|
-
@person.save
|
97
|
-
@person.reload
|
98
|
-
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z').should == time_string + ' UTC'
|
99
|
-
end
|
100
|
-
|
101
|
-
else
|
102
|
-
|
103
|
-
it "should return stored time string as Time with correct timezone" do
|
104
|
-
Time.zone = 'Melbourne'
|
105
|
-
time_string = "2000-06-01 02:03:04"
|
106
|
-
@person.birth_date_and_time = time_string
|
107
|
-
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should return time object from database in correct timezone" do
|
111
|
-
Time.zone = 'Melbourne'
|
112
|
-
time_string = "2000-06-01 09:00:00"
|
113
|
-
@person.birth_date_and_time = time_string
|
114
|
-
@person.save
|
115
|
-
@person.reload
|
116
|
-
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
it "should return correct date value after new value assigned" do
|
122
|
-
today = Date.today
|
123
|
-
tomorrow = Date.today + 1.day
|
124
|
-
@person.birth_date = today
|
125
|
-
@person.birth_date.should == today
|
126
|
-
@person.birth_date = tomorrow
|
127
|
-
@person.birth_date.should == tomorrow
|
128
|
-
end
|
129
|
-
|
130
|
-
it "should update date attribute on existing object" do
|
131
|
-
today = Date.today
|
132
|
-
tomorrow = Date.today + 1.day
|
133
|
-
person = Person.create(:birth_date => today)
|
134
|
-
person.birth_date = tomorrow
|
135
|
-
person.save!
|
136
|
-
person.reload
|
137
|
-
person.birth_date.should == tomorrow
|
138
|
-
end
|
139
|
-
|
140
|
-
describe "attribute writer" do
|
141
|
-
|
142
|
-
it "should be able to be overridden in class" do
|
143
|
-
Person.class_eval { attr_accessor :birth_date }
|
144
|
-
person = Person.new
|
145
|
-
person.birth_date = 'overriden'
|
146
|
-
person.birth_date.should == 'overriden'
|
147
|
-
end
|
148
|
-
|
149
|
-
after do
|
150
|
-
Person.class_eval do
|
151
|
-
undef_method(:birth_date)
|
152
|
-
undef_method(:birth_date=)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
end
|