Initialise enterprise country in controller, not model. Fixes address SQL load every time an enterprise is instanciated.

This commit is contained in:
Rohan Mitchell
2014-07-04 11:55:22 +10:00
parent 206275b794
commit 94902268e2
3 changed files with 11 additions and 11 deletions

View File

@@ -19,6 +19,17 @@ module Admin
end
protected
def build_resource_with_address
enterprise = build_resource_without_address
enterprise.address = Spree::Address.new
enterprise.address.country = Spree::Country.find_by_id(Spree::Config[:default_country_id])
enterprise
end
alias_method_chain :build_resource, :address
private
# When an enterprise user creates another enterprise, it is granted management

View File

@@ -29,7 +29,6 @@ class Enterprise < ActiveRecord::Base
validates_presence_of :address
validates_associated :address
before_create :initialize_country
before_validation :set_unused_address_fields
after_validation :geocode_address
@@ -210,12 +209,6 @@ class Enterprise < ActiveRecord::Base
url.andand.sub /(https?:\/\/)?(www\.)?/, ''
end
# Give us an empty address on create, and set the country to the default
def initialize_country
self.address ||= Spree::Address.new
self.address.country = Spree::Country.find_by_id(Spree::Config[:default_country_id]) if self.address.new_record?
end
def set_unused_address_fields
address.firstname = address.lastname = address.phone = 'unused' if address.present?
end

View File

@@ -67,10 +67,6 @@ describe Enterprise do
it { should delegate(:state_name).to(:address) }
end
it "should default address country to system country" do
create(:distributor_enterprise).address.country.should == Spree::Country.find_by_id(Spree::Config[:default_country_id])
end
describe "scopes" do
describe 'active' do