has_phone_numbers 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ == master
2
+
3
+ == 0.0.5 / 2008-10-26
4
+
5
+ * Change how the base module is included to prevent namespacing conflicts
6
+ * Change PhoneNumber#to_s to PhoneNumber#display_value
7
+
8
+ == 0.0.4 / 2008-06-22
9
+
10
+ * Remove log files from gems
11
+
12
+ == 0.0.3 / 2008-05-05
13
+
14
+ * Updated documentation
15
+
16
+ == 0.0.2 / 2007-09-26
17
+
18
+ * Move test fixtures out of the test application root directory
19
+
20
+ == 0.0.1 / 2007-08-21
21
+
22
+ * Added documentation
23
+ * Removed dependency on has_association_helper
File without changes
@@ -4,21 +4,21 @@
4
4
 
5
5
  == Resources
6
6
 
7
- Wiki
8
-
9
- * http://wiki.pluginaweek.org/Has_phone_numbers
10
-
11
7
  API
12
8
 
13
9
  * http://api.pluginaweek.org/has_phone_numbers
14
10
 
11
+ Bugs
12
+
13
+ * http://pluginaweek.lighthouseapp.com/projects/13275-has_phone_numbers
14
+
15
15
  Development
16
16
 
17
- * http://dev.pluginaweek.org/browser/trunk/has_phone_numbers
17
+ * http://github.com/pluginaweek/has_phone_numbers
18
18
 
19
19
  Source
20
20
 
21
- * http://svn.pluginaweek.org/trunk/has_phone_numbers
21
+ * git://github.com/pluginaweek/has_phone_numbers.git
22
22
 
23
23
  == Description
24
24
 
@@ -39,12 +39,12 @@ modified for your own usage.
39
39
  :number => '1234567890',
40
40
  :extension => '123'
41
41
  )
42
- phone_number.to_s # => 1- 1234567890 ext. 123
42
+ phone_number.display_value # => 1- 1234567890 ext. 123
43
43
 
44
44
  == Testing
45
45
 
46
46
  Before you can run any tests, the following gem must be installed:
