validates_and_formats_phones 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +20 -0
- data/Rakefile +1 -1
- data/lib/validates_and_formats_phones/validates_and_formats_phones.rb +5 -3
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -41,6 +41,7 @@ validation and formatting for the field :phone like so:
|
|
41
41
|
> person.save
|
42
42
|
> => false
|
43
43
|
|
44
|
+
|
44
45
|
You can pass your own fields like so:
|
45
46
|
|
46
47
|
class Person
|
@@ -65,6 +66,22 @@ You can also format and specify fields, like so:
|
|
65
66
|
validates_and_formats_phones :fax, mobile, '####-####'
|
66
67
|
end
|
67
68
|
|
69
|
+
|
70
|
+
= Additional configuration options
|
71
|
+
|
72
|
+
As of version 0.0.6 all configuration options available
|
73
|
+
to validates_each are also available to the phone validator.
|
74
|
+
For example:
|
75
|
+
|
76
|
+
class Person
|
77
|
+
validates_and_formats_phones :fax, '###-###-####', :if => :some_function_name
|
78
|
+
end
|
79
|
+
|
80
|
+
Here's a quick link to the validates_each documentation at the time of writing:
|
81
|
+
|
82
|
+
http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M002161
|
83
|
+
|
84
|
+
|
68
85
|
As a tiny added benefit, String is extended with a to_phone method.
|
69
86
|
|
70
87
|
> '123 456 7890'.to_phone
|
@@ -73,6 +90,9 @@ As a tiny added benefit, String is extended with a to_phone method.
|
|
73
90
|
> '12345678'.to_phone('####-####')
|
74
91
|
> => '1234-5678'
|
75
92
|
|
93
|
+
=Change Log
|
94
|
+
0.0.6
|
95
|
+
1. Add configuration options available in validates_each method.
|
76
96
|
If you find any bugs, please report them at our github page:
|
77
97
|
|
78
98
|
http://github.com/btelles-validates_and_formats_phones
|
data/Rakefile
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def self.extract_formats_and_fields(formats_and_fields)
|
9
|
+
options = {:on => :save, :allow_nil => false}
|
10
|
+
options.merge!(formats_and_fields.extract_options!)
|
9
11
|
formats = []
|
10
12
|
fields = []
|
11
13
|
formats_and_fields.each do |option|
|
@@ -15,17 +17,17 @@
|
|
15
17
|
end
|
16
18
|
formats << DEFAULT_FORMAT if formats.empty?
|
17
19
|
fields << :phone if fields.empty?
|
18
|
-
[formats, fields]
|
20
|
+
[formats, fields, options]
|
19
21
|
end
|
20
22
|
|
21
23
|
module ClassMethods
|
22
24
|
|
23
25
|
def validates_and_formats_phones(*args)
|
24
|
-
formats, fields = ValidatesAndFormatsPhones.extract_formats_and_fields(args)
|
26
|
+
formats, fields, options = ValidatesAndFormatsPhones.extract_formats_and_fields(args)
|
25
27
|
|
26
28
|
size_options = formats.collect {|format| format.count '#'}
|
27
29
|
|
28
|
-
validates_each
|
30
|
+
validates_each(fields, options) do |record, attr, value|
|
29
31
|
unless value.blank? || size_options.include?(value.scan(/\d/).size)
|
30
32
|
if size_options.size > 1
|
31
33
|
last = size_options.pop
|