Permalink
Browse files

Initial release

  • Loading branch information...
1 parent c55c709 commit 2fd9b19c2b7a31e26f7ff14d7288b673f058aebd @obrie obrie committed Jul 22, 2007
Showing with 368 additions and 415 deletions.
  1. +5 −5 README
  2. +5 −5 Rakefile
  3. +21 −3 app/models/address.rb
  4. +0 −13 app/models/country.rb
  5. +5 −9 db/migrate/001_create_countries.rb
  6. +3 −7 db/migrate/002_create_regions.rb
  7. +10 −11 db/migrate/003_create_addresses.rb
  8. +1 −1 init.rb
  9. +0 −34 lib/acts_as_addressable.rb
  10. +32 −0 lib/has_addresses.rb
  11. 0 tasks/{acts_as_addressable_tasks.rake → has_addresses_tasks.rake}
  12. +7 −0 test/app_root/app/models/company.rb
  13. +30 −0 test/app_root/config/environment.rb
  14. +11 −0 test/app_root/db/migrate/001_create_companies.rb
  15. +9 −0 test/app_root/db/migrate/002_add_address_kinds.rb
  16. +47 −0 test/app_root/test/fixtures/addresses.yml
  17. +7 −0 test/app_root/test/fixtures/companies.yml
  18. +7 −7 test/{rails_root → app_root}/test/fixtures/countries.yml
  19. +5 −0 test/app_root/test/fixtures/regions.yml
  20. +0 −2 test/rails_root/app/controllers/application.rb
  21. +0 −3 test/rails_root/app/models/credit_card.rb
  22. +0 −45 test/rails_root/config/boot.rb
  23. +0 −22 test/rails_root/config/database.yml
  24. +0 −17 test/rails_root/config/environment.rb
  25. 0 test/rails_root/config/environments/in_memory.rb
  26. 0 test/rails_root/config/environments/mysql.rb
  27. 0 test/rails_root/config/environments/postgresql.rb
  28. 0 test/rails_root/config/environments/sqlite.rb
  29. 0 test/rails_root/config/environments/sqlite3.rb
  30. +0 −5 test/rails_root/config/routes.rb
  31. +0 −16 test/rails_root/db/migrate/001_create_credit_cards.rb
  32. +0 −33 test/rails_root/test/fixtures/addresses.yml
  33. +0 −14 test/rails_root/test/fixtures/credit_cards.yml
  34. +0 −11 test/rails_root/test/fixtures/regions.yml
  35. +0 −11 test/rails_root/vendor/plugins/acts_as_addressable/init.rb
  36. +8 −18 test/test_helper.rb
  37. +0 −9 test/unit/acts_as_addressable_test.rb
  38. +74 −33 test/unit/address_test.rb
  39. +39 −53 test/unit/country_test.rb
  40. +15 −0 test/unit/has_addresses_test.rb
  41. +27 −28 test/unit/region_test.rb
