-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Uniform bill_address and ship_address behaviour in Spree::UserAddressBook module #3563
Conversation
core/db/migrate/20200320144521_add_default_billng_flag_to_user_addresses.rb
Outdated
Show resolved
Hide resolved
core/db/migrate/20200320144521_add_default_billng_flag_to_user_addresses.rb
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
context "shipping address" do | ||
it do | ||
subject | ||
expect(user.default_address).to eq address |
There was a problem hiding this comment.
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 }
There was a problem hiding this comment.
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
0336f71
to
ae300ca
Compare
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.
ae300ca
to
f6fdf08
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (long ago)
f9c3ff0
to
0619618
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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! " \ |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
0619618
to
decc624
Compare
@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. |
decc624
to
5c9dafe
Compare
I've just force-pushed again. CircleCI seems to be working now. |
5c9dafe
to
f2e2043
Compare
There was a problem hiding this 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
f5f106d
to
e3b1fbf
Compare
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
e3b1fbf
to
2abbf81
Compare
There was a problem hiding this 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!
72f4114
to
49347c3
Compare
@aldesantis you've broken it! 😆 |
49347c3
to
d9a5df5
Compare
@kennyadsl sorry! 🙏 Fixed it now. |
@aldesantis great, can you please change your review status? It's still |
@kennyadsl done! |
Responding to the issue #3539, the association
has_one :bill_address
provided byUserAddressBook
toLegacyUser
has been made similar to the one already in place fordefault_address
aliasship_address
.Namely,
user.bill_address
is now theAddress
pointed by the onlyUserAddress
having the newdefault_billing
flag column set totrue
, which plays the same role as thedefault
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.