validates_timeliness 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ = 1.1.2 [2009-01-12]
2
+ - Fixed bugs
3
+ - matcher failing for custom error message without interpolation keys using I18n
4
+ - validator custom error messages not being extracted
5
+
1
6
  = 1.1.1 [2009-01-03]
2
7
  - Fixed bug in matcher for options local variable
3
8
 
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'date'
5
5
  require 'spec/rake/spectask'
6
6
 
7
7
  GEM = "validates_timeliness"
8
- GEM_VERSION = "1.1.1"
8
+ GEM_VERSION = "1.1.2"
9
9
  AUTHOR = "Adam Meehan"
10
10
  EMAIL = "adam.meehan@gmail.com"
11
11
  HOMEPAGE = "http://github.com/adzap/validates_timeliness"
data/TODO CHANGED
@@ -1,7 +1,5 @@
1
1
  - :format option
2
2
  - :with_date and :with_time options
3
- - Merb and Data Mapper support
4
- - does it have before_type_cast
5
- - timezone handling
6
- - view helper support
7
3
  - valid formats could come from locale file
4
+ - formats to use month and day names from i18n
5
+ - add replace_formats instead add_formats :before
@@ -10,16 +10,14 @@ module ValidatesTimeliness
10
10
  # string values.
11
11
  #
12
12
  class Formats
13
- cattr_accessor :time_formats
14
- cattr_accessor :date_formats
15
- cattr_accessor :datetime_formats
16
-
17
- cattr_accessor :time_expressions
18
- cattr_accessor :date_expressions
19
- cattr_accessor :datetime_expressions
20
-
21
- cattr_accessor :format_tokens
22
- cattr_accessor :format_proc_args
13
+ cattr_accessor :time_formats,
14
+ :date_formats,
15
+ :datetime_formats,
16
+ :time_expressions,
17
+ :date_expressions,
18
+ :datetime_expressions,
19
+ :format_tokens,
20
+ :format_proc_args
23
21
 
24
22
  # Format tokens:
25
23
  # y = year
@@ -139,13 +137,13 @@ module ValidatesTimeliness
139
137
  # should just be the arg name.
140
138
  #
141
139
  @@format_proc_args = {
142
- :year => [0, 'y', 'unambiguous_year(y)'],
143
- :month => [1, 'm', 'month_index(m)'],
144
- :day => [2, 'd', 'd'],
145
- :hour => [3, 'h', 'full_hour(h,md)'],
146
- :min => [4, 'n', 'n'],
147
- :sec => [5, 's', 's'],
148
- :usec => [6, 'u', 'microseconds(u)'],
140
+ :year => [0, 'y', 'unambiguous_year(y)'],
141
+ :month => [1, 'm', 'month_index(m)'],
142
+ :day => [2, 'd', 'd'],
143
+ :hour => [3, 'h', 'full_hour(h,md)'],
144
+ :min => [4, 'n', 'n'],
145
+ :sec => [5, 's', 's'],
146
+ :usec => [6, 'u', 'microseconds(u)'],
149
147
  :meridian => [nil, 'md', nil]
150
148
  }
151
149
 
@@ -165,6 +163,7 @@ module ValidatesTimeliness
165
163
  return string unless string.is_a?(String)
166
164
 
167
165
  expressions = expression_set(type, string)
166
+ # TODO cleanup using select
168
167
  time_array = nil
169
168
  expressions.each do |(regexp, processor)|
170
169
  regexp = strict || type == :datetime ? /\A#{regexp}\Z/ : (type == :date ? /\A#{regexp}/ : /#{regexp}\Z/)
@@ -173,7 +172,7 @@ module ValidatesTimeliness
173
172
  break
174
173
  end
175
174
  end
176
- return time_array
175
+ time_array
177
176
  end
178
177
 
179
178
  # Delete formats of specified type. Error raised if format not found.
@@ -223,7 +222,7 @@ module ValidatesTimeliness
223
222
  def format_expression_generator(string_format)
224
223
  regexp = string_format.dup
225
224
  order = {}
226
- regexp.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes ]/
225
+ regexp.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes
227
226
 
228
227
  format_tokens.each do |token|
229
228
  token_name = token.keys.first
@@ -260,7 +259,7 @@ module ValidatesTimeliness
260
259
  end
261
260
 
262
261
  def compile_formats(formats)
263
- formats.collect { |format| regexp, format_proc = format_expression_generator(format) }
262
+ formats.map { |format| regexp, format_proc = format_expression_generator(format) }
264
263
  end
265
264
 
266
265
  # Pick expression set and combine date and datetimes for
@@ -300,6 +299,22 @@ module ValidatesTimeliness
300
299
  return month.to_i if month.to_i.nonzero?
301
300
  Date::ABBR_MONTHNAMES.index(month.capitalize) || Date::MONTHNAMES.index(month.capitalize)
302
301
  end
302
+
303
+ def month_names
304
+ @@month_names = if defined?(I18n)
305
+ I18n('dates.months')
306
+ else
307
+ Date::MONTHNAMES
308
+ end
309
+ end
310
+
311
+ def abbreviated_month_names
312
+ @@abbreviated_month_names = if defined?(I18n)
313
+ I18n('dates.months')
314
+ else
315
+ Date::ABBR_MONTHNAMES
316
+ end
317
+ end
303
318
 
304
319
  def microseconds(usec)
305
320
  (".#{usec}".to_f * 1_000_000).to_i
