has_addresses 0.0.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +50 -0
- data/{MIT-LICENSE → LICENSE} +2 -2
- data/README.rdoc +63 -0
- data/Rakefile +48 -41
- data/app/models/address.rb +92 -50
- data/app/models/country.rb +287 -101
- data/app/models/region.rb +5378 -106
- data/db/migrate/001_create_countries.rb +7 -7
- data/db/migrate/002_create_regions.rb +7 -5
- data/db/migrate/003_create_addresses.rb +10 -12
- data/lib/has_addresses.rb +9 -68
- data/test/app_root/app/models/company.rb +1 -5
- data/test/app_root/config/environment.rb +6 -19
- data/test/app_root/db/migrate/001_create_companies.rb +1 -1
- data/test/app_root/db/migrate/002_migrate_has_addresses_to_version_3.rb +13 -0
- data/test/factory.rb +68 -0
- data/test/functional/has_addresses_test.rb +22 -0
- data/test/test_helper.rb +11 -5
- data/test/unit/address_test.rb +210 -79
- data/test/unit/country_test.rb +101 -72
- data/test/unit/region_test.rb +104 -61
- metadata +70 -61
- data/CHANGELOG +0 -23
- data/README +0 -122
- data/tasks/has_addresses_tasks.rake +0 -23
- data/test/app_root/db/migrate/002_add_address_kinds.rb +0 -9
- data/test/files/iso_3166.xml +0 -1719
- data/test/files/iso_3166_2.xml +0 -8617
- data/test/fixtures/addresses.yml +0 -47
- data/test/fixtures/companies.yml +0 -7
- data/test/fixtures/countries.yml +0 -12
- data/test/fixtures/regions.yml +0 -5
- data/test/unit/has_addresses_test.rb +0 -15
@@ -1,14 +1,14 @@
|
|
1
1
|
class CreateCountries < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
3
|
create_table :countries do |t|
|
4
|
-
t.
|
5
|
-
t.
|
6
|
-
t.
|
7
|
-
|
4
|
+
t.string :name, :official_name, :null => false
|
5
|
+
t.string :alpha_2_code, :null => false, :limit => 2
|
6
|
+
t.string :alpha_3_code, :null => false, :limit => 3
|
7
|
+
end
|
8
|
+
|
9
|
+
change_table :countries do |t|
|
10
|
+
t.index :alpha_2_code
|
8
11
|
end
|
9
|
-
add_index :countries, :name, :unique => true
|
10
|
-
add_index :countries, :alpha_2_code, :unique => true
|
11
|
-
add_index :countries, :alpha_3_code, :unique => true
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.down
|
@@ -1,12 +1,14 @@
|
|
1
1
|
class CreateRegions < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
3
|
create_table :regions do |t|
|
4
|
-
t.
|
5
|
-
t.
|
6
|
-
t.
|
4
|
+
t.references :country, :null => false
|
5
|
+
t.string :code, :name, :abbreviation, :null => false
|
6
|
+
t.string :group
|
7
|
+
end
|
8
|
+
|
9
|
+
change_table :regions do |t|
|
10
|
+
t.index :code
|
7
11
|
end
|
8
|
-
add_index :regions, [:name, :country_id], :unique => true
|
9
|
-
add_index :regions, [:abbreviation, :country_id], :unique => true
|
10
12
|
end
|
11
13
|
|
12
14
|
def self.down
|
@@ -1,21 +1,19 @@
|
|
1
1
|
class CreateAddresses < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
3
|
create_table :addresses do |t|
|
4
|
-
t.
|
5
|
-
t.
|
6
|
-
t.
|
7
|
-
t.
|
8
|
-
t.
|
9
|
-
t.
|
10
|
-
t.
|
11
|
-
t.
|
12
|
-
t.
|
13
|
-
t.column :created_at, :timestamp, :null => false
|
14
|
-
t.column :updated_at, :datetime, :null => false
|
4
|
+
t.references :addressable, :polymorphic => true, :null => false
|
5
|
+
t.string :street_1, :null => false
|
6
|
+
t.string :street_2
|
7
|
+
t.string :city, :null => false
|
8
|
+
t.references :region
|
9
|
+
t.string :custom_region
|
10
|
+
t.string :postal_code, :null => false
|
11
|
+
t.references :country
|
12
|
+
t.timestamps
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
16
|
def self.down
|
19
17
|
drop_table :addresses
|
20
18
|
end
|
21
|
-
end
|
19
|
+
end
|
data/lib/has_addresses.rb
CHANGED
@@ -1,74 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@@verbose = true
|
10
|
-
|
11
|
-
def self.included(base) #:nodoc:
|
12
|
-
base.extend(MacroMethods)
|
13
|
-
end
|
14
|
-
|
15
|
-
module MacroMethods
|
16
|
-
# Creates a new association for having a single address. This takes
|
17
|
-
# the same parameters as +has_one+. By default, the following associations
|
18
|
-
# are the same:
|
19
|
-
#
|
20
|
-
# class Person < ActiveRecord::Base
|
21
|
-
# has_address
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# and
|
25
|
-
#
|
26
|
-
# class Person < ActiveRecord::Base
|
27
|
-
# has_one :address,
|
28
|
-
# :class_name => 'Address',
|
29
|
-
# :as => :addressable,
|
30
|
-
# :dependent => :destroy
|
31
|
-
# end
|
32
|
-
def has_address(*args, &extension)
|
33
|
-
create_address_association(:one, :address, *args, &extension)
|
34
|
-
end
|
35
|
-
|
36
|
-
# Creates a new association for having a multiple addresses. This takes
|
37
|
-
# the same parameters as +has_many+. By default, the following associations
|
38
|
-
# are the same:
|
39
|
-
#
|
40
|
-
# class Person < ActiveRecord::Base
|
41
|
-
# has_addresses
|
42
|
-
# end
|
43
|
-
#
|
44
|
-
# and
|
45
|
-
#
|
46
|
-
# class Person < ActiveRecord::Base
|
47
|
-
# has_many :addresses,
|
48
|
-
# :class_name => 'Address',
|
49
|
-
# :as => :addressable,
|
50
|
-
# :dependent => :destroy
|
51
|
-
# end
|
52
|
-
def has_addresses(*args, &extension)
|
53
|
-
create_address_association(:many, :addresses, *args, &extension)
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
def create_address_association(cardinality, association_id, *args, &extension)
|
58
|
-
options = extract_options_from_args!(args)
|
59
|
-
options.symbolize_keys!.reverse_merge!(
|
60
|
-
:class_name => 'Address',
|
61
|
-
:as => :addressable,
|
62
|
-
:dependent => :destroy
|
63
|
-
)
|
64
|
-
|
65
|
-
send("has_#{cardinality}", args.first || association_id, options, &extension)
|
66
|
-
end
|
67
|
-
end
|
1
|
+
# Adds a generic implementation for dealing with regions, countries, and
|
2
|
+
# addresses
|
3
|
+
module HasAddresses
|
4
|
+
module MacroMethods
|
5
|
+
# Creates the following association:
|
6
|
+
# * +addresses+ - All addresses associated with the current record
|
7
|
+
def has_addresses
|
8
|
+
has_many :addresses, :as => :addressable
|
68
9
|
end
|
69
10
|
end
|
70
11
|
end
|
71
12
|
|
72
13
|
ActiveRecord::Base.class_eval do
|
73
|
-
|
14
|
+
extend HasAddresses::MacroMethods
|
74
15
|
end
|
@@ -1,25 +1,12 @@
|
|
1
1
|
require 'config/boot'
|
2
2
|
|
3
|
-
$:.unshift("#{RAILS_ROOT}/../../../../../rails/plugin_dependencies/lib")
|
4
|
-
begin
|
5
|
-
require 'plugin_dependencies'
|
6
|
-
rescue Exception => e
|
7
|
-
end
|
8
|
-
|
9
3
|
Rails::Initializer.run do |config|
|
10
|
-
config.plugin_paths
|
11
|
-
|
12
|
-
"#{RAILS_ROOT}/../../../../migrations",
|
13
|
-
"#{RAILS_ROOT}/../../../../../rails",
|
14
|
-
"#{RAILS_ROOT}/../../../../../test"
|
15
|
-
])
|
16
|
-
config.plugins = [
|
17
|
-
'loaded_plugins',
|
18
|
-
'appable_plugins',
|
19
|
-
'plugin_migrations',
|
20
|
-
File.basename(File.expand_path("#{RAILS_ROOT}/../..")),
|
21
|
-
'dry_validity_assertions'
|
22
|
-
]
|
4
|
+
config.plugin_paths << '..'
|
5
|
+
config.plugins = %w(enumerate_by has_addresses)
|
23
6
|
config.cache_classes = false
|
24
7
|
config.whiny_nils = true
|
8
|
+
config.action_controller.session = {:key => 'rails_session', :secret => 'd229e4d22437432705ab3985d4d246'}
|
9
|
+
config.after_initialize do
|
10
|
+
EnumerateBy.perform_caching = false
|
11
|
+
end
|
25
12
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class MigrateHasAddressesToVersion3 < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
ActiveRecord::Migrator.new(:up, "#{Rails.root}/../../db/migrate", 0).migrations.each do |migration|
|
4
|
+
migration.migrate(:up)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.down
|
9
|
+
ActiveRecord::Migrator.new(:up, "#{Rails.root}/../../db/migrate", 0).migrations.each do |migration|
|
10
|
+
migration.migrate(:down)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/test/factory.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module Factory
|
2
|
+
# Build actions for the model
|
3
|
+
def self.build(model, &block)
|
4
|
+
name = model.to_s.underscore
|
5
|
+
|
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
|
34
|
+
end
|
35
|
+
|
36
|
+
build Address do |attributes|
|
37
|
+
attributes[:addressable] = create_company unless attributes.include?(:addressable)
|
38
|
+
attributes[:region] = create_region(attributes.slice(:country)) unless attributes.include?(:region)
|
39
|
+
attributes.reverse_merge!(
|
40
|
+
:street_1 => '1600 Amphitheatre Parkway',
|
41
|
+
:city => 'Mountain View',
|
42
|
+
:postal_code => '94043'
|
43
|
+
)
|
44
|
+
end
|
45
|
+
|
46
|
+
build Company do |attributes|
|
47
|
+
attributes.reverse_merge!(
|
48
|
+
:name => 'Google'
|
49
|
+
)
|
50
|
+
end
|
51
|
+
|
52
|
+
build Country do |attributes|
|
53
|
+
attributes.reverse_merge!(
|
54
|
+
:name => 'United States',
|
55
|
+
:official_name => 'United States of America',
|
56
|
+
:alpha_2_code => 'US',
|
57
|
+
:alpha_3_code => 'USA'
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
build Region do |attributes|
|
62
|
+
attributes[:country] = create_country unless attributes.include?(:country)
|
63
|
+
attributes.reverse_merge!(
|
64
|
+
:name => 'California',
|
65
|
+
:abbreviation => 'CA'
|
66
|
+
)
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class CompanyByDefaultTest < ActiveRecord::TestCase
|
4
|
+
def setup
|
5
|
+
@company = create_company
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_should_not_have_any_addresses
|
9
|
+
assert @company.addresses.empty?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class CompanyWithAddressesTest < ActiveRecord::TestCase
|
14
|
+
def setup
|
15
|
+
@company = create_company
|
16
|
+
@address = create_address(:addressable => @company)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_have_addresses
|
20
|
+
assert_equal [@address], @company.addresses
|
21
|
+
end
|
22
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
# Load the plugin testing framework
|
2
|
-
$:.unshift("#{File.dirname(__FILE__)}
|
2
|
+
$:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
|
3
3
|
require 'rubygems'
|
4
4
|
require 'plugin_test_helper'
|
5
5
|
|
6
|
-
|
6
|
+
# Run the migrations
|
7
|
+
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
|
7
8
|
|
8
|
-
|
9
|
+
# Mixin the factory helper
|
10
|
+
require File.expand_path("#{File.dirname(__FILE__)}/factory")
|
11
|
+
Test::Unit::TestCase.class_eval do
|
12
|
+
include Factory
|
13
|
+
end
|
9
14
|
|
10
|
-
#
|
11
|
-
|
15
|
+
# Remove defaults for testing
|
16
|
+
Region.delete_all
|
17
|
+
Country.delete_all
|