Caching address and touching enterprise on save

This commit is contained in:
Will Marshall
2014-07-02 16:53:21 +10:00
parent a8fde53715
commit 2f98da8eef
3 changed files with 12 additions and 1 deletions

View File

@@ -2,6 +2,8 @@ Spree::Address.class_eval do
has_one :enterprise
belongs_to :country, class_name: "Spree::Country"
after_save :touch_enterprise
geocoded_by :full_address
delegate :name, :to => :state, :prefix => true, :allow_nil => true
@@ -15,6 +17,10 @@ Spree::Address.class_eval do
private
def touch_enterprise
enterprise.andand.touch
end
# We have a hard-to-track-down bug around invalid addresses with all-nil fields finding
# their way into the database. I don't know what the source of them is, so this patch
# is designed to track them down.

View File

@@ -18,7 +18,6 @@ end
class Api::UncachedEnterpriseSerializer < ActiveModel::Serializer
attributes :orders_close_at, :active
has_one :address, serializer: Api::AddressSerializer
def orders_close_at
OrderCycle.first_closing_for(object).andand.orders_close_at
@@ -44,6 +43,8 @@ class Api::CachedEnterpriseSerializer < ActiveModel::Serializer
has_many :distributors, key: :hubs, serializer: Api::IdSerializer
has_many :suppliers, key: :producers, serializer: Api::IdSerializer
#has_one :address, serializer: Api::AddressSerializer
def pickup
object.shipping_methods.where(:require_ship_address => false).present?
end

View File

@@ -46,6 +46,10 @@ describe Enterprise do
expect{sm.save!}.to change {enterprise.reload.updated_at }
end
end
it "touches enterprise when address is updated" do
expect{enterprise.address.save!}.to change {enterprise.reload.updated_at }
end
end
end
end