-
-
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
Improve JS handling for OrdersController #1440
Conversation
@@ -5,6 +5,7 @@ class OrdersController < Spree::StoreController | |||
helper 'spree/products', 'spree/orders' | |||
|
|||
respond_to :html | |||
respond_to :js, only: [:populate, :update] |
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 can actually get rid of this one if you prefer, as one can easily just add the line in a decorator.
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 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 } |
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.
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? |
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.
Use Hash#key? instead of Hash#has_key?.
I've actually changed the code around, and also modified the |
Still looking good to me. @mtomov could you please change the target of this PR to the |
* 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
Looks reasonable to me 👍 |
Thanks again @mtomov |
access to the @order from within the rendered
.js.erb
files#populate
and#update
actions.It is left for the developer to actually implement the
js.erb
views#update
to perform the behave properly for JS actions as wellTo consider:
to avoid the whole rescue operation, and manually assigning line item
errors to the order's
:base
, we'd need to do to be doingorder.save
instead of
line_item.save!
inOrderContents
-> That would have the effect to "automatically" assign the line item
errors on the order in the form:
line_items.quantity
in Rails 4.xand "line_items[0].quantity" in Rails 5
More info:
Improve a way to handle errors for collection nested attributes rails/rails#8638
Errors can be indexed with nested attributes rails/rails#19686