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

Improve JS handling for OrdersController #1440

Merged
merged 2 commits into from
Sep 29, 2016
Merged

Improve JS handling for OrdersController #1440

merged 2 commits into from
Sep 29, 2016

Conversation

mtomov
Copy link
Contributor

@mtomov mtomov commented Sep 14, 2016

  • Using an instance variable allows JS populate operations to have
    access to the @order from within the rendered .js.erb files
  • Assign all line_item errors to the order's base
  • Allow to respond to JS requests for #populate and #update actions.
    It is left for the developer to actually implement the js.erb views
  • Adjusts #update to perform the behave properly for JS actions as well

To consider:

@@ -5,6 +5,7 @@ class OrdersController < Spree::StoreController
helper 'spree/products', 'spree/orders'

respond_to :html
respond_to :js, only: [:populate, :update]
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 can actually get rid of this one if you prefer, as one can easily just add the line in a decorator.

Copy link
Contributor

@jhawthorn jhawthorn Sep 14, 2016

Choose a reason for hiding this comment

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

I would prefer that since the JS response will not be in core. Otherwise, 👍

it "should advance the order if :checkout button is pressed" do
allow(order).to receive(:update_attributes).and_return true
expect(order).to receive(:next)
put :update, {checkout: true}, { order_id: 1 }

Choose a reason for hiding this comment

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

Space inside { missing.
Space inside } missing.

@@ -18,10 +18,11 @@ def show

def update
if @order.contents.update_cart(order_params)
@order.next if params.has_key?(:checkout) && @order.cart?

Choose a reason for hiding this comment

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

Use Hash#key? instead of Hash#has_key?.

@mtomov mtomov changed the title Improve access to @order from populate action Improve JS handling for OrdersController Sep 18, 2016
@mtomov
Copy link
Contributor Author

mtomov commented Sep 19, 2016

I've actually changed the code around, and also modified the #update method, so if you'd like to have a new look, that would be great.

@jhawthorn
Copy link
Contributor

Still looking good to me.

@mtomov could you please change the target of this PR to the master branch?

@mtomov mtomov changed the base branch from v1.4 to master September 20, 2016 07:48
  * Using an instance variable allows JS populate operations to have
    access to the @order from within the rendered .js.erb files

  * Assign all line_item errors to the order's base

  * Allow to respond to JS requests for `#populate` and `#update` actions.
    It is left for the developer to actually implement the `js.erb` views

To consider / TODO:

  - to avoid the whole rescue operation, and manually assigning line item
    errors to the order, we'd need to do to be doing `order.save`
    instead of `line_item.save!` in [`OrderContents`](https://github.com/solidusio/solidus/blob/master/core/app/models/spree/order_contents.rb#L151)

  -> That would have the effect to "automatically" assign the line item
     errors on the order in the form: `line_items.quantity` in Rails 4.x
     and "line_items[0].quantity" in Rails 5

  More info:
  - rails/rails#8638
  - rails/rails#19686
  - move the non-redirect operations out of the html handling block, so
    that they will also be executed on a JS format call

  - add missing test for the click on the `checkout` button, which is
    meant to advance the order instead of only updating the cart
@cbrunsdon
Copy link
Contributor

Looks reasonable to me 👍

@jhawthorn jhawthorn merged commit 9912c99 into solidusio:master Sep 29, 2016
@jhawthorn
Copy link
Contributor

Thanks again @mtomov

@mtomov mtomov deleted the improve-populate-for-ajax branch September 30, 2016 08:11
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.

4 participants