mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Initialise enterprise country in controller, not model. Fixes address SQL load every time an enterprise is instanciated.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user