From 94902268e25dc6cf08ac30e5ec500752f61b8b62 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 4 Jul 2014 11:55:22 +1000 Subject: [PATCH] Initialise enterprise country in controller, not model. Fixes address SQL load every time an enterprise is instanciated. --- app/controllers/admin/enterprises_controller.rb | 11 +++++++++++ app/models/enterprise.rb | 7 ------- spec/models/enterprise_spec.rb | 4 ---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index a4f4eea834..53e58d110b 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -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 diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index d3040041e4..2a4acc1d0a 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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 diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index aa655bebfc..53c78ddc6a 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -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