app/models/region.rb in has_addresses-0.5.0 vs app/models/region.rb in has_addresses-0.5.1

- old
+ new

@@ -41,17 +41,29 @@ validates_presence_of :country_id, :name, :abbreviation validates_length_of :abbreviation, :within => 1..5 def initialize(attributes = nil) #:nodoc: - super + super(self.class.with_defaults(attributes)) + end + + # Adds the default attributes for the given country attributes + def self.with_defaults(attributes = nil) + attributes ||= {} + attributes.symbolize_keys! - self.abbreviation = "%03d" % (attributes[:id] % 1000) if attributes && !attributes.include?(:abbreviation) && attributes[:id] - self.code = "#{country}-#{abbreviation}" + country = attributes.delete(:country) + attributes[:country_id] = country.id if country + attributes[:abbreviation] = "%03d" % (attributes[:id] % 1000) if !attributes.include?(:abbreviation) && attributes[:id] + + country ||= attributes[:country_id] && Country.find(attributes[:country_id]) + attributes[:code] = "#{country}-#{attributes[:abbreviation]}" + + attributes end - fast_bootstrap( + fast_bootstrap([ # Andorra [{:country => Country['AD']}].map {|r| [ r.merge(:id => 20002, :name => "Canillo"), r.merge(:id => 20003, :name => "Encamp"), r.merge(:id => 20004, :name => "La Massana"), @@ -5378,7 +5390,7 @@ r.merge(:id => 716007, :name => "Masvingo", :abbreviation => 'MV'), r.merge(:id => 716008, :name => "Matabeleland North", :abbreviation => 'MN'), r.merge(:id => 716009, :name => "Matabeleland South", :abbreviation => 'MS'), r.merge(:id => 716010, :name => "Midlands", :abbreviation => 'MI') ]} - ) + ].flatten.map {|attributes| with_defaults(attributes)}) end