mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Simplify main method by creating two private methods
Include some unit tests
This commit is contained in:
committed by
Maikel Linke
parent
19018c2564
commit
753a643bf1
@@ -26,22 +26,23 @@ module AddressTransformation
|
||||
}.with_indifferent_access[key]
|
||||
end
|
||||
|
||||
if address[:state].present?
|
||||
address[:state] = Spree::State.find_by(
|
||||
"LOWER(abbr) = ? OR LOWER(name) = ?",
|
||||
address.dig(:state, :code)&.downcase,
|
||||
address.dig(:state, :name)&.downcase,
|
||||
)
|
||||
end
|
||||
|
||||
if address[:country].present?
|
||||
address[:country] = Spree::Country.find_by(
|
||||
"LOWER(iso) = ? OR LOWER(name) = ?",
|
||||
address.dig(:country, :code)&.downcase,
|
||||
address.dig(:country, :name)&.downcase,
|
||||
)
|
||||
end
|
||||
address[:state] = find_state(address) if address[:state].present?
|
||||
address[:country] = find_country(address) if address[:country].present?
|
||||
|
||||
attributes["#{to}_attributes"] = address
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_state(address)
|
||||
Spree::State.find_by("LOWER(abbr) = ? OR LOWER(name) = ?",
|
||||
address.dig(:state, :code)&.downcase,
|
||||
address.dig(:state, :name)&.downcase)
|
||||
end
|
||||
|
||||
def find_country(address)
|
||||
Spree::Country.find_by("LOWER(iso) = ? OR LOWER(name) = ?",
|
||||
address.dig(:country, :code)&.downcase,
|
||||
address.dig(:country, :name)&.downcase)
|
||||
end
|
||||
end
|
||||
|
||||
97
spec/controllers/concerns/address_transformation_spec.rb
Normal file
97
spec/controllers/concerns/address_transformation_spec.rb
Normal file
@@ -0,0 +1,97 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe AddressTransformation do
|
||||
include AddressTransformation
|
||||
|
||||
describe "#transform_address!" do
|
||||
describe "default cases" do
|
||||
let(:attributes) do
|
||||
{ shipping_address: CustomerSchema.address_example,
|
||||
billing_address: CustomerSchema.address_example }
|
||||
end
|
||||
|
||||
let(:wanted_address) do
|
||||
CustomerSchema.address_example.
|
||||
except( :first_name, :last_name, :locality,
|
||||
:postal_code, :region, :street_address_1, :street_address_2).
|
||||
merge(
|
||||
firstname: "Alice",
|
||||
lastname: "Springs",
|
||||
address1: "1 Flinders Street",
|
||||
address2: "",
|
||||
zipcode: "1234",
|
||||
city: "Melbourne",
|
||||
state: Spree::State.find_by(name: "Victoria"),
|
||||
country: Spree::Country.find_by(name: "Australia"),
|
||||
)
|
||||
end
|
||||
|
||||
it "transforms the shipping_address and the billing_address" do
|
||||
transformed_address = transform_address!(attributes, :shipping_address, :ship_address)
|
||||
expect(transformed_address).to eq(wanted_address)
|
||||
expect(attributes["ship_address_attributes"]).to eq(wanted_address)
|
||||
|
||||
transformed_address = transform_address!(attributes, :billing_address, :bill_address)
|
||||
expect(transformed_address).to eq( wanted_address)
|
||||
expect(attributes["bill_address_attributes"]).to eq(wanted_address)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when the address attributes are nil" do
|
||||
let(:attributes) do
|
||||
{ shipping_address: nil,
|
||||
billing_address: nil }
|
||||
end
|
||||
|
||||
it "returns nil" do
|
||||
transformed_address = transform_address!(attributes, :shipping_address, :ship_address)
|
||||
expect(transformed_address).to be_nil
|
||||
expect(attributes["ship_address_attributes"]).to be_nil
|
||||
|
||||
transformed_address = transform_address!(attributes, :billing_address, :bill_address)
|
||||
expect(transformed_address).to be_nil
|
||||
expect(attributes["bill_address_attributes"]).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "when attributes doesn't contains the searched key" do
|
||||
let(:attributes) do
|
||||
{
|
||||
address: CustomerSchema.address_example,
|
||||
}
|
||||
end
|
||||
|
||||
it "returns nil" do
|
||||
transformed_address = transform_address!(attributes, :shipping_address, :ship_address)
|
||||
expect(transformed_address).to be_nil
|
||||
expect(attributes["ship_address_attributes"]).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#find_state" do
|
||||
it "finds the state" do
|
||||
state = find_state(
|
||||
state: {
|
||||
code: "VIC",
|
||||
name: "Victoria",
|
||||
},
|
||||
)
|
||||
expect(state).to eq(Spree::State.find_by(name: "Victoria"))
|
||||
end
|
||||
end
|
||||
|
||||
describe "#find_country" do
|
||||
it "finds the country" do
|
||||
country = find_country(
|
||||
country: {
|
||||
code: "AU",
|
||||
name: "Australia",
|
||||
},
|
||||
)
|
||||
expect(country).to eq(Spree::Country.find_by(name: "Australia"))
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user