Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix geocoding addresses, add specs
Łukasz Adamczyk4caa81cf3a7c
diff --git a/api/spec/serializers/spree/api/v2/platform/address_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/address_serializer_spec.rbindex 7ecf3569a1..24ae0d5260 100644--- a/api/spec/serializers/spree/api/v2/platform/address_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/address_serializer_spec.rb@@ -27,6 +27,8 @@ describe Spree::Api::V2::Platform::AddressSerializer dodeleted_at: address.deleted_at,label: address.label,quick_checkout: false,+ latitude: address.latitude,+ longitude: address.longitude,public_metadata: {},private_metadata: {}},diff --git a/api/spec/serializers/spree/api/v2/platform/property_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/property_serializer_spec.rbindex c61acb7843..1ce9f3f71a 100644--- a/api/spec/serializers/spree/api/v2/platform/property_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/property_serializer_spec.rb@@ -20,6 +20,7 @@ describe Spree::Api::V2::Platform::PropertySerializer doupdated_at: resource.updated_at,filterable: resource.filterable,filter_param: resource.filter_param,+ kind: resource.kind,display_on: resource.display_on,position: resource.position,public_metadata: resource.public_metadata,diff --git a/core/app/jobs/spree/addresses/geocode_address_job.rb b/core/app/jobs/spree/addresses/geocode_address_job.rbindex 740dc6dee5..2365a1404a 100644--- a/core/app/jobs/spree/addresses/geocode_address_job.rb+++ b/core/app/jobs/spree/addresses/geocode_address_job.rb@@ -19,7 +19,7 @@ module Spree# Unfortunately there is no way to get the error message from Geocoder,# but the request is fully displayed in the server logsSpree::ErrorHandler.call(- exception: GeocodeAddressError.new("Cannot geocode address #{address.id}"),+ exception: GeocodeAddressError.new("Cannot geocode address ID: #{address.id}"),opts: { report_context: { address_id: address_id } })enddiff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rbindex 504c55efa9..551c937972 100644--- a/core/app/models/spree/address.rb+++ b/core/app/models/spree/address.rb@@ -217,7 +217,9 @@ module Spreeprivatedef should_geocode?- saved_changes.key?(:address1) || saved_changes.key?(:city) || saved_changes.key?(:state_id) || saved_changes.key?(:country_id)+ Spree::Config[:geocode_addresses] && (+ saved_changes.key?(:address1) || saved_changes.key?(:city) || saved_changes.key?(:state_id) || saved_changes.key?(:country_id)+ )enddef set_default_valuesdiff --git a/core/app/services/spree/error_handler.rb b/core/app/services/spree/error_handler.rbindex 9ce7bec88e..68d2859a56 100644--- a/core/app/services/spree/error_handler.rb+++ b/core/app/services/spree/error_handler.rb@@ -23,7 +23,7 @@ module Spreedef log_error(exception:, message:, opts:)Rails.logger.error messageRails.logger.error "User ID: #{opts[:user]&.id}" if opts[:user]- Rails.logger.error exception.backtrace.join("\n")+ Rails.logger.error exception.backtrace.join("\n") if exception.backtrace.present?success(exception: exception, message: message, opts: opts)enddiff --git a/core/db/migrate/20250127112758_add_latitude_and_longitude_to_spree_addresses.rb b/core/db/migrate/20250127112758_add_latitude_and_longitude_to_spree_addresses.rbnew file mode 100644index 0000000000..57e788933e--- /dev/null+++ b/core/db/migrate/20250127112758_add_latitude_and_longitude_to_spree_addresses.rb@@ -0,0 +1,6 @@+class AddLatitudeAndLongitudeToSpreeAddresses < ActiveRecord::Migration[6.1]+ def change+ add_column :spree_addresses, :latitude, :decimal, if_not_exists: true+ add_column :spree_addresses, :longitude, :decimal, if_not_exists: true+ end+enddiff --git a/core/lib/spree/core/configuration.rb b/core/lib/spree/core/configuration.rbindex cb2748b27f..336afd95af 100644--- a/core/lib/spree/core/configuration.rb+++ b/core/lib/spree/core/configuration.rb@@ -44,6 +44,7 @@ module Spreepreference :disable_store_presence_validation, :boolean, default: false # when turned off disables Store presence validation for Products and Payment Methodspreference :expedited_exchanges, :boolean, default: false # NOTE this requires payment profiles to be supported on your gateway of choice as well as a delayed job handler to be configured with activejob. kicks off an exchange shipment upon return authorization save. charge customer if they do not return items within timely manner.preference :expedited_exchanges_days_window, :integer, default: 14 # the amount of days the customer has to return their item after the expedited exchange is shipped in order to avoid being charged+ preference :geocode_addresses, :boolean, default: truepreference :layout, :string, deprecated: 'Please use Spree::Frontend::Config[:layout] instead'preference :logo, :string, deprecated: truepreference :mailer_logo, :string, deprecated: truediff --git a/core/spec/jobs/spree/addresses/geocode_address_job_spec.rb b/core/spec/jobs/spree/addresses/geocode_address_job_spec.rbnew file mode 100644index 0000000000..ded44f214f--- /dev/null+++ b/core/spec/jobs/spree/addresses/geocode_address_job_spec.rb@@ -0,0 +1,57 @@+require 'spec_helper'++RSpec.describe Spree::Addresses::GeocodeAddressJob, :vcr do+ subject(:geocode_address) { described_class.new.perform(address.id) }++ let(:address) do+ create(+ :address,+ address1: '100 California Street',+ city: 'San Francisco',+ zipcode: '94111',+ state: california_state,+ country: usa_country+ )+ end++ let(:usa_country) { Spree::Country.find_by(iso: 'US') || create(:usa_country) }+ let(:california_state) { create(:state, name: 'California', abbr: 'CA', country: usa_country) }++ let(:geocoder_coordinates) { [37.8, -122.4] }++ before do+ allow(Geocoder).to receive(:coordinates).+ with(address.geocoder_address, country: address.country_iso3).+ and_return(geocoder_coordinates)+ end++ it 'geocodes the address' do+ geocode_address++ expect(address.reload.latitude).to be_present+ expect(address.longitude).to be_present+ end++ context 'when the address cannot be geocoded' do+ let(:geocoder_coordinates) { nil }+ let(:error_handler) { instance_double(Spree::ErrorHandler, call: nil) }++ before do+ allow(Spree::ErrorHandler).to receive(:new).and_return(error_handler)+ allow(error_handler).to receive(:call)+ end++ it 'handles the error' do+ geocode_address++ expect(error_handler).to have_received(:call).+ with(+ exception: Spree::Addresses::GeocodeAddressError.new("Cannot geocode address ID: #{address.id}"),+ opts: { report_context: { address_id: address.id } }+ )++ expect(address.reload.latitude).to be_nil+ expect(address.longitude).to be_nil+ end+ end+enddiff --git a/core/spec/models/spree/base_spec.rb b/core/spec/models/spree/base_spec.rbindex 2c1d12a817..3e17b73191 100644--- a/core/spec/models/spree/base_spec.rb+++ b/core/spec/models/spree/base_spec.rb@@ -57,11 +57,11 @@ describe Spree::Base doexpect(Spree::LegacyUser.json_api_columns).to include('email')end- it { expect(Spree::Address.json_api_columns).to contain_exactly('address1', 'address2', 'alternative_phone', 'city', 'company', 'created_at', 'deleted_at', 'firstname', 'label', 'lastname', 'phone', 'state_name', 'updated_at', 'zipcode', 'public_metadata', 'private_metadata', 'quick_checkout') }+ it { expect(Spree::Address.json_api_columns).to contain_exactly('address1', 'address2', 'alternative_phone', 'city', 'company', 'created_at', 'deleted_at', 'firstname', 'label', 'lastname', 'phone', 'state_name', 'updated_at', 'zipcode', 'public_metadata', 'private_metadata', 'quick_checkout', 'latitude', 'longitude') }it { expect(Spree::Address.json_api_columns).not_to include('country_id') }enddescribe '.json_api_permitted_attributes' do- it { expect(Spree::Address.json_api_permitted_attributes).to contain_exactly('firstname', 'lastname', 'address1', 'address2', 'city', 'zipcode', 'phone', 'state_name', 'alternative_phone', 'company', 'state_id', 'country_id', 'created_at', 'updated_at', 'user_id', 'deleted_at', 'label', 'public_metadata', 'private_metadata', 'quick_checkout') }+ it { expect(Spree::Address.json_api_permitted_attributes).to contain_exactly('firstname', 'lastname', 'address1', 'address2', 'city', 'zipcode', 'phone', 'state_name', 'alternative_phone', 'company', 'state_id', 'country_id', 'created_at', 'updated_at', 'user_id', 'deleted_at', 'label', 'public_metadata', 'private_metadata', 'quick_checkout', 'latitude', 'longitude') }endend