47
- * plugin_test_helper[http://wiki.pluginaweek.org/Plugin_test_helper]
47
+ * plugin_test_helper[http://github.com/pluginaweek/plugin_test_helper]
48
48
 
49
49
  To run against a specific version of Rails:
50
50
 
@@ -53,4 +53,4 @@ To run against a specific version of Rails:
53
53
  == Dependencies
54
54
 
55
55
  * Rails 2.1 or later
56
- * plugins_plus[http://wiki.pluginaweek.org/Plugins_plus]
56
+ * plugins_plus[http://github.com/pluginaweek/plugins_plugins] (optional if app files are copied to your project tree)
data/Rakefile CHANGED
@@ -3,46 +3,54 @@ require 'rake/rdoctask'
3
3
  require 'rake/gempackagetask'
4
4
  require 'rake/contrib/sshpublisher'
5
5
 
6
- PKG_NAME = 'has_phone_numbers'
7
- PKG_VERSION = '0.0.4'
8
- PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
9
- RUBY_FORGE_PROJECT = 'pluginaweek'
10
-
11
- desc 'Default: run unit tests.'
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = 'has_phone_numbers'
8
+ s.version = '0.0.5'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = 'Demonstrates a reference implementation for handling phone numbers.'
11
+
12
+ s.files = FileList['{app,db,lib,test}/**/*'] + %w(CHANGELOG.rdoc init.rb LICENSE Rakefile README.rdoc) - FileList['test/app_root/{log,log/*,script,script/*}']
13
+ s.require_path = 'lib'
14
+ s.has_rdoc = true
15
+ s.test_files = Dir['test/**/*_test.rb']
16
+
17
+ s.author = 'Aaron Pfeifer'
18
+ s.email = 'aaron@pluginaweek.org'
19
+ s.homepage = 'http://www.pluginaweek.org'
20
+ s.rubyforge_project = 'pluginaweek'
21
+ end
22
+
23
+ desc 'Default: run all tests.'
12
24
  task :default => :test
13
25
 
14
- desc 'Test the has_phone_numbers plugin.'
26
+ desc "Test the #{spec.name} plugin."
15
27
  Rake::TestTask.new(:test) do |t|
16
28
  t.libs << 'lib'
17
- t.pattern = 'test/**/*_test.rb'
29
+ t.test_files = spec.test_files
18
30
  t.verbose = true
19
31
  end
20
32
 
21
- desc 'Generate documentation for the has_phone_numbers plugin.'
33
+ begin
34
+ require 'rcov/rcovtask'
35
+ namespace :test do
36
+ desc "Test the #{spec.name} plugin with Rcov."
37
+ Rcov::RcovTask.new(:rcov) do |t|
38
+ t.libs << 'lib'
39
+ t.test_files = spec.test_files
40
+ t.rcov_opts << '--exclude="^(?!lib/|app/)"'
41
+ t.verbose = true
42
+ end
43
+ end
44
+ rescue LoadError
45
+ end
46
+
47
+ desc "Generate documentation for the #{spec.name} plugin."
22
48
  Rake::RDocTask.new(:rdoc) do |rdoc|
23
49
  rdoc.rdoc_dir = 'rdoc'
24
- rdoc.title = 'HasPhoneNumbers'
50
+ rdoc.title = spec.name
25
51
  rdoc.template = '../rdoc_template.rb'
26
52
  rdoc.options << '--line-numbers' << '--inline-source'
27
- rdoc.rdoc_files.include('README')
28
- rdoc.rdoc_files.include('lib/**/*.rb')
29
- end
30
-
31
- spec = Gem::Specification.new do |s|
32
- s.name = PKG_NAME
33
- s.version = PKG_VERSION
34
- s.platform = Gem::Platform::RUBY
35
- s.summary = 'Demonstrates a reference implementation for handling phone numbers.'
36
-
37
- s.files = FileList['{app,db,lib,test}/**/*'].to_a - FileList['test/app_root/log/*'].to_a + %w(CHANGELOG init.rb MIT-LICENSE Rakefile README)
38
- s.require_path = 'lib'
39
- s.autorequire = 'has_phone_numbers'
40
- s.has_rdoc = true
41
- s.test_files = Dir['test/**/*_test.rb']
42
-
43
- s.author = 'Aaron Pfeifer'
44
- s.email = 'aaron@pluginaweek.org'
45
- s.homepage = 'http://www.pluginaweek.org'
53
+ rdoc.rdoc_files.include('README.rdoc', 'CHANGELOG.rdoc', 'LICENSE', 'lib/**/*.rb', 'app/**/*.rb')
46
54
  end
47
55
 
48
56
  Rake::GemPackageTask.new(spec) do |p|
@@ -51,14 +59,14 @@ Rake::GemPackageTask.new(spec) do |p|
51
59
  p.need_zip = true
52
60
  end
53
61
 
54
- desc 'Publish the beta gem'
62
+ desc 'Publish the beta gem.'
55
63
  task :pgem => [:package] do
56
- Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
64
+ Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{spec.name}-#{spec.version}.gem").upload
57
65
  end
58
66
 
59
- desc 'Publish the API documentation'
67
+ desc 'Publish the API documentation.'
60
68
  task :pdoc => [:rdoc] do
61
- Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{PKG_NAME}", 'rdoc').upload
69
+ Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{spec.name}", 'rdoc').upload
62
70
  end
63
71
 
64
72
  desc 'Publish the API docs and gem'
@@ -71,10 +79,10 @@ task :release => [:gem, :package] do
71
79
  ruby_forge = RubyForge.new.configure
72
80
  ruby_forge.login
73
81
 
74
- %w( gem tgz zip ).each do |ext|
75
- file = "pkg/#{PKG_FILE_NAME}.#{ext}"
82
+ %w(gem tgz zip).each do |ext|
83
+ file = "pkg/#{spec.name}-#{spec.version}.#{ext}"
76
84
  puts "Releasing #{File.basename(file)}..."
77
85
 
78
- ruby_forge.add_release(RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, file)
86
+ ruby_forge.add_release(spec.rubyforge_project, spec.name, spec.version, file)
79
87
  end
80
88
  end
@@ -1,4 +1,10 @@
1
- # Represents a phone number (most likely in the United States)
1
+ # Represents a phone number split into multiple parts:
2
+ # * +country_code+ - Uniquely identifiers the country to which the number belongs. This value is based on the E.164 standard (http://en.wikipedia.org/wiki/E.164)
3
+ # * +number+ - The subscriber number (10 digits in length)
4
+ # * +extension+ - A number that can route to different phones at a location
5
+ #
6
+ # This phone number format is biased towards those types found in the United
7
+ # States and may need to be adjusted for international support.
2
8
  class PhoneNumber < ActiveRecord::Base
3
9
  belongs_to :phoneable,
4
10
  :polymorphic => true
@@ -19,10 +25,18 @@ class PhoneNumber < ActiveRecord::Base
19
25
  :maximum => 10,
20
26
  :allow_nil => true
21
27
 
22
- def to_s #:nodoc
28
+ # Generates a human-readable version of the phone number, based on all of the
29
+ # various parts of the number.
30
+ #
31
+ # For example,
32
+ #
33
+ # phone = PhoneNumber.new(:country_code => '1', :number => '123-456-7890')
34
+ # phone.display_value # => "1- 123-456-7890"
35
+ # phone.extension = "123"
36
+ # phone.display_value # => "1- 123-456-7890 ext. 123"
37
+ def display_value
23
38
  human_number = "#{country_code}- #{number}"
24
39
  human_number << " ext. #{extension}" if extension
25
-
26
40
  human_number
27
41
  end
28
42
  end
@@ -1,12 +1,6 @@
1
1
  module PluginAWeek #:nodoc:
2
2
  # Adds a generic implementation for dealing with phone numbers
3
3
  module HasPhoneNumbers
4
- def self.included(base) #:nodoc:
5
- base.class_eval do
6
- extend PluginAWeek::HasPhoneNumbers::MacroMethods
7
- end
8
- end
9
-
10
4
  module MacroMethods
11
5
  # Creates the following association:
12
6
  # * +phone_number+ - All phone numbers associated with the current record.
@@ -19,5 +13,5 @@ module PluginAWeek #:nodoc:
19
13
  end
20
14
 
21
15
  ActiveRecord::Base.class_eval do
22
- include PluginAWeek::HasPhoneNumbers
16
+ extend PluginAWeek::HasPhoneNumbers::MacroMethods
23
17
  end
@@ -1,9 +1,3 @@
1
1
  class Person < ActiveRecord::Base
2
- has_one :cell_phone,
3
- :class_name => 'PhoneNumber',
4
- :conditions => {:kind => 'cell'}
5
- has_one :work_phone,
6
- :class_name => 'PhoneNumber',
7
- :conditions => {:kind => 'work'}
8
- has_many :phone_numbers
2
+ has_phone_numbers
9
3
  end
data/test/factory.rb CHANGED
@@ -1,26 +1,36 @@
1
1
  module Factory
2
- # Build actions for the class
3
- def self.build(klass, &block)
4
- name = klass.to_s.underscore
5
- define_method("#{name}_attributes", block)
2
+ # Build actions for the model
3
+ def self.build(model, &block)
4
+ name = model.to_s.underscore
6
5
 
7
- module_eval <<-end_eval
8
- def valid_#{name}_attributes(attributes = {})
9
- #{name}_attributes(attributes)
10
- attributes
11
- end
12
-
13
- def new_#{name}(attributes = {})
14
- #{klass}.new(valid_#{name}_attributes(attributes))
15
- end
16
-
17
- def create_#{name}(*args)
18
- record = new_#{name}(*args)
19
- record.save!
20
- record.reload
21
- record
22
- end
23
- end_eval
6
+ define_method("#{name}_attributes", block)
7
+ define_method("valid_#{name}_attributes") {|*args| valid_attributes_for(model, *args)}
8
+ define_method("new_#{name}") {|*args| new_record(model, *args)}
9
+ define_method("create_#{name}") {|*args| create_record(model, *args)}
10
+ end
11
+
12
+ # Get valid attributes for the model
13
+ def valid_attributes_for(model, attributes = {})
14
+ name = model.to_s.underscore
15
+ send("#{name}_attributes", attributes)
16
+ attributes.stringify_keys!
17
+ attributes
18
+ end
19
+
20
+ # Build an unsaved record
21
+ def new_record(model, *args)
22
+ attributes = valid_attributes_for(model, *args)
23
+ record = model.new(attributes)
24
+ attributes.each {|attr, value| record.send("#{attr}=", value) if model.accessible_attributes && !model.accessible_attributes.include?(attr) || model.protected_attributes && model.protected_attributes.include?(attr)}
25
+ record
26
+ end
27
+
28
+ # Build and save/reload a record
29
+ def create_record(model, *args)
30
+ record = new_record(model, *args)
31
+ record.save!
32
+ record.reload
33
+ record
24
34
  end
25
35
 
26
36
  build Person do |attributes|
@@ -0,0 +1,22 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class PersonByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @person = create_person
6
+ end
7
+
8
+ def test_should_not_have_any_phone_numbers
9
+ assert @person.phone_numbers.empty?
10
+ end
11
+ end
12
+
13
+ class PersonWithPhoneNumbersTest < Test::Unit::TestCase
14
+ def setup
15
+ @person = create_person
16
+ @phone_number = create_phone_number(:phoneable => @person)
17
+ end
18
+
19
+ def test_should_have_phone_numbers
20
+ assert_equal [@phone_number], @person.phone_numbers
21
+ end
22
+ end
data/test/test_helper.rb CHANGED
@@ -8,6 +8,6 @@ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
8
8
 
9
9
  # Mixin the factory helper
10
10
  require File.expand_path("#{File.dirname(__FILE__)}/factory")
11
- class Test::Unit::TestCase #:nodoc:
11
+ Test::Unit::TestCase.class_eval do
12
12
  include Factory
13
13
  end
@@ -113,6 +113,24 @@ class PhoneNumberTest < Test::Unit::TestCase
113
113
  assert !phone_number.valid?
114
114
  assert_equal 1, Array(phone_number.errors.on(:extension)).size
115
115
  end
116
+
117
+ def test_should_protect_attributes_from_mass_assignment
118
+ phone_number = PhoneNumber.new(
119
+ :id => 1,
120
+ :phoneable_id => 1,
121
+ :phoneable_type => 'User',
122
+ :country_code => '123',
123
+ :number => '8675309',
124
+ :extension => '999'
125
+ )
126
+
127
+ assert_nil phone_number.id
128
+ assert_equal 1, phone_number.phoneable_id
129
+ assert_equal 'User', phone_number.phoneable_type
130
+ assert_equal '123', phone_number.country_code
131
+ assert_equal '8675309', phone_number.number
132
+ assert_equal '999', phone_number.extension
133
+ end
116
134
  end
117
135
 
118
136
  class PhoneNumberAfterBeingCreatedTest < Test::Unit::TestCase
@@ -133,8 +151,8 @@ class PhoneNumberAfterBeingCreatedTest < Test::Unit::TestCase
133
151
  assert_equal @person, @phone_number.phoneable
134
152
  end
135
153
 
136
- def test_should_generate_stringified_version_of_phone_number
137
- assert_equal '1- 1234567890', @phone_number.to_s
154
+ def test_should_have_a_display_value
155
+ assert_equal '1- 1234567890', @phone_number.display_value
138
156
  end
139
157
  end
140
158
 
@@ -143,7 +161,7 @@ class PhoneNumberWithExtensionTest < Test::Unit::TestCase
143
161
  @phone_number = create_phone_number(:country_code => '1', :number => '1234567890', :extension => '123')
144
162
  end
145
163
 
146
- def test_should_generate_stringified_version_of_phone_number
147
- assert_equal '1- 1234567890 ext. 123', @phone_number.to_s
164
+ def test_should_have_a_display_value
165
+ assert_equal '1- 1234567890 ext. 123', @phone_number.display_value
148
166
  end
149
167
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_phone_numbers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer
8
- autorequire: has_phone_numbers
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-22 00:00:00 -04:00
12
+ date: 2008-10-26 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -37,17 +37,17 @@ files:
37
37
  - test/app_root/db/migrate
38
38
  - test/app_root/db/migrate/001_create_people.rb
39
39
  - test/app_root/db/migrate/002_migrate_has_phone_numbers_to_version_1.rb
40
- - test/app_root/db/migrate/003_add_phone_number_kinds.rb
41
- - test/app_root/log
40
+ - test/functional
41
+ - test/functional/has_phone_numbers_test.rb
42
42
  - test/test_helper.rb
43
43
  - test/factory.rb
44
44
  - test/unit
45
45
  - test/unit/phone_number_test.rb
46
- - CHANGELOG
46
+ - CHANGELOG.rdoc
47
47
  - init.rb
48
- - MIT-LICENSE
48
+ - LICENSE
49
49
  - Rakefile
50
- - README
50
+ - README.rdoc
51
51
  has_rdoc: true
52
52
  homepage: http://www.pluginaweek.org
53
53
  post_install_message:
@@ -69,10 +69,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  version:
70
70
  requirements: []
71
71
 
72
- rubyforge_project:
73
- rubygems_version: 1.1.1
72
+ rubyforge_project: pluginaweek
73
+ rubygems_version: 1.2.0
74
74
  signing_key:
75
75
  specification_version: 2
76
76
  summary: Demonstrates a reference implementation for handling phone numbers.
77
77
  test_files:
78
+ - test/functional/has_phone_numbers_test.rb
78
79
  - test/unit/phone_number_test.rb
data/CHANGELOG DELETED
@@ -1,19 +0,0 @@
1
- *SVN*
2
-
3
- *0.0.4* (June 22nd, 2008)
4
-
5
- * Remove log files from gems
6
-
7
- *0.0.3* (May 5th, 2008)
8
-
9
- * Updated documentation
10
-
11
- *0.0.2* (September 26th, 2007)
12
-
13
- * Move test fixtures out of the test application root directory
14
-
15
- *0.0.1* (August 21st, 2007)
16
-
17
- * Added documentation
18
-
19
- * Removed dependency on has_association_helper
@@ -1,9 +0,0 @@
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