vandrijevik-has_phone_number 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.markdown +53 -0
- data/Rakefile +23 -0
- data/lib/has_phone_number.rb +55 -0
- data/lib/has_phone_number/phone_number.rb +23 -0
- data/lib/has_phone_number/validateable.rb +31 -0
- data/test/config/boot.rb +110 -0
- data/test/config/database.yml +5 -0
- data/test/config/environment.rb +14 -0
- data/test/config/environments/test.rb +28 -0
- data/test/config/routes.rb +43 -0
- data/test/has_phone_number_test.rb +92 -0
- data/test/phone_number_test.rb +26 -0
- data/test/test_helper.rb +16 -0
- data/test/us_test.rb +60 -0
- metadata +66 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
HasPhoneNumber
|
2
|
+
==============
|
3
|
+
|
4
|
+
HasPhoneNumber makes phone number validations in ActiveRecord easy. Make sure your model
|
5
|
+
has a string attribute to store your phone number, pass it to `has_phone_number`, and
|
6
|
+
you're done!
|
7
|
+
|
8
|
+
class Person < ActiveRecord::Base
|
9
|
+
has_phone_number :cell_phone
|
10
|
+
end
|
11
|
+
|
12
|
+
This will ensure that the phone number in `cell_phone` is of a valid phone number
|
13
|
+
format, and invalidate Person if the phone number is invalid. It will also play
|
14
|
+
nice with `form_for`, so you can display the errors to your users.
|
15
|
+
|
16
|
+
Using Formats
|
17
|
+
-------------
|
18
|
+
|
19
|
+
By default, `has_phone_number` will validate against a US phone number format. For this,
|
20
|
+
all you need to do is:
|
21
|
+
|
22
|
+
class Person < ActiveRecord::Base
|
23
|
+
has_phone_number :cell_phone
|
24
|
+
end
|
25
|
+
|
26
|
+
If, however you want to validate against a different phone number format, you can:
|
27
|
+
|
28
|
+
class Office < ActiveRecord::Base
|
29
|
+
has_phone_number :main, :format => :us
|
30
|
+
has_phone_number :abroad, :format => :france
|
31
|
+
end
|
32
|
+
|
33
|
+
This will validate the `main` attribute against a US number format (included),
|
34
|
+
and `abroad` against a France number format (if you've specified it).
|
35
|
+
|
36
|
+
Specifying Formats
|
37
|
+
------------------
|
38
|
+
|
39
|
+
Format names and specifications are stored in the HasPhoneNumber::PhoneNumber.formats hash.
|
40
|
+
The key is a symbol (that can be used with the :format option to `has_phone_number`),
|
41
|
+
and the value is a regex that the attribute is matched against.
|
42
|
+
|
43
|
+
In a Rails project, you can specify additional formats a number of ways, but the cleanest is
|
44
|
+
to put your number formats in an intializer. For example, `config/initializers/phone_number_formats.rb`:
|
45
|
+
|
46
|
+
my_formats = {:my_number_format => /\d{10}/,
|
47
|
+
:no_area_code => /\d{3}-\d{4}/}
|
48
|
+
|
49
|
+
HasPhoneNumber::PhoneNumber.formats.update(my_formats)
|
50
|
+
|
51
|
+
Hope you like it!
|
52
|
+
|
53
|
+
Copyright (c) 2009 [Vladimir Andrijevik], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the has_phone_number plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the has_phone_number plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'HasPhoneNumber'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module HasPhoneNumber
|
2
|
+
autoload :PhoneNumber, 'has_phone_number/phone_number'
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def has_phone_number(*attr_names)
|
6
|
+
setup_number_formats_mapping
|
7
|
+
|
8
|
+
phone_number_format = attr_names.extract_options![:format]
|
9
|
+
record_number_format(attr_names, phone_number_format)
|
10
|
+
|
11
|
+
setup_aggregations(attr_names)
|
12
|
+
validates_as_phone_number attr_names.map {|attribute_name| "#{attribute_name}_as_phone_number".to_sym}
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def setup_number_formats_mapping
|
17
|
+
unless respond_to?(:number_formats) && respond_to?(:number_formats=)
|
18
|
+
class_inheritable_hash :number_formats
|
19
|
+
self.number_formats = {}
|
20
|
+
self.number_formats.default = :us
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def record_number_format(attr_names, format)
|
25
|
+
if format.present?
|
26
|
+
attr_names.each { |attribute_name| self.number_formats[attribute_name.to_sym] = format }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def setup_aggregations(attr_names)
|
31
|
+
attr_names.each do |attribute_name|
|
32
|
+
composed_of "#{attribute_name}_as_phone_number".intern,
|
33
|
+
:class_name => "HasPhoneNumber::PhoneNumber",
|
34
|
+
:mapping => [attribute_name, "number"],
|
35
|
+
:allow_nil => true,
|
36
|
+
:constructor => Proc.new { |phone_number| HasPhoneNumber::PhoneNumber.new(phone_number, number_formats[attribute_name.to_sym]) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def validates_as_phone_number(*attr_names)
|
41
|
+
configuration = { :on => :save }
|
42
|
+
configuration.update(attr_names.extract_options!)
|
43
|
+
|
44
|
+
validates_each(attr_names, configuration) do |record, attr_name, value|
|
45
|
+
unless value.nil? || value.valid?
|
46
|
+
original_attr_name = attr_name.to_s.gsub(/_as_phone_number$/, "").to_sym
|
47
|
+
record.errors.add(original_attr_name, :invalid, :default => configuration[:message], :value => value)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end # ClassMethods
|
52
|
+
|
53
|
+
end # HasPhoneNumber
|
54
|
+
|
55
|
+
ActiveRecord::Base.class_eval { extend HasPhoneNumber::ClassMethods } if defined?(ActiveRecord::Base)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module HasPhoneNumber
|
2
|
+
autoload :Validateable, File.join(File.dirname(__FILE__), "validateable")
|
3
|
+
|
4
|
+
class PhoneNumber
|
5
|
+
include Validateable
|
6
|
+
|
7
|
+
class_inheritable_hash :formats
|
8
|
+
self.formats = {:us => /^(\(\d{3}\) |\d{3}[- ])\d{3}[- ]?\d{4}$/}
|
9
|
+
|
10
|
+
attr_reader :number, :number_format
|
11
|
+
validates_each(:number) do |record, attr_name, value|
|
12
|
+
unless record.formats[record.number_format].match(value)
|
13
|
+
record.errors.add(attr_name, :invalid, :value => value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(number, number_format)
|
18
|
+
@number = number
|
19
|
+
@number_format = number_format
|
20
|
+
@errors = ActiveRecord::Errors.new(self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/base'
|
3
|
+
|
4
|
+
module Validateable
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
extend ClassMethods
|
8
|
+
include InstanceMethods
|
9
|
+
include ActiveRecord::Validations
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def self_and_descendants_from_active_record; [self]; end
|
15
|
+
def human_name; name.humanize; end
|
16
|
+
|
17
|
+
def human_attribute_name(attribute)
|
18
|
+
attribute.to_s.titleize
|
19
|
+
end
|
20
|
+
end # ClassMethods
|
21
|
+
|
22
|
+
module InstanceMethods
|
23
|
+
[:save, :save!, :update_attribute, :new_record?].each do |method_name|
|
24
|
+
define_method(method_name) {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def method_missing(symbol, *params)
|
28
|
+
symbol.to_s =~ /(.*)_before_type_cast$/ ? send($1) : super
|
29
|
+
end
|
30
|
+
end # InstanceMethods
|
31
|
+
end # Validateable
|
data/test/config/boot.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Don't change this file!
|
2
|
+
# Configure your app in config/environment.rb and config/environments/*.rb
|
3
|
+
|
4
|
+
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
|
5
|
+
|
6
|
+
module Rails
|
7
|
+
class << self
|
8
|
+
def boot!
|
9
|
+
unless booted?
|
10
|
+
preinitialize
|
11
|
+
pick_boot.run
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def booted?
|
16
|
+
defined? Rails::Initializer
|
17
|
+
end
|
18
|
+
|
19
|
+
def pick_boot
|
20
|
+
(vendor_rails? ? VendorBoot : GemBoot).new
|
21
|
+
end
|
22
|
+
|
23
|
+
def vendor_rails?
|
24
|
+
File.exist?("#{RAILS_ROOT}/vendor/rails")
|
25
|
+
end
|
26
|
+
|
27
|
+
def preinitialize
|
28
|
+
load(preinitializer_path) if File.exist?(preinitializer_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
def preinitializer_path
|
32
|
+
"#{RAILS_ROOT}/config/preinitializer.rb"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Boot
|
37
|
+
def run
|
38
|
+
load_initializer
|
39
|
+
Rails::Initializer.run(:set_load_path)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class VendorBoot < Boot
|
44
|
+
def load_initializer
|
45
|
+
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
|
46
|
+
Rails::Initializer.run(:install_gem_spec_stubs)
|
47
|
+
Rails::GemDependency.add_frozen_gem_path
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class GemBoot < Boot
|
52
|
+
def load_initializer
|
53
|
+
self.class.load_rubygems
|
54
|
+
load_rails_gem
|
55
|
+
require 'initializer'
|
56
|
+
end
|
57
|
+
|
58
|
+
def load_rails_gem
|
59
|
+
if version = self.class.gem_version
|
60
|
+
gem 'rails', version
|
61
|
+
else
|
62
|
+
gem 'rails'
|
63
|
+
end
|
64
|
+
rescue Gem::LoadError => load_error
|
65
|
+
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
|
66
|
+
exit 1
|
67
|
+
end
|
68
|
+
|
69
|
+
class << self
|
70
|
+
def rubygems_version
|
71
|
+
Gem::RubyGemsVersion rescue nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def gem_version
|
75
|
+
if defined? RAILS_GEM_VERSION
|
76
|
+
RAILS_GEM_VERSION
|
77
|
+
elsif ENV.include?('RAILS_GEM_VERSION')
|
78
|
+
ENV['RAILS_GEM_VERSION']
|
79
|
+
else
|
80
|
+
parse_gem_version(read_environment_rb)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def load_rubygems
|
85
|
+
require 'rubygems'
|
86
|
+
min_version = '1.3.1'
|
87
|
+
unless rubygems_version >= min_version
|
88
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
|
92
|
+
rescue LoadError
|
93
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
|
94
|
+
exit 1
|
95
|
+
end
|
96
|
+
|
97
|
+
def parse_gem_version(text)
|
98
|
+
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
def read_environment_rb
|
103
|
+
File.read("#{RAILS_ROOT}/config/environment.rb")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# All that for this:
|
110
|
+
Rails.boot!
|
@@ -0,0 +1,14 @@
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'test'
|
2
|
+
|
3
|
+
# Specifies gem version of Rails to use when vendor/rails is not present
|
4
|
+
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
|
5
|
+
|
6
|
+
# Bootstrap the Rails environment, frameworks, and default configuration
|
7
|
+
require File.join(File.dirname(__FILE__), 'boot')
|
8
|
+
|
9
|
+
Rails::Initializer.run do |config|
|
10
|
+
config.frameworks -= [ :active_resource, :action_mailer, :action_controller, :action_view ]
|
11
|
+
|
12
|
+
config.gem 'sqlite3-ruby', :lib => 'sqlite3'
|
13
|
+
config.gem 'thoughtbot-shoulda', :lib => 'shoulda/rails', :source => 'http://gems.github.com'
|
14
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Settings specified here will take precedence over those in config/environment.rb
|
2
|
+
|
3
|
+
# The test environment is used exclusively to run your application's
|
4
|
+
# test suite. You never need to work with it otherwise. Remember that
|
5
|
+
# your test database is "scratch space" for the test suite and is wiped
|
6
|
+
# and recreated between test runs. Don't rely on the data there!
|
7
|
+
config.cache_classes = true
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.action_controller.consider_all_requests_local = true
|
14
|
+
config.action_controller.perform_caching = false
|
15
|
+
config.action_view.cache_template_loading = true
|
16
|
+
|
17
|
+
# Disable request forgery protection in test environment
|
18
|
+
config.action_controller.allow_forgery_protection = false
|
19
|
+
|
20
|
+
# Tell Action Mailer not to deliver emails to the real world.
|
21
|
+
# The :test delivery method accumulates sent emails in the
|
22
|
+
# ActionMailer::Base.deliveries array.
|
23
|
+
config.action_mailer.delivery_method = :test
|
24
|
+
|
25
|
+
# Use SQL instead of Active Record's schema dumper when creating the test database.
|
26
|
+
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
27
|
+
# like if you have constraints or database-specific column types
|
28
|
+
# config.active_record.schema_format = :sql
|
@@ -0,0 +1,43 @@
|
|
1
|
+
ActionController::Routing::Routes.draw do |map|
|
2
|
+
# The priority is based upon order of creation: first created -> highest priority.
|
3
|
+
|
4
|
+
# Sample of regular route:
|
5
|
+
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
|
6
|
+
# Keep in mind you can assign values other than :controller and :action
|
7
|
+
|
8
|
+
# Sample of named route:
|
9
|
+
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
|
10
|
+
# This route can be invoked with purchase_url(:id => product.id)
|
11
|
+
|
12
|
+
# Sample resource route (maps HTTP verbs to controller actions automatically):
|
13
|
+
# map.resources :products
|
14
|
+
|
15
|
+
# Sample resource route with options:
|
16
|
+
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
|
17
|
+
|
18
|
+
# Sample resource route with sub-resources:
|
19
|
+
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
|
20
|
+
|
21
|
+
# Sample resource route with more complex sub-resources
|
22
|
+
# map.resources :products do |products|
|
23
|
+
# products.resources :comments
|
24
|
+
# products.resources :sales, :collection => { :recent => :get }
|
25
|
+
# end
|
26
|
+
|
27
|
+
# Sample resource route within a namespace:
|
28
|
+
# map.namespace :admin do |admin|
|
29
|
+
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
|
30
|
+
# admin.resources :products
|
31
|
+
# end
|
32
|
+
|
33
|
+
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
|
34
|
+
# map.root :controller => "welcome"
|
35
|
+
|
36
|
+
# See how all your routes lay out with "rake routes"
|
37
|
+
|
38
|
+
# Install the default routes as the lowest priority.
|
39
|
+
# Note: These default routes make all actions in every controller accessible via GET requests. You should
|
40
|
+
# consider removing the them or commenting them out if you're using named routes and resources.
|
41
|
+
map.connect ':controller/:action/:id'
|
42
|
+
map.connect ':controller/:action/:id.:format'
|
43
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Office < ActiveRecord::Base
|
4
|
+
has_phone_number :main
|
5
|
+
has_phone_number :abroad, :format => :uk
|
6
|
+
has_phone_number :chicago, :boston
|
7
|
+
has_phone_number :paris, :lyon, :format => :france
|
8
|
+
end
|
9
|
+
|
10
|
+
class HasPhoneNumberTest < ActiveSupport::TestCase
|
11
|
+
context "An object with phone numbers" do
|
12
|
+
setup { @office = Office.new }
|
13
|
+
|
14
|
+
should "respond to main_as_phone_number" do
|
15
|
+
assert_respond_to @office, :main_as_phone_number
|
16
|
+
end # respond to main_as_phone_number
|
17
|
+
|
18
|
+
should "have a number_formats registry" do
|
19
|
+
assert_respond_to Office, :number_formats
|
20
|
+
end # have a number_formats registry
|
21
|
+
|
22
|
+
should "allow specifying a format for the phone number" do
|
23
|
+
assert_equal :uk, Office.number_formats[:abroad]
|
24
|
+
end # allow specifying a format for the phone number
|
25
|
+
|
26
|
+
should "default the phone number format to :us" do
|
27
|
+
assert_equal :us, Office.number_formats[:main]
|
28
|
+
end # default the phone number format to :us
|
29
|
+
|
30
|
+
should "allow specifying more than one phone number attribute in a single call" do
|
31
|
+
assert_respond_to @office, :chicago_as_phone_number
|
32
|
+
assert_respond_to @office, :boston_as_phone_number
|
33
|
+
end # allow specifying more than one phone number attribute in a single call
|
34
|
+
|
35
|
+
should "register the correct format for all phone numbers specified in a single call" do
|
36
|
+
assert_equal :france, Office.number_formats[:paris]
|
37
|
+
assert_equal :france, Office.number_formats[:lyon]
|
38
|
+
end # apply the correct format to all phone numbers specified in a single call
|
39
|
+
|
40
|
+
should "validate all phone numbers specified in a single call" do
|
41
|
+
@office.lyon = "123"
|
42
|
+
@office.lyon_as_phone_number.expects(:valid?)
|
43
|
+
|
44
|
+
@office.valid?
|
45
|
+
end # validate all phone numbers specified in a single call
|
46
|
+
|
47
|
+
context "whose value object is invalid" do
|
48
|
+
setup do
|
49
|
+
@office.main_as_phone_number = stub(:number => "123", :valid? => false)
|
50
|
+
end
|
51
|
+
|
52
|
+
should("not be valid") { deny @office.valid? }
|
53
|
+
|
54
|
+
should "add an error to the attribute" do
|
55
|
+
@office.valid?
|
56
|
+
|
57
|
+
assert_nil @office.errors.on(:main_as_phone_number)
|
58
|
+
assert_not_nil @office.errors.on(:main)
|
59
|
+
end # add an error to the attribute
|
60
|
+
end
|
61
|
+
|
62
|
+
context "with a nil phone number attribute" do
|
63
|
+
should("have a nil corresponding value object") { assert_nil @office.main_as_phone_number }
|
64
|
+
end # with a nil phone number attribute
|
65
|
+
|
66
|
+
context "with a set phone attribute" do
|
67
|
+
setup { @office.main = "1" }
|
68
|
+
|
69
|
+
should "have a set value object" do
|
70
|
+
assert_kind_of HasPhoneNumber::PhoneNumber, @office.main_as_phone_number
|
71
|
+
end
|
72
|
+
|
73
|
+
should "set the number of the value object to the value of the phone attribute" do
|
74
|
+
assert_equal @office.main, @office.main_as_phone_number.number
|
75
|
+
end # set the number of the value object to the value of the phone attribute
|
76
|
+
|
77
|
+
should "set the value object to the appropriate number format" do
|
78
|
+
assert_equal :us, @office.main_as_phone_number.number_format
|
79
|
+
end # set the value object to the appropriate number format
|
80
|
+
end # with a set phone attribute
|
81
|
+
|
82
|
+
context "with a set value object" do
|
83
|
+
setup do
|
84
|
+
@office.main_as_phone_number = stub(:number => "123")
|
85
|
+
end
|
86
|
+
|
87
|
+
should "have an attribute value that matches the number of the value object" do
|
88
|
+
assert_equal @office.main_as_phone_number.number, @office.main
|
89
|
+
end # have an attribute value that matches the number of the value object
|
90
|
+
end # with a set value object
|
91
|
+
end # An object with phone numbers
|
92
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'has_phone_number/phone_number'
|
3
|
+
|
4
|
+
class PhoneNumberTest < ActiveSupport::TestCase
|
5
|
+
context "A Phone Number" do
|
6
|
+
setup do
|
7
|
+
@phone_number = HasPhoneNumber::PhoneNumber.new("123", :test)
|
8
|
+
end
|
9
|
+
|
10
|
+
context "whose number matches its format" do
|
11
|
+
setup do
|
12
|
+
HasPhoneNumber::PhoneNumber.formats[:test].expects(:match).returns(stub("match_stub"))
|
13
|
+
end
|
14
|
+
|
15
|
+
should("be valid") { assert @phone_number.valid? }
|
16
|
+
end # whose number matches its format
|
17
|
+
|
18
|
+
context "whose number does not match its format" do
|
19
|
+
setup do
|
20
|
+
HasPhoneNumber::PhoneNumber.formats[:test].expects(:match).returns(nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
should("not be valid") { deny @phone_number.valid? }
|
24
|
+
end # whose number does not match its format
|
25
|
+
end # A Phone Number
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/test_case'
|
4
|
+
require 'active_record'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'shoulda'
|
7
|
+
|
8
|
+
require 'config/environment'
|
9
|
+
require 'db/schema'
|
10
|
+
require File.join(File.dirname(__FILE__), "..", "init.rb")
|
11
|
+
|
12
|
+
class ActiveSupport::TestCase
|
13
|
+
def deny(expression)
|
14
|
+
assert !expression
|
15
|
+
end
|
16
|
+
end
|
data/test/us_test.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'has_phone_number/phone_number'
|
3
|
+
|
4
|
+
class UsTest < ActiveSupport::TestCase
|
5
|
+
context "An US Phone Number" do
|
6
|
+
setup do
|
7
|
+
@format = HasPhoneNumber::PhoneNumber.formats[:us]
|
8
|
+
end
|
9
|
+
|
10
|
+
context "with less than 10 digits" do
|
11
|
+
setup { @phone_number = "(312) 456-789" }
|
12
|
+
should("not match") { deny @format.match @phone_number }
|
13
|
+
end # with less than 10 digits
|
14
|
+
|
15
|
+
context "with more than 10 digits" do
|
16
|
+
setup { @phone_number = "312-456-78901" }
|
17
|
+
should("not match") { deny @format.match @phone_number }
|
18
|
+
end # with more than 10 digits
|
19
|
+
|
20
|
+
context "with 10 digits" do
|
21
|
+
context "and characters other than (), - and space" do
|
22
|
+
setup { @phone_number = "a312b456c7890" }
|
23
|
+
|
24
|
+
should("not match") { deny @format.match @phone_number }
|
25
|
+
end # and characters other than (), - and space
|
26
|
+
|
27
|
+
context "that are grouped and separated by hyphens" do
|
28
|
+
setup { @phone_number = "312-456-7890" }
|
29
|
+
|
30
|
+
should("match") { assert @format.match @phone_number }
|
31
|
+
end # that are grouped and separated by hyphens
|
32
|
+
|
33
|
+
context "that are grouped and separated by spaces" do
|
34
|
+
setup { @phone_number = "312 456 7890" }
|
35
|
+
|
36
|
+
should("match") { assert @format.match @phone_number }
|
37
|
+
end # that are grouped and separated by spaces
|
38
|
+
|
39
|
+
context "where the area code is in parentheses" do
|
40
|
+
context "followed by a hyphen, and the exchange is followed by a space" do
|
41
|
+
setup { @phone_number = "(312)-456 7890" }
|
42
|
+
|
43
|
+
should("not match") { deny @format.match @phone_number }
|
44
|
+
end # followed by a hyphen, and then space-separated
|
45
|
+
|
46
|
+
context "followed by a space, and the exchange is followed by a space" do
|
47
|
+
setup { @phone_number = "(312) 456 7890" }
|
48
|
+
|
49
|
+
should("match") { assert @format.match @phone_number }
|
50
|
+
end # followed by a space, and the exchange is followed by a space
|
51
|
+
|
52
|
+
context "followed by a space, and the exchange is followed by a hyphen" do
|
53
|
+
setup { @phone_number = "(312) 456-7890" }
|
54
|
+
|
55
|
+
should("match") { assert @format.match @phone_number }
|
56
|
+
end # followed by a space, and the exchange is followed by a hyphen
|
57
|
+
end # where the area code is in parentheses
|
58
|
+
end # with 10 digits
|
59
|
+
end # A Phone Number
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vandrijevik-has_phone_number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vladimir Andrijevik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: HasPhoneNumber makes validations for US phone numbers in ActiveRecord models easy.
|
17
|
+
email: vladimir@andrijevik.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/has_phone_number/phone_number.rb
|
26
|
+
- lib/has_phone_number/validateable.rb
|
27
|
+
- lib/has_phone_number.rb
|
28
|
+
- MIT-LICENSE
|
29
|
+
- Rakefile
|
30
|
+
- README.markdown
|
31
|
+
has_rdoc: false
|
32
|
+
homepage: http://github.com/vandrijevik/has_phone_number/
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.2.0
|
54
|
+
signing_key:
|
55
|
+
specification_version: 2
|
56
|
+
summary: a phone number validator for ActiveRecord
|
57
|
+
test_files:
|
58
|
+
- test/config/boot.rb
|
59
|
+
- test/config/database.yml
|
60
|
+
- test/config/environment.rb
|
61
|
+
- test/config/environments/test.rb
|
62
|
+
- test/config/routes.rb
|
63
|
+
- test/has_phone_number_test.rb
|
64
|
+
- test/phone_number_test.rb
|
65
|
+
- test/test_helper.rb
|
66
|
+
- test/us_test.rb
|