has_phone_numbers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,7 @@
1
+ *SVN*
2
+
3
+ *0.0.1* (August 21st, 2007)
4
+
5
+ * Added documentation
6
+
7
+ * Removed dependency on has_association_helper
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006-2007 Aaron Pfeifer & Neil Abraham
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 ADDED
@@ -0,0 +1,55 @@
1
+ = has_phone_numbers
2
+
3
+ has_phone_numbers adds a base skeleton for handling phone numbers.
4
+
5
+ == Resources
6
+
7
+ API
8
+
9
+ * http://api.pluginaweek.org/has_phone_numbers
10
+
11
+ Wiki
12
+
13
+ * http://wiki.pluginaweek.org/Has_phone_numbers
14
+
15
+ Announcement
16
+
17
+ * http://www.pluginaweek.org/
18
+
19
+ Source
20
+
21
+ * http://svn.pluginaweek.org/trunk/plugins/active_record/has/has_phone_numbers
22
+
23
+ Development
24
+
25
+ * http://dev.pluginaweek.org/browser/trunk/plugins/active_record/has/has_phone_numbers
26
+
27
+ == Description
28
+
29
+ A phone number is a simple model whose data and functionality should be
30
+ standardized across multiple applications. Phone numbers are minimialistic in
31
+ terms of the type of data required and follows the standard U.S. format.
32
+ Support for international formats may be added in the future.
33
+
34
+ === Running migrations
35
+
36
+ To migrate the tables required for has_phone_numbers, you can either run the
37
+ migration from the command line like so:
38
+
39
+ rake db:migrate:plugins PLUGIN=has_phone_numbers
40
+
41
+ or (more ideally) generate a migration file that will integrate into your main
42
+ application's migration path:
43
+
44
+ ruby script/generate plugin_migration MigrateHasPhoneNumbersToVersion1
45
+
46
+ == Testing
47
+
48
+ The following plugins/gems must be installed before any tests can be run:
49
+ * plugin_dependencies - http://wiki.pluginaweekk.org/Plugin_dependencies
50
+ * loaded_plugins - http://wiki.pluginaweek.org/Loaded_plugins
51
+ * appable_plugins - http://wiki.pluginaweek.org/Appable_plugins
52
+
53
+ == Dependencies
54
+
55
+ This plugin does not depend on the presence of any other plugin.
data/Rakefile ADDED
@@ -0,0 +1,79 @@
1
+ require 'rake/testtask'
2
+ require 'rake/rdoctask'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/contrib/sshpublisher'
5
+
6
+ PKG_NAME = 'has_phone_numbers'
7
+ PKG_VERSION = '0.0.1'
8
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
9
+ RUBY_FORGE_PROJECT = 'pluginaweek'
10
+
11
+ desc 'Default: run unit tests.'
12
+ task :default => :test
13
+
14
+ desc 'Test the has_phone_numbers plugin.'
15
+ Rake::TestTask.new(:test) do |t|
16
+ t.libs << 'lib'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = true
19
+ end
20
+
21
+ desc 'Generate documentation for the has_phone_numbers plugin.'
22
+ Rake::RDocTask.new(:rdoc) do |rdoc|
23
+ rdoc.rdoc_dir = 'rdoc'
24
+ rdoc.title = 'HasPhoneNumbers'
25
+ rdoc.options << '--line-numbers' << '--inline-source'
26
+ rdoc.rdoc_files.include('README')
27
+ rdoc.rdoc_files.include('lib/**/*.rb')
28
+ end
29
+
30
+ spec = Gem::Specification.new do |s|
31
+ s.name = PKG_NAME
32
+ s.version = PKG_VERSION
33
+ s.platform = Gem::Platform::RUBY
34
+ s.summary = 'Adds a base skeleton for handling phone numbers.'
35
+
36
+ s.files = FileList['{app,db,lib,test}/**/*'].to_a + %w(CHANGELOG init.rb MIT-LICENSE Rakefile README)
37
+ s.require_path = 'lib'
38
+ s.autorequire = 'has_phone_numbers'
39
+ s.has_rdoc = true
40
+ s.test_files = Dir['test/**/*_test.rb']
41
+
42
+ s.author = 'Aaron Pfeifer, Neil Abraham'
43
+ s.email = 'info@pluginaweek.org'
44
+ s.homepage = 'http://www.pluginaweek.org'
45
+ end
46
+
47
+ Rake::GemPackageTask.new(spec) do |p|
48
+ p.gem_spec = spec
49
+ p.need_tar = true
50
+ p.need_zip = true
51
+ end
52
+
53
+ desc 'Publish the beta gem'
54
+ task :pgem => [:package] do
55
+ Rake::SshFilePublisher.new('pluginaweek@pluginaweek.org', '/home/pluginaweek/gems.pluginaweek.org/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
56
+ end
57
+
58
+ desc 'Publish the API documentation'
59
+ task :pdoc => [:rdoc] do
60
+ Rake::SshDirPublisher.new('pluginaweek@pluginaweek.org', "/home/pluginaweek/api.pluginaweek.org/#{PKG_NAME}", 'rdoc').upload
61
+ end
62
+
63
+ desc 'Publish the API docs and gem'
64
+ task :publish => [:pdoc, :release]
65
+
66
+ desc 'Publish the release files to RubyForge.'
67
+ task :release => [:gem, :package] do
68
+ require 'rubyforge'
69
+
70
+ ruby_forge = RubyForge.new
71
+ ruby_forge.login
72
+
73
+ %w( gem tgz zip ).each do |ext|
74
+ file = "pkg/#{PKG_FILE_NAME}.#{ext}"
75
+ puts "Releasing #{File.basename(file)}..."
76
+
77
+ ruby_forge.add_release(RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, file)
78
+ end
79
+ end
@@ -0,0 +1,29 @@
1
+ # Represents a phone number
2
+ class PhoneNumber < ActiveRecord::Base
3
+ belongs_to :phoneable,
4
+ :polymorphic => true
5
+
6
+ validates_presence_of :phoneable_id,
7
+ :phoneable_type,
8
+ :country_code,
9
+ :number
10
+
11
+ with_options(:allow_nil => true) do |klass|
12
+ klass.validates_numericality_of :country_code,
13
+ :number,
14
+ :extension
15
+ klass.validates_length_of :country_code,
16
+ :in => 1..3
17
+ klass.validates_length_of :number,
18
+ :is => 10
19
+ klass.validates_length_of :extension,
20
+ :maximum => 10
21
+ end
22
+
23
+ def to_s #:nodoc
24
+ human_number = "#{country_code}- #{number}"
25
+ human_number << " ext. #{extension}" if extension
26
+
27
+ human_number
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ class CreatePhoneNumbers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :phone_numbers do |t|
4
+ t.column :phoneable_id, :integer, :null => false, :references => nil
5
+ t.column :phoneable_type, :string, :null => false
6
+ # Default is the United States
7
+ t.column :country_code, :string, :null => false, :limit => 3, :default => 1
8
+ t.column :number, :string, :null => false, :limit => 12
9
+ t.column :extension, :string, :limit => 10
10
+ t.column :created_at, :timestamp, :null => false
11
+ t.column :updated_at, :datetime, :null => false
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ drop_table :phone_numbers
17
+ end
18
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'has_phone_numbers'
@@ -0,0 +1,69 @@
1
+ module PluginAWeek #:nodoc:
2
+ module Has #:nodoc:
3
+ # Adds base models, with minimal attribute definitions, for interacting with
4
+ # phone numbers.
5
+ module PhoneNumbers
6
+ def self.included(base) #:nodoc:
7
+ base.extend(MacroMethods)
8
+ end
9
+
10
+ module MacroMethods
11
+ # Creates a new association for having a single phone number. This
12
+ # takes the same parameters as ActiveRecord::Associations::ClassMethods#has_one.
13
+ # By default, the following associations are the same:
14
+ #
15
+ # class Person < ActiveRecord::Base
16
+ # has_phone_number
17
+ # end
18
+ #
19
+ # and
20
+ #
21
+ # class Person < ActiveRecord::Base
22
+ # has_one :phone_number,
23
+ # :class_name => 'PhoneNumber',
24
+ # :as => :phoneable,
25
+ # :dependent => :destroy
26
+ # end
27
+ def has_phone_number(*args, &extension)
28
+ create_phone_number_association(:one, :phone_number, *args, &extension)
29
+ end
30
+
31
+ # Creates a new association for having a multiple phone numbers. This
32
+ # takes the same parameters as ActiveRecord::Associations::ClassMethods#has_many.
33
+ # By default, the following associations are the same:
34
+ #
35
+ # class Person < ActiveRecord::Base
36
+ # has_phone_numbers
37
+ # end
38
+ #
39
+ # and
40
+ #
41
+ # class Person < ActiveRecord::Base
42
+ # has_many :phone_numbers,
43
+ # :class_name => 'PhoneNumber',
44
+ # :as => :phoneable,
45
+ # :dependent => :destroy
46
+ # end
47
+ def has_phone_numbers(*args, &extension)
48
+ create_phone_number_association(:many, :phone_numbers, *args, &extension)
49
+ end
50
+
51
+ private
52
+ def create_phone_number_association(cardinality, association_id, *args, &extension)
53
+ options = extract_options_from_args!(args)
54
+ options.symbolize_keys!.reverse_merge!(
55
+ :class_name => 'PhoneNumber',
56
+ :as => :phoneable,
57
+ :dependent => :destroy
58
+ )
59
+
60
+ send("has_#{cardinality}", args.first || association_id, options, &extension)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ ActiveRecord::Base.class_eval do
68
+ include PluginAWeek::Has::PhoneNumbers
69
+ end
@@ -0,0 +1,7 @@
1
+ class Person < ActiveRecord::Base
2
+ has_phone_number :cell_phone,
3
+ :conditions => ['phone_numbers.kind = ?', 'cell']
4
+ has_phone_number :work_phone,
5
+ :conditions => ['phone_numbers.kind = ?', 'work']
6
+ has_phone_numbers :phone_numbers
7
+ end
@@ -0,0 +1,30 @@
1
+ require 'config/boot'
2
+
3
+ $:.unshift("#{RAILS_ROOT}/../../../../../rails/plugin_dependencies/lib")
4
+ begin
5
+ require 'plugin_dependencies'
6
+ rescue
7
+ end
8
+
9
+ Rails::Initializer.run do |config|
10
+ config.log_level = :debug
11
+ config.cache_classes = false
12
+ config.whiny_nils = true
13
+ config.breakpoint_server = true
14
+ config.load_paths << "#{RAILS_ROOT}/../../lib"
15
+
16
+ config.plugin_paths.concat([
17
+ "#{RAILS_ROOT}/../../..",
18
+ "#{RAILS_ROOT}/../../../../migrations",
19
+ "#{RAILS_ROOT}/../../../../../rails",
20
+ "#{RAILS_ROOT}/../../../../../test"
21
+ ])
22
+ config.plugins = [
23
+ File.basename(File.expand_path("#{RAILS_ROOT}/../..")),
24
+ 'appable_plugins',
25
+ 'plugin_migrations',
26
+ 'dry_validity_assertions'
27
+ ]
28
+ end
29
+
30
+ Dependencies.log_activity = true
@@ -0,0 +1,11 @@
1
+ class CreatePeople < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :people do |t|
4
+ t.column :name, :string
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :people
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class AddPhoneNumberKinds < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :phone_numbers, :kind, :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column :phone_numbers, :kind, :string
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ john_doe:
2
+ id: 1
3
+ name: John Doe
@@ -0,0 +1,20 @@
1
+ cell:
2
+ id: 1
3
+ phoneable_id: 1
4
+ phoneable_type: Person
5
+ country_code: 1
6
+ number: 1234567890
7
+ created_at: <%= Time.now.to_s(:db) %>
8
+ updated_at: <%= Time.now.to_s(:db) %>
9
+ kind: cell
10
+
11
+ work:
12
+ id: 2
13
+ phoneable_id: 1
14
+ phoneable_type: Person
15
+ country_code: 1
16
+ number: 1234567891
17
+ extension: 123
18
+ created_at: <%= Time.now.to_s(:db) %>
19
+ updated_at: <%= Time.now.to_s(:db) %>
20
+ kind: work
@@ -0,0 +1,9 @@
1
+ # Load the plugin testing framework
2
+ $:.unshift("#{File.dirname(__FILE__)}/../../../../test/plugin_test_helper/lib")
3
+ require 'rubygems'
4
+ require 'plugin_test_helper'
5
+
6
+ PluginAWeek::PluginMigrations.migrate('has_phone_numbers')
7
+
8
+ # Run the migrations
9
+ ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class HasPhoneNumbersTest < Test::Unit::TestCase
4
+ fixtures :phone_numbers, :people
5
+
6
+ def test_should_create_one_association
7
+ assert_equal phone_numbers(:cell), people(:john_doe).cell_phone
8
+ assert_equal phone_numbers(:work), people(:john_doe).work_phone
9
+ end
10
+
11
+ def test_should_create_many_association
12
+ assert_equal 2, people(:john_doe).phone_numbers.size
13
+ assert_equal [], [phone_numbers(:cell), phone_numbers(:work)] - people(:john_doe).phone_numbers
14
+ end
15
+ end
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class PhoneNumberTest < Test::Unit::TestCase
4
+ fixtures :phone_numbers
5
+
6
+ def test_should_be_valid
7
+ assert_valid phone_numbers(:cell)
8
+ end
9
+
10
+ def test_should_require_phoneable_id
11
+ assert_invalid phone_numbers(:cell), 'phoneable_id', nil
12
+ end
13
+
14
+ def test_should_require_phoneable_type
15
+ assert_invalid phone_numbers(:cell), 'phoneable_type', nil
16
+ end
17
+
18
+ def test_should_require_country_code
19
+ assert_invalid phone_numbers(:cell), 'country_code', nil
20
+ end
21
+
22
+ def test_should_require_number
23
+ assert_invalid phone_numbers(:cell), 'number', nil
24
+ end
25
+
26
+ def test_should_require_country_code_be_a_number
27
+ assert_invalid phone_numbers(:cell), 'country_code', 'invalid', '123invalid'
28
+ end
29
+
30
+ def test_should_require_number_be_a_number
31
+ assert_invalid phone_numbers(:cell), 'number', 'invalid', '123invalid'
32
+ end
33
+
34
+ def test_should_require_extension_be_a_number
35
+ assert_invalid phone_numbers(:cell), 'extension', 'invalid', '123invalid'
36
+ end
37
+
38
+ def test_should_restrict_country_code_length
39
+ assert_invalid phone_numbers(:cell), 'extension', 'invalid', '123invalid'
40
+ end
41
+
42
+ def test_should_restrict_number_length
43
+ assert_invalid phone_numbers(:cell), 'number', '123', '12345678901'
44
+ assert_valid phone_numbers(:cell), 'number', '1234567890'
45
+ end
46
+
47
+ def test_should_restrict_extension_length
48
+ assert_invalid phone_numbers(:cell), 'extension', '12345678901'
49
+ assert_valid phone_numbers(:cell), 'extension', '1', '123', '1234567890'
50
+ end
51
+
52
+ def test_should_allow_phoneable_to_be_of_any_type
53
+ assert_valid phone_numbers(:cell), 'phoneable_type', 'House', 'Person'
54
+ end
55
+
56
+ def test_should_generate_stringified_version_of_phone_number
57
+ assert_equal '1- 1234567890', phone_numbers(:cell).to_s
58
+ end
59
+
60
+ def test_should_generate_stringified_version_of_phone_number_with_extension
61
+ assert_equal '1- 1234567891 ext. 123', phone_numbers(:work).to_s
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: has_phone_numbers
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-08-21 00:00:00 -04:00
8
+ summary: Adds a base skeleton for handling phone numbers.
9
+ require_paths:
10
+ - lib
11
+ email: info@pluginaweek.org
12
+ homepage: http://www.pluginaweek.org
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: has_phone_numbers
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Aaron Pfeifer, Neil Abraham
31
+ files:
32
+ - app/models
33
+ - app/models/phone_number.rb
34
+ - db/migrate
35
+ - db/migrate/001_create_phone_numbers.rb
36
+ - lib/has_phone_numbers.rb
37
+ - test/test_helper.rb
38
+ - test/app_root
39
+ - test/unit
40
+ - test/app_root/test
41
+ - test/app_root/config
42
+ - test/app_root/app
43
+ - test/app_root/db
44
+ - test/app_root/test/fixtures
45
+ - test/app_root/test/fixtures/phone_numbers.yml
46
+ - test/app_root/test/fixtures/people.yml
47
+ - test/app_root/config/environment.rb
48
+ - test/app_root/app/models
49
+ - test/app_root/app/models/person.rb
50
+ - test/app_root/db/migrate
51
+ - test/app_root/db/migrate/001_create_people.rb
52
+ - test/app_root/db/migrate/002_add_phone_number_kinds.rb
53
+ - test/unit/phone_number_test.rb
54
+ - test/unit/has_phone_numbers_test.rb
55
+ - CHANGELOG
56
+ - init.rb
57
+ - MIT-LICENSE
58
+ - Rakefile
59
+ - README
60
+ test_files:
61
+ - test/unit/phone_number_test.rb
62
+ - test/unit/has_phone_numbers_test.rb
63
+ rdoc_options: []
64
+
65
+ extra_rdoc_files: []
66
+
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ requirements: []
72
+
73
+ dependencies: []
74
+