contactable 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ generators/contactable/USAGE
6
+ generators/contactable/contactable_generator.rb
7
+ generators/contactable/templates/migrations/create_contactable_related_tables.rb
8
+ generators/contactable/templates/models/address.rb
9
+ generators/contactable/templates/models/address_type.rb
10
+ generators/contactable/templates/models/city.rb
11
+ generators/contactable/templates/models/country.rb
12
+ generators/contactable/templates/models/email.rb
13
+ generators/contactable/templates/models/email_type.rb
14
+ generators/contactable/templates/models/instant_messenger.rb
15
+ generators/contactable/templates/models/instant_messenger_protocol.rb
16
+ generators/contactable/templates/models/instant_messenger_type.rb
17
+ generators/contactable/templates/models/phone.rb
18
+ generators/contactable/templates/models/phone_type.rb
19
+ generators/contactable/templates/models/province.rb
20
+ generators/contactable/templates/models/website.rb
21
+ generators/contactable/templates/models/website_type.rb
22
+ lib/contactable.rb
23
+ test/contactable_test.rb
@@ -0,0 +1,2 @@
1
+ = Contactable
2
+ A rails gem that adds contact information to an active record model
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('contactable', '0.1.0') do |p|
6
+ p.description = "Adds contact information to an active record model"
7
+ p.url = "http://github.com/lperichon/contactable"
8
+ p.author = "Luis Perichon"
9
+ p.email = "info@luisperichon.com.ar"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{contactable}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Luis Perichon"]
9
+ s.date = %q{2009-11-18}
10
+ s.description = %q{Adds contact information to an active record model}
11
+ s.email = %q{info@luisperichon.com.ar}
12
+ s.extra_rdoc_files = ["LICENSE", "README.rdoc", "lib/contactable.rb"]
13
+ s.files = ["LICENSE", "Manifest", "README.rdoc", "Rakefile", "generators/contactable/USAGE", "generators/contactable/contactable_generator.rb", "generators/contactable/templates/migrations/create_contactable_related_tables.rb", "generators/contactable/templates/models/address.rb", "generators/contactable/templates/models/address_type.rb", "generators/contactable/templates/models/city.rb", "generators/contactable/templates/models/country.rb", "generators/contactable/templates/models/email.rb", "generators/contactable/templates/models/email_type.rb", "generators/contactable/templates/models/instant_messenger.rb", "generators/contactable/templates/models/instant_messenger_protocol.rb", "generators/contactable/templates/models/instant_messenger_type.rb", "generators/contactable/templates/models/phone.rb", "generators/contactable/templates/models/phone_type.rb", "generators/contactable/templates/models/province.rb", "generators/contactable/templates/models/website.rb", "generators/contactable/templates/models/website_type.rb", "lib/contactable.rb", "test/contactable_test.rb", "contactable.gemspec"]
14
+ s.homepage = %q{http://github.com/lperichon/contactable}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Contactable", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{contactable}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Adds contact information to an active record model}
20
+ s.test_files = ["test/contactable_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Creates a migration that adds contactable fields to the given model
3
+
4
+ Example:
5
+ ./script/generate contactable contact [--skip_related]
6
+
7
+ This will create:
8
+ db/migrate/TIMESTAMP_create_contactable_related_tables.rb
9
+ app/models/...
10
+ db/migrate/TIMESTAMP_add_contactable_fields_to_contact.rb
@@ -0,0 +1,45 @@
1
+ class ContactableGenerator < Rails::Generator::NamedBase
2
+ default_options :skip_related => false
3
+
4
+ def add_options!(opt)
5
+ opt.on('-sr', '--skip_related') { |value| options[:skip_related] = value }
6
+ end
7
+
8
+ def manifest
9
+ record do |m|
10
+ unless options[:skip_related]
11
+ # migration
12
+ m.file 'migrations/create_contactable_related_tables.rb', "db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S')}_create_contactable_related_tables.rb"
13
+
14
+ # models
15
+ %w( address address_type phone phone_type instant_messenger
16
+ instant_messenger_type instant_messenger_protocol website website_type
17
+ email email_type country province city ).each do |model|
18
+ m.file "models/#{model}.rb", "app/models/#{model}.rb"
19
+ end
20
+ end
21
+ m.migration_template 'migration:migration.rb', "db/migrate", {:assigns => contactable_local_assigns,
22
+ :migration_file_name => "add_contactable_fields_to_#{custom_file_name}"
23
+ }
24
+ end
25
+ end
26
+
27
+ private
28
+ def custom_file_name
29
+ custom_name = class_name.underscore.downcase
30
+ custom_name = custom_name.pluralize if ActiveRecord::Base.pluralize_table_names
31
+ end
32
+
33
+ def contactable_local_assigns
34
+ returning(assigns = {}) do
35
+ assigns[:migration_action] = "add"
36
+ assigns[:class_name] = "add_contactable_fields_to_#{custom_file_name}"
37
+ assigns[:table_name] = custom_file_name
38
+ assigns[:attributes] = []
39
+ assigns[:attributes] << Rails::Generator::GeneratedAttribute.new("first_name", "string")
40
+ assigns[:attributes] << Rails::Generator::GeneratedAttribute.new("middle_name", "string")
41
+ assigns[:attributes] << Rails::Generator::GeneratedAttribute.new("last_name", "string")
42
+ assigns[:attributes] << Rails::Generator::GeneratedAttribute.new("birthday", "date")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,108 @@
1
+ class CreateContactableRelatedTables < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :addresses do |t|
4
+ t.integer :owner_id
5
+ t.string :owner_type
6
+ t.integer :type_id, :default => 1
7
+ t.string :address
8
+ t.integer :city_id
9
+ t.integer :province_id
10
+ t.integer :country_id
11
+ t.string :zip
12
+
13
+ t.timestamps
14
+ end
15
+
16
+ create_table :emails do |t|
17
+ t.integer :owner_id
18
+ t.string :owner_type
19
+ t.integer :type_id, :default => 1
20
+ t.string :address
21
+
22
+ t.timestamps
23
+ end
24
+
25
+ create_table :instant_messengers do |t|
26
+ t.integer :owner_id
27
+ t.string :owner_type
28
+ t.integer :protocol_id, :default => 1
29
+ t.integer :type_id, :default => 1
30
+ t.string :nick
31
+
32
+ t.timestamps
33
+ end
34
+
35
+ create_table :phones do |t|
36
+ t.integer :owner_id
37
+ t.string :owner_type
38
+ t.integer :type_id, :default => 1
39
+ t.string :number
40
+
41
+ t.timestamps
42
+ end
43
+
44
+ create_table :websites do |t|
45
+ t.integer :owner_id
46
+ t.string :owner_type
47
+ t.integer :type_id, :default => 1
48
+ t.string :address
49
+
50
+ t.timestamps
51
+ end
52
+
53
+ create_table :address_types do |t|
54
+ t.string :description
55
+ end
56
+
57
+ create_table :cities do |t|
58
+ t.references :province
59
+ t.string :name
60
+ end
61
+
62
+ create_table :countries do |t|
63
+ t.string :name
64
+ end
65
+
66
+ create_table :email_types do |t|
67
+ t.string :description
68
+ end
69
+
70
+ create_table :instant_messenger_protocols do |t|
71
+ t.string :name
72
+ end
73
+
74
+ create_table :instant_messenger_types do |t|
75
+ t.string :description
76
+ end
77
+
78
+ create_table :phone_types do |t|
79
+ t.string :description
80
+ end
81
+
82
+ create_table :provinces do |t|
83
+ t.references :country
84
+ t.string :name
85
+ end
86
+
87
+ create_table :website_types do |t|
88
+ t.string :description
89
+ end
90
+ end
91
+
92
+ def self.down
93
+ drop_table :addresses
94
+ drop_table :emails
95
+ drop_table :instant_messengers
96
+ drop_table :phones
97
+ drop_table :websites
98
+ drop_table :address_types
99
+ drop_table :cities
100
+ drop_table :countries
101
+ drop_table :email_types
102
+ drop_table :instant_messenger_protocols
103
+ drop_table :instant_messenger_types
104
+ drop_table :phone_types
105
+ drop_table :provinces
106
+ drop_table :website_types
107
+ end
108
+ end
@@ -0,0 +1,33 @@
1
+ class Address < ActiveRecord::Base
2
+
3
+ belongs_to :owner, :polymorphic => true
4
+ belongs_to :type, :class_name => 'AddressType'
5
+ belongs_to :city
6
+ belongs_to :country
7
+ belongs_to :province
8
+
9
+ def city_name
10
+ city.name if city
11
+ end
12
+
13
+ def city_name=(name)
14
+ self.city = City.find_or_create_by_name(name.to_s.titleize) unless name.blank?
15
+ end
16
+
17
+ def province_name
18
+ province.name if province
19
+ end
20
+
21
+ def province_name=(name)
22
+ self.province = Province.find_or_create_by_name(name.to_s.titleize) unless name.blank?
23
+ end
24
+
25
+ def country_name
26
+ country.name if country
27
+ end
28
+
29
+ def country_name=(name)
30
+ self.country = Country.find_or_create_by_name(name.to_s.titleize) unless name.blank?
31
+ end
32
+
33
+ end
@@ -0,0 +1,5 @@
1
+ class AddressType < ActiveRecord::Base
2
+
3
+ validates_presence_of :description
4
+
5
+ end
@@ -0,0 +1,4 @@
1
+ class City < ActiveRecord::Base
2
+ belongs_to :province
3
+ validates_presence_of :name, :province
4
+ end
@@ -0,0 +1,4 @@
1
+ class Country < ActiveRecord::Base
2
+ has_many :provinces
3
+ validates_presence_of :name
4
+ end
@@ -0,0 +1,14 @@
1
+ class Email < ActiveRecord::Base
2
+
3
+ validates_presence_of :address
4
+ validates_format_of :address,
5
+ :with => /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
6
+
7
+ belongs_to :owner, :polymorphic => true
8
+ belongs_to :type, :class_name => 'EmailType'
9
+
10
+ def before_save
11
+ self.address.downcase!
12
+ end
13
+
14
+ end
@@ -0,0 +1,5 @@
1
+ class EmailType < ActiveRecord::Base
2
+
3
+ validates_presence_of :description
4
+
5
+ end
@@ -0,0 +1,9 @@
1
+ class InstantMessenger < ActiveRecord::Base
2
+
3
+ validates_presence_of :nick
4
+
5
+ belongs_to :owner, :polymorphic => true
6
+ belongs_to :protocol, :class_name => 'InstantMessengerProtocol'
7
+ belongs_to :type, :class_name => 'InstantMessengerType'
8
+
9
+ end
@@ -0,0 +1,5 @@
1
+ class InstantMessengerProtocol < ActiveRecord::Base
2
+
3
+ validates_presence_of :name
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ class InstantMessengerType < ActiveRecord::Base
2
+
3
+ validates_presence_of :description
4
+
5
+ end
@@ -0,0 +1,8 @@
1
+ class Phone < ActiveRecord::Base
2
+
3
+ validates_presence_of :number
4
+
5
+ belongs_to :owner, :polymorphic => true
6
+ belongs_to :type, :class_name => 'PhoneType'
7
+
8
+ end
@@ -0,0 +1,5 @@
1
+ class PhoneType < ActiveRecord::Base
2
+
3
+ validates_presence_of :description
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ class Province < ActiveRecord::Base
2
+ belongs_to :country
3
+ has_many :cities
4
+ validates_presence_of :name, :country
5
+ end
@@ -0,0 +1,8 @@
1
+ class Website < ActiveRecord::Base
2
+
3
+ validates_presence_of :address
4
+
5
+ belongs_to :owner, :polymorphic => true
6
+ belongs_to :type, :class_name => 'WebsiteType'
7
+
8
+ end
@@ -0,0 +1,5 @@
1
+ class WebsiteType < ActiveRecord::Base
2
+
3
+ validates_presence_of :description
4
+
5
+ end
@@ -0,0 +1,84 @@
1
+ module Contactable
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ # This extension provides contact information to an active record model.
7
+ #
8
+ # Examples:
9
+ #
10
+ # class Person < ActiveRecord::Base
11
+ # contactable
12
+ # end
13
+ module ClassMethods
14
+ # Configuration options are:
15
+ #
16
+ def contactable(*args, &block)
17
+ # default options
18
+ options = {}
19
+ options.merge!(args.pop) if args.last.kind_of? Hash
20
+
21
+ class_eval <<-EOV
22
+ def name_part_columns
23
+ [:first_name, :middle_name, :last_name]
24
+ end
25
+
26
+ def name_parts
27
+ name_parts = []
28
+ name_part_columns.each do |name_part|
29
+ name_parts << self.send(name_part) if self.respond_to?(name_part)
30
+ end
31
+ name_parts
32
+ end
33
+
34
+ # contactable#name() supports 2 options:
35
+ # If a name column exists that one is used
36
+ # If not use first, middle and last_name columns (only if available)
37
+ def name
38
+ if self[:name]
39
+ self[:name]
40
+ else
41
+ name_parts.join(' ')
42
+ end
43
+ end
44
+
45
+ def initials
46
+ if self[:name]
47
+ self[:name].tr('-', ' ').split(' ').map { |word| word.chars.first.upcase.to_s + '.'}.join
48
+ else
49
+ name_parts.map {|name_part| name_part.chars.first.upcase.to_s + '.'}.join
50
+ end
51
+ end
52
+
53
+ def age
54
+ if self.respond_to?(:birthday) && !self.birthday.nil?
55
+ now = Time.now.utc.to_date
56
+ now.year - birthday.year - (birthday.to_date.change(:year => now.year) > now ? 1 : 0)
57
+ end
58
+ end
59
+
60
+ has_many :addresses, :as => :owner
61
+ has_many :emails, :as => :owner
62
+ has_many :instant_messengers, :as => :owner
63
+ has_many :phones, :as => :owner
64
+ has_many :websites, :as => :owner
65
+
66
+ accepts_nested_attributes_for :addresses,
67
+ :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true
68
+ accepts_nested_attributes_for :emails,
69
+ :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true
70
+ accepts_nested_attributes_for :instant_messengers,
71
+ :reject_if => proc { |attributes| attributes['nick'].blank? }, :allow_destroy => true
72
+ accepts_nested_attributes_for :phones,
73
+ :reject_if => proc { |attributes| attributes['number'].blank? }, :allow_destroy => true
74
+ accepts_nested_attributes_for :websites,
75
+ :reject_if => proc { |attributes| attributes['address'].blank? }, :allow_destroy => true
76
+ EOV
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+ class ActiveRecord::Base
83
+ include Contactable
84
+ end
@@ -0,0 +1,84 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ gem 'activerecord'
4
+ gem 'timecop'
5
+ require 'timecop'
6
+ require 'active_record'
7
+
8
+ require "#{File.dirname(__FILE__)}/../lib/contactable"
9
+
10
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
11
+
12
+ def setup_db
13
+ ActiveRecord::Schema.define(:version => 1) do
14
+ create_table :contacts do |t|
15
+ t.column :first_name, :string
16
+ t.column :last_name, :string
17
+ t.column :birthday, :date
18
+ end
19
+ create_table :companies do |t|
20
+ t.column :name, :string
21
+ end
22
+ create_table :email_addresses do |t|
23
+ t.column :contactable_id, :integer
24
+ t.column :contactable_type, :string
25
+ t.column :address, :string
26
+ end
27
+ end
28
+ end
29
+
30
+ def teardown_db
31
+ ActiveRecord::Base.connection.tables.each do |table|
32
+ ActiveRecord::Base.connection.drop_table(table)
33
+ end
34
+ end
35
+
36
+ class Contact < ActiveRecord::Base
37
+ contactable
38
+ end
39
+
40
+ class Company < ActiveRecord::Base
41
+ contactable
42
+ end
43
+
44
+ class EmailAddress < ActiveRecord::Base
45
+ belongs_to :contactable, :polymorphic => true
46
+ validates_presence_of :contactable, :address
47
+ end
48
+
49
+ class ContactableTest < Test::Unit::TestCase
50
+
51
+ def setup
52
+ setup_db
53
+
54
+ # Set Time.now to September 1, 2008 10:05:00 AM (at this instant), but allow it to move forward
55
+ t = Time.local(2009, 11, 6, 10, 5, 0)
56
+ Timecop.travel(t)
57
+ @contact = Contact.create! :first_name => "Bart", :last_name => "Simpson", :birthday => Date.parse('3-3-1983')
58
+ @company = Company.create! :name => "The Leftorium"
59
+ end
60
+
61
+ def teardown
62
+ teardown_db
63
+ end
64
+
65
+ def test_name
66
+ assert_equal 'Bart Simpson', @contact.name
67
+ assert_equal 'The Leftorium', @company.name
68
+ end
69
+
70
+ def test_initials
71
+ assert_equal 'B.S.', @contact.initials
72
+ end
73
+
74
+ def test_age
75
+ assert_equal 26, @contact.age
76
+ end
77
+
78
+ def test_email_addresses
79
+ assert @contact.email_addresses.empty?
80
+
81
+ contact_with_email = Contact.create!(:first_name => 'Homer', :last_name => "Simpson", :email_addresses_attributes => [{:address => 'homer@simpsons.com'}] )
82
+ end
83
+
84
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contactable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luis Perichon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-18 00:00:00 -03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Adds contact information to an active record model
17
+ email: info@luisperichon.com.ar
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ - lib/contactable.rb
26
+ files:
27
+ - LICENSE
28
+ - Manifest
29
+ - README.rdoc
30
+ - Rakefile
31
+ - generators/contactable/USAGE
32
+ - generators/contactable/contactable_generator.rb
33
+ - generators/contactable/templates/migrations/create_contactable_related_tables.rb
34
+ - generators/contactable/templates/models/address.rb
35
+ - generators/contactable/templates/models/address_type.rb
36
+ - generators/contactable/templates/models/city.rb
37
+ - generators/contactable/templates/models/country.rb
38
+ - generators/contactable/templates/models/email.rb
39
+ - generators/contactable/templates/models/email_type.rb
40
+ - generators/contactable/templates/models/instant_messenger.rb
41
+ - generators/contactable/templates/models/instant_messenger_protocol.rb
42
+ - generators/contactable/templates/models/instant_messenger_type.rb
43
+ - generators/contactable/templates/models/phone.rb
44
+ - generators/contactable/templates/models/phone_type.rb
45
+ - generators/contactable/templates/models/province.rb
46
+ - generators/contactable/templates/models/website.rb
47
+ - generators/contactable/templates/models/website_type.rb
48
+ - lib/contactable.rb
49
+ - test/contactable_test.rb
50
+ - contactable.gemspec
51
+ has_rdoc: true
52
+ homepage: http://github.com/lperichon/contactable
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --line-numbers
58
+ - --inline-source
59
+ - --title
60
+ - Contactable
61
+ - --main
62
+ - README.rdoc
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "1.2"
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project: contactable
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Adds contact information to an active record model
84
+ test_files:
85
+ - test/contactable_test.rb