Skip to content

Use save instead of update_columns in the OrderUpdater#425

Merged
jhawthorn merged 3 commits intosolidusio:masterfrom
jhawthorn:order_updater_save
Jan 14, 2016
Merged

Use save instead of update_columns in the OrderUpdater#425
jhawthorn merged 3 commits intosolidusio:masterfrom
jhawthorn:order_updater_save

Conversation

@jhawthorn
Copy link
Copy Markdown
Contributor

This uses a plan-old #save(validate: false) instead of using update_columns. A few changes were needed to make this work:

  • Removing homogenize_line_item_currencies, which could cause an order update on an order save.
  • Tweaking the Importer::Order so that order.contents.add wasn't called with an unsaved shipment on the order.

I don't think this would be considered a change to our public API, but it has some potential to cause issues in other stores. However, I think it's also a significant improvement, and would allow after_save and similar AR hooks to actually work as expected.

Looking for thoughts on:

  • What is the point of homogenize_line_item_currencies? It was unit tested, but I don't understand what sort of store would need this behaviour.
  • Can we make this change to the Updater.

@Senjai
Copy link
Copy Markdown
Contributor

Senjai commented Oct 8, 2015

I'm 👍, having recently made changes so that attribute changes are present for the order updater hooks:

Ideally, I wouldn't want to register an update hook with order here, but seeing as though the states I was interested in were only changing with update_columns, there was no other way I could add behavior around situations where those states change.

Changing this to save would allow me to use Active Record hooks instead, which is considered the "proper" approach for the specific problem I was attempting to solve.

I also have no idea what homogenize_line_item_currencies is really for.

@gmacdougall
Copy link
Copy Markdown
Member

This is a great step in the right direction. 👍

@magnusvk
Copy link
Copy Markdown
Contributor

magnusvk commented Oct 8, 2015

Well, we're not multi-currency, so we don't care about homogenize_line_item_currencies. From reading the spec, it seems that method is intended to support switching currencies on an order. So if you flip an order from USD to EUR, say, it would also flip all the line items to EUR if there's a EUR price available or raise an error otherwise. That sounds like sensible behavior to me, but it's certainly not something we rely on.

Anyhow, I'm super excited about eliminating updates without callbacks, so 👍 on this approach. Still a little nervous about the potential impact of this. Perhaps we should put this on a dev box and see how it works for us?

@Senjai
Copy link
Copy Markdown
Contributor

Senjai commented Oct 8, 2015

I would expect that the most significant issues would come with:

  • Callbacks now being triggered when they weren't before on orders.
  • If anyone uses update! in a scenario when the order is saved, a stack level too deep error could occur where it wouldn't before.
  • Mystery currency stuff Magnus talked about.

@jhawthorn
Copy link
Copy Markdown
Contributor Author

Another potential for issues: unpersisted associated records will now be saved on an update!

@athal7 athal7 added this to the 1.2.0 milestone Oct 21, 2015
@jhawthorn jhawthorn changed the title [RFC] Use save instead of update_columns in the OrderUpdater Use save instead of update_columns in the OrderUpdater Oct 21, 2015
@mtomov
Copy link
Copy Markdown
Contributor

mtomov commented Oct 27, 2015

To help a bit with the CurrencyUpdater mystery. At woolandthegang, we were dealing with 3 currencies. There was a Spree extension for switching currency, which was effectively changing the session[:currency] variable.

The session[:currency] variable is returned from the #current_currency method and used below through the #current_order_params to search for a previous order by token.

So, no change of order currency is happening at the initial current_order lookup. Now, the other place I was thinking where this one can be called from is the #merge! method on Order. However, you will see that this is only merging line items with the same currency.

So, to my experience, there is no code or interface in Spree to actually change the currency of an order.

Furthermore that concern is so fundamentally broken:

  • business logic class is persisting data (no single responsibility .. )
  • after_update with an if condition on Order.rb is missing tests
  • the after_update "catch all" is clearly hard to reason about. This class should be explicitly called from where the actual currency change is happening.

So, either delete it, or change it to only assign changes to the Order without persistence. I'm favouring deletion.

@gmacdougall
Copy link
Copy Markdown
Member

As the person who wrote most of the initial multi-currency code, I should probably weigh in here.

The intent when I was designing the multi-currency system was to explicitly not allow people to switch the currency of an order. If you switch currencies, you get a new order. This avoids all of the problems that homogenize_line_item_currencies tries to solve, but doesn't do a particularly good job of.

I am in favour of removing it, with a note in the release notes to explain why it has gone.

@magnusvk
Copy link
Copy Markdown
Contributor

Does this still look like something we'd like to ship? It does to me!

@mtomov
Copy link
Copy Markdown
Contributor

mtomov commented Dec 17, 2015

I don't see why not. Maybe @jhawthorn is a bit out of time.

@jhawthorn
Copy link
Copy Markdown
Contributor Author

Rebased. This is still good to go by me.

@jhawthorn jhawthorn removed their assignment Dec 30, 2015
@jhawthorn
Copy link
Copy Markdown
Contributor Author

Rebased again

@cbrunsdon
Copy link
Copy Markdown
Contributor

I am pretty scared by the change of the persist_totals call.

I'm very pro this change overall, but aren't comfortable merging it in just before we get 1.2 ready, so @jhawthorn has agreed to merge it very early in 1.3.

Stores that heavily abuse callbacks could get affected by the extra ones running on the .save, and if there were problems caused by that change they might take a while to figure out something wrong is even happening, with potential large impact to a running store.

In short: 👍 for 1.3

@cbrunsdon cbrunsdon modified the milestones: 1.3.0, 1.2.0 Jan 11, 2016
jhawthorn added a commit that referenced this pull request Jan 14, 2016
Use save instead of update_columns in the OrderUpdater
@jhawthorn jhawthorn merged commit e31c273 into solidusio:master Jan 14, 2016
@jhawthorn
Copy link
Copy Markdown
Contributor Author

Merging. We want this to get as much testing and exposure as possible before our 1.3 release.

@jhawthorn jhawthorn deleted the order_updater_save branch January 14, 2016 22:51
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