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

Making simple coordinator insufficient stock to include a message #3577

Merged

Conversation

softr8
Copy link
Contributor

@softr8 softr8 commented Apr 3, 2020

It is hard to know what items were the ones that were
unavailable, this change includes item names that
were unable to be fullfilled, then this message
can be used in the frontend easily.

What do you think about this?

I saw that the frontend does some complex code to achieve kind of the same here

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@softr8 softr8 changed the title Making simple coordinator to include a message Making simple coordinator insufficient stock to include a message Apr 3, 2020
raise Spree::Order::InsufficientStock
item_names = leftover.variants.map(&:name).to_sentence
message = I18n.t('spree.inventory_error_flash_for_insufficient_quantity', names: item_names)
raise Spree::Order::InsufficientStock.new(message)
Copy link
Member

Choose a reason for hiding this comment

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

What about enhancing this spec

expect{ shipments }.to raise_error(Spree::Order::InsufficientStock)
to check also for the message?

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.

I think there's another path to explore here, let me know your thoughts.

raise Spree::Order::InsufficientStock
item_names = leftover.variants.map(&:name).to_sentence
message = I18n.t('spree.inventory_error_flash_for_insufficient_quantity', names: item_names)
raise Spree::Order::InsufficientStock.new(message)
Copy link
Member

Choose a reason for hiding this comment

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

I like the idea but I think we can take advantage of creating a custom exception that contains the unavailable items and let the consumer decide what to do with those.

What I have in mind is changing the exception definition to something like:

class InsufficientStock < StandardError
  attr_reader :items

  def initialize(message=nil, items: [])
    super(message)

    @items = items
  end
end

and push names in it with:

raise Spree::Order::InsufficientStock.new(items: leftovers)

At that point, we can consume the exception data in different ways depending on the context, even using rescue_from since its handler methods accept an argument that will be the exception containing the items.

rescue_from Spree::Order::InsufficientStock, with: :something 

def something(exception)
  # exception.items is available here!
end

What do you think?

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 like it! will refactor my code to take this path

@softr8 softr8 force-pushed the improving/insufficient-stock-exception branch from 3678847 to cecb3c1 Compare April 30, 2020 15:12
@softr8
Copy link
Contributor Author

softr8 commented Apr 30, 2020

What do you think about this new approach @kennyadsl

class InsufficientStock < StandardError
attr_reader :items

def initialize(message = nil, items: [])
Copy link
Member

Choose a reason for hiding this comment

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

Should the default value of items: be an array or a hash?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True! it returns a hash of items

@softr8 softr8 force-pushed the improving/insufficient-stock-exception branch from cecb3c1 to ace5ace Compare April 30, 2020 21:29
@kennyadsl
Copy link
Member

Rerunning specs, it's a flaky one!

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, @softr8!

Copy link
Member

@jarednorman jarednorman 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 to me! (Though there's a "simlpe" typo in the commit message that could be fixed.)

It is hard to know what items were the ones that were
unavailable, this change makes the exception to include
all the items that were unable to be fullfilled so they can
be rescued and then can be manipulated to build a better
error message
@softr8 softr8 force-pushed the improving/insufficient-stock-exception branch from ace5ace to 75f5e98 Compare May 5, 2020 17:26
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.

Very cool, thanks @softr8!

@aldesantis aldesantis merged commit afd7f5b into solidusio:master May 16, 2020
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.

5 participants