diff --git a/app/models/spree/address_decorator.rb b/app/models/spree/address_decorator.rb index 3a06b54d18..9bb3fc6418 100644 --- a/app/models/spree/address_decorator.rb +++ b/app/models/spree/address_decorator.rb @@ -1,3 +1,5 @@ +require 'open_food_web/notify_invalid_address_save' + Spree::Address.class_eval do has_one :enterprise @@ -7,7 +9,7 @@ Spree::Address.class_eval do delegate :name, :to => :state, :prefix => true, :allow_nil => true def full_address - full_address = [address1, address2, zipcode, city, country.name, state.andand.name] + full_address = [address1, address2, zipcode, city, country.andand.name, state.andand.name] filtered_address = full_address.select{ |field| !field.nil? && field != '' } filtered_address.compact.join(', ') end diff --git a/lib/open_food_web/notify_invalid_address_save.rb b/lib/open_food_web/notify_invalid_address_save.rb new file mode 100644 index 0000000000..2c5005aa45 --- /dev/null +++ b/lib/open_food_web/notify_invalid_address_save.rb @@ -0,0 +1,34 @@ +module OpenFoodWeb + + # 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. + # This is intended to be a temporary investigative measure, and should be removed from the + # code base shortly. + # + #-- Rohan, 17-9-2913 + module NotifyInvalidAddressSave + def create + if self.class == Spree::Address && self.zipcode.nil? + Bugsnag.notify RuntimeError.new('Saving a Spree::Address with nil values') + end + + super + end + + def update + if self.class == Spree::Address && self.zipcode.nil? + Bugsnag.notify RuntimeError.new('Saving a Spree::Address with nil values') + end + + super + end + end +end + + +module ActiveRecord + class Base + include OpenFoodWeb::NotifyInvalidAddressSave + end +end diff --git a/spec/lib/open_food_web/notify_invalid_address_save_spec.rb b/spec/lib/open_food_web/notify_invalid_address_save_spec.rb new file mode 100644 index 0000000000..978fa755c6 --- /dev/null +++ b/spec/lib/open_food_web/notify_invalid_address_save_spec.rb @@ -0,0 +1,20 @@ +require 'open_food_web/notify_invalid_address_save' + +module OpenFoodWeb + describe NotifyInvalidAddressSave do + describe "notifying bugsnag when a Spree::Address is saved with missing data" do + it "notifies on create" do + Bugsnag.should_receive(:notify) + a = Spree::Address.new zipcode: nil + a.save validate: false + end + + it "notifies on update" do + Bugsnag.should_receive(:notify) + a = create(:address) + a.zipcode = nil + a.save validate: false + end + end + end +end