@@ -123,7 +123,8 @@ module Spec
123
123
  restriction = [restriction] unless restriction.is_a?(Array)
124
124
  restriction.map! {|r| @validator.send(:type_cast_value, r) }
125
125
  interpolate = @validator.send(:interpolation_values, option, restriction )
126
- if defined?(I18n)
126
+ # get I18n message if defined and has interpolation keys in msg
127
+ if defined?(I18n) && !@validator.send(:custom_error_messages).include?(option)
127
128
  msg = @record.errors.generate_message(@expected, option, interpolate)
128
129
  else
129
130
  msg = msg % interpolate
@@ -115,7 +115,7 @@ module ValidatesTimeliness
115
115
  return @custom_error_messages if defined?(@custom_error_messages)
116
116
  @custom_error_messages = configuration.inject({}) {|msgs, (k, v)|
117
117
  if md = /(.*)_message$/.match(k.to_s)
118
- msgs[md[0].to_sym] = v
118
+ msgs[md[1].to_sym] = v
119
119
  end
120
120
  msgs
121
121
  }
@@ -5,27 +5,39 @@ end
5
5
 
6
6
  class WithValidation < Person
7
7
  validates_date :birth_date,
8
- :before => '2000-01-10', :after => '2000-01-01',
9
- :on_or_before => '2000-01-09', :on_or_after => '2000-01-02',
8
+ :before => '2000-01-10',
9
+ :after => '2000-01-01',
10
+ :on_or_before => '2000-01-09',
11
+ :on_or_after => '2000-01-02',
10
12
  :between => ['2000-01-01', '2000-01-03']
11
13
 
12
14
  validates_time :birth_time,
13
- :before => '23:00', :after => '09:00',
14
- :on_or_before => '22:00', :on_or_after => '10:00',
15
+ :before => '23:00',
16
+ :after => '09:00',
17
+ :on_or_before => '22:00',
18
+ :on_or_after => '10:00',
15
19
  :between => ['09:00', '17:00']
20
+
16
21
  validates_datetime :birth_date_and_time,
17
- :before => '2000-01-10 23:00', :after => '2000-01-01 09:00',
18
- :on_or_before => '2000-01-09 23:00', :on_or_after => '2000-01-02 09:00',
22
+ :before => '2000-01-10 23:00',
23
+ :after => '2000-01-01 09:00',
24
+ :on_or_before => '2000-01-09 23:00',
25
+ :on_or_after => '2000-01-02 09:00',
19
26
  :between => ['2000-01-01 09:00', '2000-01-01 17:00']
20
27
 
21
28
  end
22
29
 
23
30
  class CustomMessages < Person
24
- validates_date :birth_date, :invalid_date_message => 'is not really a date',
25
- :before => '2000-01-10', :before_message => 'is too late',
26
- :after => '2000-01-01', :after_message => 'is too early',
27
- :on_or_before=> '2000-01-09', :on_or_before_message => 'is just too late',
28
- :on_or_after => '2000-01-02', :on_or_after_message => 'is just too early'
31
+ validates_date :birth_date,
32
+ :invalid_date_message => 'is not really a date',
33
+ :before => '2000-01-10',
34
+ :before_message => 'is too late',
35
+ :after => '2000-01-01',
36
+ :after_message => 'is too early',
37
+ :on_or_before => '2000-01-09',
38
+ :on_or_before_message => 'is just too late',
39
+ :on_or_after => '2000-01-02',
40
+ :on_or_after_message => 'is just too early'
29
41
  end
30
42
 
31
43
  describe "ValidateTimeliness matcher" do
@@ -363,6 +363,40 @@ describe ValidatesTimeliness::Validator do
363
363
  end
364
364
  end
365
365
 
366
+ describe "custom_error_messages" do
367
+ it "should return hash of custom error messages from configuration with _message truncated from keys" do
368
+ configure_validator(:type => :date, :invalid_date_message => 'thats no date')
369
+ validator.send(:custom_error_messages)[:invalid_date].should == 'thats no date'
370
+ end
371
+
372
+ it "should return empty hash if no custom error messages in configuration" do
373
+ configure_validator(:type => :date)
374
+ validator.send(:custom_error_messages).should be_empty
375
+ end
376
+ end
377
+
378
+ describe "interpolation_values" do
379
+ if defined?(I18n)
380
+ it "should return hash of interpolation keys with restriction values" do
381
+ before = '1900-01-01'
382
+ configure_validator(:type => :date, :before => before)
383
+ validator.send(:interpolation_values, :before, before.to_date).should == {:restriction => before}
384
+ end
385
+
386
+ it "should return empty hash if no interpolation keys are in message" do
387
+ before = '1900-01-01'
388
+ configure_validator(:type => :date, :before => before, :before_message => 'too late')
389
+ validator.send(:interpolation_values, :before, before.to_date).should be_empty
390
+ end
391
+ else
392
+ it "should return array of interpolation values" do
393
+ before = '1900-01-01'
394
+ configure_validator(:type => :date, :before => before)
395
+ validator.send(:interpolation_values, :before, before.to_date).should == [before]
396
+ end
397
+ end
398
+ end
399
+
366
400
  describe "restriction errors" do
367
401
  before :each do
368
402
  configure_validator(:type => :date, :before => lambda { raise })
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_timeliness
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
@@ -9,7 +9,7 @@ autorequire: validates_timeliness
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-03 00:00:00 +11:00
12
+ date: 2009-01-12 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15