Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniform bill_address and ship_address behaviour in Spree::UserAddressBook module #3563

Merged
merged 9 commits into from
May 22, 2020

Conversation

oldjackson
Copy link
Contributor

@oldjackson oldjackson commented Mar 20, 2020

Responding to the issue #3539, the association has_one :bill_address provided by UserAddressBook to LegacyUser has been made similar to the one already in place for default_address alias ship_address.

Namely, user.bill_address is now the Address pointed by the only UserAddress having the new default_billing flag column set to true, which plays the same role as the default one (which retains the function of selecting the one and only default shipping address).

Being now possible to set the two default addresses independently,

  • #persist_order_address now calls #save_in_address_book in the same way for the two addresses, solving the issue.

  • the new public methods #mark_default_ship_address and #mark_default_bill_address are provided. #mark_default_address just calls the former for backward compatibility, and can be deprecated.

@oldjackson oldjackson changed the title Reorder #persist_order_address spec Uniform bill_address and ship_address behaviour in Spree::UserAddressBook module Mar 20, 2020
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @oldjackson. I left a lot of comments but most of them are about code style and syntax. I think your work here has been fantastic, thanks.

Let me also add that you could improve how commits are organized by squashing some of them and adding some meaningful commit descriptions that explain the why of each change.

Thanks again.

core/app/models/concerns/spree/user_address_book.rb Outdated Show resolved Hide resolved
core/app/models/concerns/spree/user_address_book.rb Outdated Show resolved Hide resolved
core/app/models/concerns/spree/user_address_book.rb Outdated Show resolved Hide resolved
core/app/models/concerns/spree/user_address_book.rb Outdated Show resolved Hide resolved
core/app/models/concerns/spree/user_address_book.rb Outdated Show resolved Hide resolved
core/spec/models/spree/concerns/user_address_book_spec.rb Outdated Show resolved Hide resolved
context "shipping address" do
it do
subject
expect(user.default_address).to eq address
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use this syntax: it { is_expected.to eq address }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this case, as I'm not doing an expect(subject).to, but I did it in the following one

core/spec/models/spree/concerns/user_address_book_spec.rb Outdated Show resolved Hide resolved
core/spec/models/spree/concerns/user_address_book_spec.rb Outdated Show resolved Hide resolved
core/spec/models/spree/concerns/user_address_book_spec.rb Outdated Show resolved Hide resolved
The association 'belongs_to :bill_address' is superseded by 'has_one :bill_address, through: :default_user_bill_address' so to mimick what already happens for :ship_address and use the same logics around.
@oldjackson oldjackson force-pushed the persist-order-address-fix branch from ae300ca to f6fdf08 Compare March 24, 2020 22:29
Copy link
Member

@aldesantis aldesantis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oldjackson thanks, I just left a couple of comments! Also, it looks like tests are failing because they're calling some of the APIs you deprecated. Could you take a look at those?

expect(user.default_address).to eq address
context "sets it as default" do
context "shipping address" do
it do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it blocks should always have a description unless you're using one-liners, so that it's clear what you're testing in the example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (long ago)

context "billing address" do
subject { user.save_in_address_book(address.attributes, true, :billing) }

it do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for the other: can you add a description here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (long ago)

@oldjackson oldjackson force-pushed the persist-order-address-fix branch from f9c3ff0 to 0619618 Compare March 29, 2020 15:40
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of non-blocking things if you have time. Otherwise, I'm ok with this version as well. I still think we need to revisit commits and create a cleaner git history. Thanks again!

end

def default_address=(address)
Spree::Deprecation.warn "This setter does not take into account Spree::Config.automatic_default_address and is deprecated. "\
"Please start using #ship_address="
save_in_address_book(address.attributes, true) if address
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using ship_address = address here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@kennyadsl kennyadsl Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is still not done actually, I meant:

def default_address=(address)
  Spree::Deprecation.warn "This setter does not take into account Spree::Config.automatic_default_address and is deprecated. "\
    "Please start using ship_address = address"
  ship_address = address
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

end

def bill_address=(address)
# stow a copy in our address book too
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this comment, please? I think the code is already self-explanatory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

end

user_address.address
end

def mark_default_address(address)
Spree::Deprecation.warn "Hey, this method is deprecated and it sets the ship_address only! " \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I initially suggested how to formulate this sentence, but Hey, wasn't meant to end up in real code. :P
Can you please restyle this message in line with the tone of the other deprecations (just a little bit more formal).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@oldjackson oldjackson force-pushed the persist-order-address-fix branch from 0619618 to decc624 Compare March 31, 2020 08:08
@kennyadsl
Copy link
Member

