Skip to content

Commit

Permalink
Polish some styles and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
oldjackson committed Apr 26, 2020
1 parent 91b54fc commit f5f106d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
18 changes: 14 additions & 4 deletions core/app/models/concerns/spree/user_address_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ def find_first_by_address_values(address_attrs)
def mark_default(user_address, address_type: :shipping)
column_for_default = address_type == :shipping ? :default : :default_billing
ActiveRecord::Base.transaction do
(self - [user_address]).each do |ua| # update_all would be nice, but it bypasses ActiveRecord callbacks
ua.persisted? ? ua.update!({ column_for_default => false }) : ua.write_attribute(column_for_default, false)
(self - [user_address]).each do |address| # update_all would be nice, but it bypasses ActiveRecord callbacks
if address.persisted?
address.update!(column_for_default => false)
else
address.write_attribute(column_for_default, false)
end
end

if user_address.persisted?
user_address.update!(column_for_default => true, archived: false)
else
user_address.write_attribute(column_for_default, true)
end
user_address.persisted? ? user_address.update!({ column_for_default => true, :archived => false }) : user_address.write_attribute(column_for_default, true)
end
end
end
Expand Down Expand Up @@ -112,7 +121,8 @@ def persist_order_address(order)
# @param default set whether or not this address will show up from
# #default_address or not
def save_in_address_book(address_attributes, default = false, address_type = :shipping)
return nil unless address_attributes.present?
return nil if address_attributes.blank?

address_attributes = address_attributes.to_h.with_indifferent_access

new_address = Spree::Address.factory(address_attributes)
Expand Down
14 changes: 7 additions & 7 deletions core/spec/models/spree/concerns/user_address_book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Spree
let!(:user) { create(:user) }

describe "#save_in_address_book" do
context "saving a shipping address" do
context "saving a default shipping address" do
let(:user_address) { user.user_addresses.find_first_by_address_values(address.attributes) }

subject do
Expand Down Expand Up @@ -45,7 +45,7 @@ module Spree
end
end

it "adds the address to the user's the associated addresses" do
it "adds the address to the user's associated addresses" do
is_expected.to change { user.reload.addresses.count }.by(1)
end
end
Expand Down Expand Up @@ -404,7 +404,7 @@ module Spree
end
end

context "when one order address is nil" do
context "when either ship_address or bill_address is nil" do
context "when automatic_default_address preference is at a default of true" do
before do
stub_spree_preferences(automatic_default_address: true)
Expand All @@ -428,20 +428,20 @@ module Spree
end

context "when automatic_default_address preference is false" do
let(:order) { build(:order) }

before do
stub_spree_preferences(automatic_default_address: false)
end

it "does not call save_in_address_book on ship address" do
order = build(:order)
order.ship_address = nil

expect(user).to receive(:save_in_address_book).with(kind_of(Hash), false, :billing).once
user.persist_order_address(order)
end

it "does not call save_in_address_book on bill address" do
order = build(:order)
order.bill_address = nil

expect(user).to receive(:save_in_address_book).with(kind_of(Hash), false).once
Expand All @@ -465,7 +465,7 @@ module Spree
let!(:address) { create(:address) }

# https://github.com/solidusio/solidus/issues/1241
it "resets the association and persists" do
it "resets the association and persists the ship_address" do
# Load (which will cache) the has_one association
expect(user.ship_address).to be_nil

Expand All @@ -482,7 +482,7 @@ module Spree
let!(:address) { create(:address) }

# https://github.com/solidusio/solidus/issues/1241
it "resets the association and persists" do
it "resets the association and persists the bill_address" do
# Load (which will cache) the has_one association
expect(user.bill_address).to be_nil

Expand Down

0 comments on commit f5f106d

Please sign in to comment.