View
10 README
@@ -1,24 +1,24 @@
-= acts_as_addressable
+= has_addresses
-acts_as_addressable .
+has_addresses .
== Resources
Wiki
-* http://wiki.pluginaweek.org/Acts_as_addressable
+* http://wiki.pluginaweek.org/Has_addresses
Announcement
* http://www.pluginaweek.org/
Source
-* http://svn.pluginaweek.org/trunk/plugins/active_record/acts/acts_as_addressable
+* http://svn.pluginaweek.org/trunk/plugins/active_record/has/has_addresses
Development
-* http://dev.pluginaweek.org/browser/trunk/plugins/active_record/acts/acts_as_addressable
+* http://dev.pluginaweek.org/browser/trunk/plugins/active_record/has/has_addresses
== Description
View
@@ -6,25 +6,25 @@ require 'rake/contrib/sshpublisher'
# Load custom rakefile extensions
Dir["#{File.dirname(__FILE__)}/tasks/**/*.rake"].sort.each {|ext| load ext}
-PKG_NAME = 'acts_as_addressable'
+PKG_NAME = 'has_addresses'
PKG_VERSION = '0.0.1'
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
RUBY_FORGE_PROJECT = 'pluginaweek'
desc 'Default: run unit tests.'
task :default => :test
-desc 'Test the acts_as_addressable plugin.'
+desc 'Test the has_addresses plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/unit/**/*_test.rb'
t.verbose = true
end
-desc 'Generate documentation for the acts_as_addressable plugin.'
+desc 'Generate documentation for the has_addresses plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'ActsAsAddressable'
+ rdoc.title = 'HasAddresses'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
@@ -38,7 +38,7 @@ spec = Gem::Specification.new do |s|
s.files = FileList['{app,db,lib,tasks,test,utilities}/**/*'].to_a + %w(init.rb MIT-LICENSE Rakefile README)
s.require_path = 'lib'
- s.autorequire = 'acts_as_addressable'
+ s.autorequire = 'has_addresses'
s.has_rdoc = true
s.test_files = Dir['test/unit/**/*_test.rb']
View
@@ -6,11 +6,18 @@ class Address < ActiveRecord::Base
belongs_to :country
validates_presence_of :addressable_id,
- :addressable_type
-
+ :addressable_type,
+ :street_1,
+ :city,
+ :postal_code
+ validates_presence_of :region_id,
+ :if => :known_region_required?
+ validates_presence_of :custom_region,
+ :if => :custom_region_required?
validates_format_of :postal_code,
:with => /^[0-9]{5}$/,
:allow_nil => true
+
before_save :ensure_exclusive_references
# Returns the region's country if the region is specified
@@ -53,10 +60,21 @@ def multi_line
end
private
+ def known_region_required?
+ country.nil? || country.regions.count != 0
+ end
+
+ # A custom region name is required if a known region was not specified and
+ # the country in which this address resides has no known regions in the
+ # database
+ def custom_region_required?
+ region_id.nil? && country && country.regions.count == 0
+ end
+
# Ensures that the country id/user region combo is not set at the same time as
# the region id
def ensure_exclusive_references
- if region_id?
+ if known_region_required?
self.country_id = nil
self.custom_region = nil
end
View
@@ -1,20 +1,13 @@
# Defined by the ISO 3166-1 standard
class Country < ActiveRecord::Base
has_many :regions
- has_many :addresses_with_known_regions,
- :through => :regions,
- :source => :addresses
- has_many :addresses_with_custom_regions,
- :class_name => 'Address'
validates_presence_of :name,
:alpha_2_code,
:alpha_3_code
-
validates_uniqueness_of :name,
:alpha_2_code,
:alpha_3_code
-
validates_length_of :name,
:within => 2..80
validates_length_of :alpha_2_code,
@@ -30,12 +23,6 @@ def official_name
read_attribute(:official_name) || name
end
- # Returns addresses, regardless of whether that are in a known or custom
- # region
- def addresses
- addresses_with_known_regions + addresses_with_custom_regions
- end
-
# Returns the name of the country
def to_s #:nodoc
name
@@ -1,11 +1,11 @@
class CreateCountries < ActiveRecord::Migration
def self.up
create_table :countries do |t|
- t.column :name, :string, :null => false, :limit => 80
- t.column :official_name, :string, :limit => 80
- t.column :alpha_2_code, :string, :null => false, :limit => 2
- t.column :alpha_3_code, :string, :null => false, :limit => 3
- t.column :calling_code, :string, :limit => 3
+ t.column :name, :string, :null => false, :limit => 80
+ t.column :official_name, :string, :limit => 80
+ t.column :alpha_2_code, :string, :null => false, :limit => 2
+ t.column :alpha_3_code, :string, :null => false, :limit => 3
+ t.column :calling_code, :string, :limit => 3
end
add_index :countries, :name, :unique => true
add_index :countries, :alpha_2_code, :unique => true
@@ -15,8 +15,4 @@ def self.up
def self.down
drop_table :countries
end
-
- def self.bootstrap
- :countries
- end
end
@@ -1,9 +1,9 @@
class CreateRegions < ActiveRecord::Migration
def self.up
create_table :regions do |t|
- t.column :country_id, :integer, :null => false, :unsigned => true
- t.column :name, :string, :null => false, :limit => 50
- t.column :abbreviation, :string, :null => false, :limit => 5
+ t.column :country_id, :integer, :null => false
+ t.column :name, :string, :null => false, :limit => 50
+ t.column :abbreviation, :string, :null => false, :limit => 5
end
add_index :regions, [:name, :country_id], :unique => true
add_index :regions, [:abbreviation, :country_id], :unique => true
@@ -12,8 +12,4 @@ def self.up
def self.down
drop_table :regions
end
-
- def self.bootstrap
- :regions
- end
end
@@ -1,18 +1,17 @@
class CreateAddresses < ActiveRecord::Migration
def self.up
create_table :addresses do |t|
- t.column :addressable_id, :integer, :unsigned => true, :references => nil
- t.column :addressable_type, :string
- t.column :street_1, :string, :limit => 100
- t.column :street_2, :string, :limit => 100
- t.column :city, :string, :limit => 255
- t.column :region_id, :integer, :unsigned => true
- t.column :custom_region, :string, :limit => 50
- t.column :postal_code, :string, :limit => 5
- t.column :country_id, :integer, :unsigned => true, :default => 223
+ t.column :addressable_id, :integer, :null => false, :references => nil
+ t.column :addressable_type, :string, :null => false
+ t.column :street_1, :string, :null => false, :limit => 100
+ t.column :street_2, :string, :limit => 100
+ t.column :city, :string, :null => false, :limit => 255
+ t.column :region_id, :integer
+ t.column :custom_region, :string, :limit => 50
+ t.column :postal_code, :string, :null => false, :limit => 5
+ t.column :country_id, :integer, :default => 223
t.column :created_at, :timestamp, :null => false
- t.column :updated_at, :datetime, :null => false
- t.column :deleted_at, :datetime
+ t.column :updated_at, :datetime, :null => false
end
end
View
@@ -1 +1 @@
-require 'acts_as_addressable'
+require 'has_addresses'
View
@@ -1,34 +0,0 @@
-require 'acts_association_helper'
-
-module PluginAWeek #:nodoc:
- module Acts #:nodoc:
- module Addressable #:nodoc:
- def self.included(base) #:nodoc:
- base.extend(MacroMethods)
- end
-
- module MacroMethods
- #
- def acts_as_addressable(*args, &extension)
- default_options = {
- :count => :one
- }
- create_acts_association(:address, default_options, *args, &extension)
-
- extend PluginAWeek::Acts::Addressable::ClassMethods
- include PluginAWeek::Acts::Addressable::InstanceMethods
- end
- end
-
- module ClassMethods #:nodoc:
- end
-
- module InstanceMethods #:nodoc:
- end
- end
- end
-end
-
-ActiveRecord::Base.class_eval do
- include PluginAWeek::Acts::Addressable
-end
View
@@ -0,0 +1,32 @@
+require 'has_association_helper'
+
+module PluginAWeek #:nodoc:
+ module Has #:nodoc:
+ module Addresses #:nodoc:
+ def self.included(base) #:nodoc:
+ base.extend(MacroMethods)
+ end
+
+ module MacroMethods
+ #
+ def has_address(*args, &extension)
+ create_address_association(:one, *args, &extension)
+ end
+
+ #
+ def has_addresses(*args, &extension)
+ create_address_association(:many, *args, &extension)
+ end
+
+ private
+ def create_address_association(count, *args, &extension)
+ create_has_association(count, :address, {}, *args, &extension)
+ end
+ end
+ end
+ end
+end
+
+ActiveRecord::Base.class_eval do
+ include PluginAWeek::Has::Addresses
+end
@@ -0,0 +1,7 @@
+class Company < ActiveRecord::Base
+ has_address :headquarters_address,
+ :conditions => ['addresses.kind = ?', 'headquarters']
+ has_address :sales_address,
+ :conditions => ['addresses.kind = ?', 'sales']
+ has_addresses
+end
@@ -0,0 +1,30 @@
+require 'config/boot'
+
+$:.unshift("#{RAILS_ROOT}/../../../../../rails/plugin_dependencies/lib")
+begin
+ require 'plugin_dependencies'
+rescue
+end
+
+Rails::Initializer.run do |config|
+ config.log_level = :debug
+ config.cache_classes = false
+ config.whiny_nils = true
+ config.breakpoint_server = true
+ config.load_paths << "#{RAILS_ROOT}/../../lib"
+
+ config.plugin_paths.concat([
+ "#{RAILS_ROOT}/../../..",
+ "#{RAILS_ROOT}/../../../../migrations",
+ "#{RAILS_ROOT}/../../../../../rails",
+ "#{RAILS_ROOT}/../../../../../test"
+ ])
+ config.plugins = [
+ File.basename(File.expand_path("#{RAILS_ROOT}/../..")),
+ 'appable_plugins',
+ 'plugin_migrations',
+ 'dry_validity_assertions'
+ ]
+end
+
+Dependencies.log_activity = true
@@ -0,0 +1,11 @@
+class CreateCompanies < ActiveRecord::Migration
+ def self.up
+ create_table :companies do |t|
+ t.column :name, :string
+ end
+ end
+
+ def self.down
+ drop_table :companies
+ end
+end
@@ -0,0 +1,9 @@
+class AddAddressKinds < ActiveRecord::Migration
+ def self.up
+ add_column :addresses, :kind, :string
+ end
+
+ def self.down
+ remove_column :addresses, :kind
+ end
+end
Oops, something went wrong.

0 comments on commit 2fd9b19

Please sign in to comment.