diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 77ab2e6dd6..31933a5138 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -1,4 +1,4 @@ -require 'open_food_network/last_used_address' +require 'open_food_network/address_finder' class CheckoutController < Spree::CheckoutController layout 'darkswarm' @@ -185,10 +185,10 @@ class CheckoutController < Spree::CheckoutController def before_address associate_user - lua = OpenFoodNetwork::LastUsedAddress.new(@order.email, @order.customer, spree_current_user) + finder = OpenFoodNetwork::AddressFinder.new(@order.email, @order.customer, spree_current_user) - @order.bill_address = lua.bill_address - @order.ship_address = lua.ship_address + @order.bill_address = finder.bill_address + @order.ship_address = finder.ship_address end # Overriding Spree's methods diff --git a/app/controllers/spree/checkout_controller_decorator.rb b/app/controllers/spree/checkout_controller_decorator.rb index 7ea930ef90..b01ffad30e 100644 --- a/app/controllers/spree/checkout_controller_decorator.rb +++ b/app/controllers/spree/checkout_controller_decorator.rb @@ -1,4 +1,4 @@ -require 'open_food_network/last_used_address' +require 'open_food_network/address_finder' Spree::CheckoutController.class_eval do @@ -23,9 +23,9 @@ Spree::CheckoutController.class_eval do def before_address associate_user - lua = OpenFoodNetwork::LastUsedAddress.new(@order.email) + finder = OpenFoodNetwork::AddressFinder.new(@order.email) - @order.bill_address = lua.bill_address - @order.ship_address = lua.ship_address + @order.bill_address = finder.bill_address + @order.ship_address = finder.ship_address end end diff --git a/app/serializers/api/admin/user_serializer.rb b/app/serializers/api/admin/user_serializer.rb index 58a6fcc87e..07d5f49d87 100644 --- a/app/serializers/api/admin/user_serializer.rb +++ b/app/serializers/api/admin/user_serializer.rb @@ -1,4 +1,4 @@ -require 'open_food_network/last_used_address' +require 'open_food_network/address_finder' class Api::Admin::UserSerializer < ActiveModel::Serializer attributes :id, :email, :confirmed @@ -7,11 +7,11 @@ class Api::Admin::UserSerializer < ActiveModel::Serializer has_one :bill_address, serializer: Api::AddressSerializer def ship_address - OpenFoodNetwork::LastUsedAddress.new(object.email, object).ship_address + OpenFoodNetwork::AddressFinder.new(object.email, object).ship_address end def bill_address - OpenFoodNetwork::LastUsedAddress.new(object.email, object).bill_address + OpenFoodNetwork::AddressFinder.new(object.email, object).bill_address end def confirmed diff --git a/lib/open_food_network/last_used_address.rb b/lib/open_food_network/address_finder.rb similarity index 98% rename from lib/open_food_network/last_used_address.rb rename to lib/open_food_network/address_finder.rb index d04fcc15ec..34cf69cd7a 100644 --- a/lib/open_food_network/last_used_address.rb +++ b/lib/open_food_network/address_finder.rb @@ -4,7 +4,7 @@ # according to this order: customer addresses, user addresses, addresses from # completed orders with an email that matches the email string provided. module OpenFoodNetwork - class LastUsedAddress + class AddressFinder attr_accessor :email, :user, :customer def initialize(*args) diff --git a/spec/lib/open_food_network/last_used_address_spec.rb b/spec/lib/open_food_network/address_finder_spec.rb similarity index 63% rename from spec/lib/open_food_network/last_used_address_spec.rb rename to spec/lib/open_food_network/address_finder_spec.rb index 44ff3ffb58..aae7b823af 100644 --- a/spec/lib/open_food_network/last_used_address_spec.rb +++ b/spec/lib/open_food_network/address_finder_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' -require 'open_food_network/last_used_address' +require 'open_food_network/address_finder' module OpenFoodNetwork - describe LastUsedAddress do + describe AddressFinder do let(:email) { 'test@example.com' } describe "initialisation" do @@ -10,16 +10,16 @@ module OpenFoodNetwork let(:customer) { create(:customer) } context "when passed any combination of instances of String, Customer or Spree::User" do - let(:lua1) { LastUsedAddress.new(email, customer, user) } - let(:lua2) { LastUsedAddress.new(customer, user, email) } + let(:finder1) { AddressFinder.new(email, customer, user) } + let(:finder2) { AddressFinder.new(customer, user, email) } it "stores arguments based on their class" do - expect(lua1.email).to eq email - expect(lua2.email).to eq email - expect(lua1.customer).to be customer - expect(lua2.customer).to be customer - expect(lua1.user).to be user - expect(lua2.user).to be user + expect(finder1.email).to eq email + expect(finder2.email).to eq email + expect(finder1.customer).to be customer + expect(finder2.customer).to be customer + expect(finder1.user).to be user + expect(finder2.user).to be user end end @@ -27,58 +27,58 @@ module OpenFoodNetwork let(:email2) { 'test2@example.com' } let(:user2) { create(:user) } let(:customer2) { create(:customer) } - let(:lua1) { LastUsedAddress.new(user2, email, email2, customer2, user, customer) } - let(:lua2) { LastUsedAddress.new(email2, customer, user, email, user2, customer2) } + let(:finder1) { AddressFinder.new(user2, email, email2, customer2, user, customer) } + let(:finder2) { AddressFinder.new(email2, customer, user, email, user2, customer2) } it "only stores the first encountered instance of a given class" do - expect(lua1.email).to eq email - expect(lua2.email).to eq email2 - expect(lua1.customer).to be customer2 - expect(lua2.customer).to be customer - expect(lua1.user).to be user2 - expect(lua2.user).to be user + expect(finder1.email).to eq email + expect(finder2.email).to eq email2 + expect(finder1.customer).to be customer2 + expect(finder2.customer).to be customer + expect(finder1.user).to be user2 + expect(finder2.user).to be user end end end describe "fallback_bill_address" do - let(:lua) { LastUsedAddress.new(email) } + let(:finder) { AddressFinder.new(email) } let(:address) { double(:address, clone: 'address_clone') } context "when a last_used_bill_address is found" do - before { allow(lua).to receive(:last_used_bill_address) { address } } + before { allow(finder).to receive(:last_used_bill_address) { address } } it "returns a clone of the bill_address" do - expect(lua.send(:fallback_bill_address)).to eq "address_clone" + expect(finder.send(:fallback_bill_address)).to eq "address_clone" end end context "when no last_used_bill_address is found" do - before { allow(lua).to receive(:last_used_bill_address) { nil } } + before { allow(finder).to receive(:last_used_bill_address) { nil } } it "returns a new empty address" do - expect(lua.send(:fallback_bill_address)).to eq Spree::Address.default + expect(finder.send(:fallback_bill_address)).to eq Spree::Address.default end end end describe "fallback_ship_address" do - let(:lua) { LastUsedAddress.new(email) } + let(:finder) { AddressFinder.new(email) } let(:address) { double(:address, clone: 'address_clone') } context "when a last_used_ship_address is found" do - before { allow(lua).to receive(:last_used_ship_address) { address } } + before { allow(finder).to receive(:last_used_ship_address) { address } } it "returns a clone of the ship_address" do - expect(lua.send(:fallback_ship_address)).to eq "address_clone" + expect(finder.send(:fallback_ship_address)).to eq "address_clone" end end context "when no last_used_ship_address is found" do - before { allow(lua).to receive(:last_used_ship_address) { nil } } + before { allow(finder).to receive(:last_used_ship_address) { nil } } it "returns a new empty address" do - expect(lua.send(:fallback_ship_address)).to eq Spree::Address.default + expect(finder.send(:fallback_ship_address)).to eq Spree::Address.default end end end @@ -89,7 +89,7 @@ module OpenFoodNetwork let(:order) { create(:completed_order_with_totals, user: nil, email: email, distributor: distributor) } context "when an email has not been provided" do - let(:lua) { LastUsedAddress.new(nil) } + let(:finder) { AddressFinder.new(nil) } context "and an order with a bill address exists" do before do @@ -97,19 +97,19 @@ module OpenFoodNetwork end it "returns nil" do - expect(lua.send(:last_used_bill_address)).to eq nil + expect(finder.send(:last_used_bill_address)).to eq nil end end end context "when an email has been provided" do - let(:lua) { LastUsedAddress.new(email) } + let(:finder) { AddressFinder.new(email) } context "and an order with a bill address exists" do before { order.update_attribute(:bill_address_id, address.id) } it "returns the bill_address" do - expect(lua.send(:last_used_bill_address)).to eq address + expect(finder.send(:last_used_bill_address)).to eq address end end @@ -117,13 +117,13 @@ module OpenFoodNetwork before { order } it "return nil" do - expect(lua.send(:last_used_bill_address)).to eq nil + expect(finder.send(:last_used_bill_address)).to eq nil end end context "when no orders exist" do it "returns nil" do - expect(lua.send(:last_used_bill_address)).to eq nil + expect(finder.send(:last_used_bill_address)).to eq nil end end end @@ -137,7 +137,7 @@ module OpenFoodNetwork let(:order) { create(:completed_order_with_totals, user: nil, email: email, distributor: distributor) } context "when an email has not been provided" do - let(:lua) { LastUsedAddress.new(nil) } + let(:finder) { AddressFinder.new(nil) } context "and an order with a required ship address exists" do before do @@ -146,13 +146,13 @@ module OpenFoodNetwork end it "returns nil" do - expect(lua.send(:last_used_ship_address)).to eq nil + expect(finder.send(:last_used_ship_address)).to eq nil end end end context "when an email has been provided" do - let(:lua) { LastUsedAddress.new(email) } + let(:finder) { AddressFinder.new(email) } context "and an order with a ship address exists" do before { order.update_attribute(:ship_address_id, address.id) } @@ -161,7 +161,7 @@ module OpenFoodNetwork before { order.update_attribute(:shipping_method_id, delivery.id) } it "returns the ship_address" do - expect(lua.send(:last_used_ship_address)).to eq address + expect(finder.send(:last_used_ship_address)).to eq address end end @@ -169,7 +169,7 @@ module OpenFoodNetwork before { order.update_attribute(:shipping_method_id, pickup.id) } it "returns nil" do - expect(lua.send(:last_used_ship_address)).to eq nil + expect(finder.send(:last_used_ship_address)).to eq nil end end end @@ -178,13 +178,13 @@ module OpenFoodNetwork before { order } it "return nil" do - expect(lua.send(:last_used_ship_address)).to eq nil + expect(finder.send(:last_used_ship_address)).to eq nil end end context "when no orders exist" do it "returns nil" do - expect(lua.send(:last_used_ship_address)).to eq nil + expect(finder.send(:last_used_ship_address)).to eq nil end end end