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.
@@ -1,14 +1,14 @@
1
1
  class CreateCountries < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :countries do |t|
4
- t.column :name, :string, :null => false, :limit => 80
5
- t.column :official_name, :string, :limit => 80
6
- t.column :alpha_2_code, :string, :null => false, :limit => 2
7
- t.column :alpha_3_code, :string, :null => false, :limit => 3
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.column :country_id, :integer, :null => false
5
- t.column :name, :string, :null => false, :limit => 50
6
- t.column :abbreviation, :string, :null => false, :limit => 5
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.column :addressable_id, :integer, :null => false, :references => nil
5
- t.column :addressable_type, :string, :null => false
6
- t.column :street_1, :string, :null => false, :limit => 100
7
- t.column :street_2, :string, :limit => 100
8
- t.column :city, :string, :null => false, :limit => 255
9
- t.column :region_id, :integer
10
- t.column :custom_region, :string, :limit => 50
11
- t.column :postal_code, :string, :null => false, :limit => 5
12
- t.column :country_id, :integer, :default => 223
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
- module PluginAWeek #:nodoc:
2
- module Has #:nodoc:
3
- # Adds base models for interacting with addresses, including Country,
4
- # Region, and Address. These have the minimal attribute definitions needed
5
- # to store addresses.
6
- module Addresses
7
- # Whether or not to use verbose output
8
- mattr_accessor :verbose
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
- include PluginAWeek::Has::Addresses
14
+ extend HasAddresses::MacroMethods
74
15
  end
@@ -1,7 +1,3 @@
1
1
  class Company < ActiveRecord::Base
2
- has_address :headquarters_address,
3
- :conditions => ['addresses.kind = ?', 'headquarters']
4
- has_address :sales_address,
5
- :conditions => ['addresses.kind = ?', 'sales']
6
2
  has_addresses
7
- end
3
+ 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.concat([
11
- "#{RAILS_ROOT}/../../..",
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
@@ -1,7 +1,7 @@
1
1
  class CreateCompanies < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :companies do |t|
4
- t.column :name, :string
4
+ t.string :name, :null => false
5
5
  end
6
6
  end
7
7
 
@@ -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__)}/../../../../test/plugin_test_helper/lib")
2
+ $:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
3
3
  require 'rubygems'
4
4
  require 'plugin_test_helper'
5
5
 
6
- PluginAWeek::Has::Addresses.verbose = false
6
+ # Run the migrations
7
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
7
8
 
8
- PluginAWeek::PluginMigrations.migrate('has_addresses')
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
- # Run the migrations
11
- ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
15
+ # Remove defaults for testing
16
+ Region.delete_all
17
+ Country.delete_all