kennyadsl commented Apr 20, 2020

@oldjackson Any chance you could force push here? I'd love to merge this but CircleCI is stuck for some reason. I already tried to rerun specs there but the status is not updating on GitHub.

@oldjackson oldjackson force-pushed the persist-order-address-fix branch from decc624 to 5c9dafe Compare April 20, 2020 15:22
@oldjackson
Copy link
Contributor Author

oldjackson commented Apr 20, 2020

I've just force-pushed again. CircleCI seems to be working now.

Copy link
Member

@spaghetticode spaghetticode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oldjackson thanks for working on this, it surely was not an easy task especially considering the vast amount of tests you had to modify and add.

I've left a few notes, also I've noticed that the commit message bodies are on a single line. Can you re-wrap them to a similar length of the other ones in this project, so they become more readable and they don't look weird on the git log?

ActiveRecord::Base.transaction do
(self - [user_address]).each do |ua| # update_all would be nice, but it bypasses ActiveRecord callbacks
ua.persisted? ? ua.update!(default: false) : ua.default = false
ua.persisted? ? ua.update!({ column_for_default => false }) : ua.write_attribute(column_for_default, false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're already changing these lines, I think it would make sense to replace the non-communicative variable name ua with something more meaningful... what do you think?

Also, I would remove reduntant curly braces from here update!({ column_for_default => false }) and from line 19 below

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly it may also be nice to not use ternary operator for better readability as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -11,56 +11,98 @@ module Spree
let!(:user) { create(:user) }

describe "#save_in_address_book" do
context "saving a default address" do
context "saving a shipping address" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need to keep the word default in the description, as this block subject is saving a default shipping address.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

end
end

context "when address is nil" do
context "when one order address is nil" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when the order sounds more correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to keep the emphasis on the case when one of the two addresses inside the order is nil. I rephrased it for better clarity (or did I?).

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line (which is repeated in the next test) may be extracted in a let

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let!(:address) { create(:address) }

# https://github.com/solidusio/solidus/issues/1241
it "resets the association and persists" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that adding exactly what gets persisted may add clarity to the message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@skukx skukx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a couple non-necessary suggestions

subject
expect(user_address.default).to be_falsey
expect(user_address.default_billing).to eq true
end
end

it "adds the address to the user's the associated addresses" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra the:

'adds the address to the user's the associated addresses' # extra the
'adds the address to the user's associated addresses' # cleaned up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Having the now symmetric :ship_address and :bill_address we can define
also the specific methods to mark their defaults, #mark_default_ship_address
and #mark_default_bill_address. The old mark_default_address is kept for
compatibility and calls the former, but gets deprecated.
In #mark_default and #save_in_address_book the argument changes name.
In #mark_default it also becomes a keyword argument for the sake of clarity.

Other style changes are also made.
@oldjackson oldjackson force-pushed the persist-order-address-fix branch 3 times, most recently from f5f106d to e3b1fbf Compare April 26, 2020 22:42
oldjackson added 4 commits May 1, 2020 13:27
The association #default_address and #default_user_address get deprecated
in favor of #ship_address #default_user_ship_address. The related setters
are also deprecated. The setters maintain their present definition as they
don't take into account Spree::Config.automatic_default_address.
For better clarity the scope ::default_shipping is defined along with
::default_billing. ::default is redefined in terms of the former, and
deprecated.
Where suitable, expectations are rewritten to make use or RSpec one-liners
@oldjackson oldjackson force-pushed the persist-order-address-fix branch from e3b1fbf to 2abbf81 Compare May 1, 2020 11:29
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @oldjackson, looking forward to merging this!

@kennyadsl kennyadsl requested a review from aldesantis May 4, 2020 15:11
@aldesantis aldesantis force-pushed the persist-order-address-fix branch from 72f4114 to 49347c3 Compare May 11, 2020 14:16
@kennyadsl
Copy link
Member

@aldesantis you've broken it! 😆

@aldesantis aldesantis force-pushed the persist-order-address-fix branch from 49347c3 to d9a5df5 Compare May 12, 2020 15:26
@aldesantis
Copy link
Member

@kennyadsl sorry! 🙏 Fixed it now.

@kennyadsl
Copy link
Member

@aldesantis great, can you please change your review status? It's still Changes Requested

@aldesantis
Copy link
Member

@kennyadsl done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants