has_addresses 0.5.0 → 0.5.1

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/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  == master
2
2
 
3
+ == 0.5.1 / 2009-05-01
4
+
5
+ * Update to enumerate_by 0.4.1's #fast_bootstrap for speed boost
6
+
3
7
  == 0.5.0 / 2009-04-30
4
8
 
5
9
  * Replace acts_as_enumeration with enumerate_by
data/README.rdoc CHANGED
@@ -60,4 +60,4 @@ To run against a specific version of Rails:
60
60
  == Dependencies
61
61
 
62
62
  * Rails 2.3 or later
63
- * enumerate_by[http://github.com/pluginaweek/enumerate_by]
63
+ * enumerate_by[http://github.com/pluginaweek/enumerate_by] 0.4.1 or later
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
5
5
 
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = 'has_addresses'
8
- s.version = '0.5.0'
8
+ s.version = '0.5.1'
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.summary = 'Demonstrates a reference implementation for handling countries, regions, and addresses.'
11
11
 
@@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s|
13
13
  s.require_path = 'lib'
14
14
  s.has_rdoc = true
15
15
  s.test_files = Dir['test/**/*_test.rb']
16
- s.add_dependency 'enumerate_by', '>= 0.4.0'
16
+ s.add_dependency 'enumerate_by', '>= 0.4.1'
17
17
 
18
18
  s.author = 'Aaron Pfeifer'
19
19
  s.email = 'aaron@pluginaweek.org'
@@ -45,11 +45,18 @@ class Country < ActiveRecord::Base
45
45
  alias_attribute :abbreviation_3, :alpha_3_code
46
46
 
47
47
  def initialize(attributes = nil) #:nodoc:
48
- super
49
- self.official_name ||= name unless attributes && attributes.include?(:official_name)
48
+ super(self.class.with_defaults(attributes))
50
49
  end
51
50
 
52
- fast_bootstrap(
51
+ # Adds the default attributes for the given country attributes
52
+ def self.with_defaults(attributes = nil)
53
+ attributes ||= {}
54
+ attributes.symbolize_keys!
55
+ attributes[:official_name] = attributes[:name] unless attributes.include?(:official_name)
56
+ attributes
57
+ end
58
+
59
+ fast_bootstrap([
53
60
  {:id => 4, :name => "Afghanistan", :official_name => "Islamic Republic of Afghanistan", :alpha_2_code => 'AF', :alpha_3_code => 'AFG'},
54
61
  {:id => 8, :name => "Albania", :official_name => "Republic of Albania", :alpha_2_code => 'AL', :alpha_3_code => 'ALB'},
55
62
  {:id => 10, :name => "Antarctica", :alpha_2_code => 'AQ', :alpha_3_code => 'ATA'},
@@ -296,5 +303,5 @@ class Country < ActiveRecord::Base
296
303
  {:id => 882, :name => "Samoa", :official_name => "Independent State of Samoa", :alpha_2_code => 'WS', :alpha_3_code => 'WSM'},
297
304
  {:id => 887, :name => "Yemen", :official_name => "Republic of Yemen", :alpha_2_code => 'YE', :alpha_3_code => 'YEM'},
298
305
  {:id => 894, :name => "Zambia", :official_name => "Republic of Zambia", :alpha_2_code => 'ZM', :alpha_3_code => 'ZMB'}
299
- )
306
+ ].map {|attributes| with_defaults(attributes)})
300
307
  end
data/app/models/region.rb CHANGED
@@ -43,13 +43,25 @@ class Region < ActiveRecord::Base
43
43
  validates_length_of :abbreviation, :within => 1..5
44
44
 
45
45
  def initialize(attributes = nil) #:nodoc:
46
- super
46
+ super(self.class.with_defaults(attributes))
47
+ end
48
+
49
+ # Adds the default attributes for the given country attributes
50
+ def self.with_defaults(attributes = nil)
51
+ attributes ||= {}
52
+ attributes.symbolize_keys!
53
+
54
+ country = attributes.delete(:country)
55
+ attributes[:country_id] = country.id if country
56
+ attributes[:abbreviation] = "%03d" % (attributes[:id] % 1000) if !attributes.include?(:abbreviation) && attributes[:id]
57
+
58
+ country ||= attributes[:country_id] && Country.find(attributes[:country_id])
59
+ attributes[:code] = "#{country}-#{attributes[:abbreviation]}"
47
60
 
48
- self.abbreviation = "%03d" % (attributes[:id] % 1000) if attributes && !attributes.include?(:abbreviation) && attributes[:id]
49
- self.code = "#{country}-#{abbreviation}"
61
+ attributes
50
62
  end
51
63
 
52
- fast_bootstrap(
64
+ fast_bootstrap([
53
65
  # Andorra
54
66
  [{:country => Country['AD']}].map {|r| [
55
67
  r.merge(:id => 20002, :name => "Canillo"),
@@ -5380,5 +5392,5 @@ class Region < ActiveRecord::Base
5380
5392
  r.merge(:id => 716009, :name => "Matabeleland South", :abbreviation => 'MS'),
5381
5393
  r.merge(:id => 716010, :name => "Midlands", :abbreviation => 'MI')
5382
5394
  ]}
5383
- )
5395
+ ].flatten.map {|attributes| with_defaults(attributes)})
5384
5396
  end
@@ -44,9 +44,10 @@ class CountryTest < ActiveRecord::TestCase
44
44
  assert country.errors.invalid?(:name)
45
45
  end
46
46
 
47
- def test_should_not_require_an_official_name
47
+ def test_should_require_an_official_name
48
48
  country = new_country(:official_name => nil)
49
- assert country.valid?
49
+ assert !country.valid?
50
+ assert country.errors.invalid?(:official_name)
50
51
  end
51
52
 
52
53
  def test_should_require_an_alpha_2_code
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_addresses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-30 00:00:00 -04:00
12
+ date: 2009-05-01 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.4.0
23
+ version: 0.4.1
24
24
  version:
25
25
  description:
26
26
  email: aaron@pluginaweek.org