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

Added the :selected option with the default tax category #12989 #13009

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

MrBowmanXD
Copy link
Contributor

@MrBowmanXD MrBowmanXD commented Dec 1, 2024

This pull request solves the default tax category not showing problem when you create a new product in the admin panel

Added the following code:

selected: Spree::TaxCategory.find_by(is_default: true)&.id

in the _tax_category_form.html.haml file.

What should we test?

1.Open product creation screen.
2.See the tax category is the one that is set to default.

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

Copy link
Collaborator

@chahmedejaz chahmedejaz left a comment

Choose a reason for hiding this comment

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

Thank you so much for working on this. I was wondering if we could handle the use case shared below.

@@ -1,5 +1,5 @@
= f.field_container :tax_category_id do
= f.label :tax_category_id, t(:tax_category)
%br
= f.collection_select(:tax_category_id, Spree::TaxCategory.all, :id, :name, {:include_blank => Spree::Config.products_require_tax_category ? false : t(:none)}, {:class => "select2 fullwidth"})
= f.collection_select(:tax_category_id, Spree::TaxCategory.all, :id, :name, {:include_blank => Spree::Config.products_require_tax_category ? false : t(:none), selected: Spree::TaxCategory.find_by(is_default: true)&.id}, {:class => "select2 fullwidth"})
Copy link
Collaborator

@chahmedejaz chahmedejaz Dec 1, 2024

Choose a reason for hiding this comment

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

This looks right, however, I was wondering if this way users might end up unintentionally selecting the tax category when it's not required. To prevent this, I think nothing should be selected if the products don't require a tax category (Spree::Config.products_require_tax_category = false). Otherwise, the default category should be selected.

Given that we are incorporating the above logic, is it possible we could move this logic to the view helper to clean up the logic from the view? Thanks.

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 have the logic requested above (in case Spree::Config.products_require_tax_category = false it shows nothing, otherwise it selects the default category). Don't know where to put the code in the helper tho. Can i put it in tax_helper.rb file in the helpers folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to a new file (app/helpers/spree/admin/tax_categories_helper.rb) the new logic requested, hope it's within the structure of the project.

@chahmedejaz
Copy link
Collaborator

P.S. I think some specs are failing due to this change, can you please look into those as well? Thanks.

@MrBowmanXD
Copy link
Contributor Author

I think the failing spec might be related to the tax category existence so maybe the updated logic might solve it, need to check this idea.

@MrBowmanXD
Copy link
Contributor Author

I think the failing spec might be related to a new :selected attribute added to the view (selected: Spree::TaxCategory.find_by(is_default: true)&.id)

Copy link
Collaborator

@chahmedejaz chahmedejaz left a comment

Choose a reason for hiding this comment

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

We are getting there :)
Apparently, we have lint issues with the newly added helper, are you happy to look into these? Thanks.

@chahmedejaz
Copy link
Collaborator

I think the failing spec might be related to a new :selected attribute added to the view (selected: Spree::TaxCategory.find_by(is_default: true)&.id)

Yes, however, we need to validate the specs whether the code is breaking some usecase in the specs or the specs need to be updated accordingly :)

@MrBowmanXD
Copy link
Contributor Author

Let me check these lint issues.

@MrBowmanXD
Copy link
Contributor Author

We are getting there :) Apparently, we have lint issues with the newly added helper, are you happy to look into these? Thanks.

Was able to resolve the lint issues in the code.

@MrBowmanXD
Copy link
Contributor Author

MrBowmanXD commented Dec 3, 2024

tax_categories_helper
I could remove :selected from the else clause to pass the failing spec. (Because before my solution there was no :Selected attribute)

Copy link
Collaborator

@chahmedejaz chahmedejaz left a comment

Choose a reason for hiding this comment

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

Thanks for working through this. 👍🏻
Now we will wait for the 2nd review. 🙂

@rioug rioug added the user facing changes Thes pull requests affect the user experience label Dec 4, 2024
@rioug rioug self-requested a review December 4, 2024 00:29
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a good practise to add unit test for your code, even if the logic seems trivial. Could you add some test for the new helper ? Have a look here for some inspiration https://github.com/openfoodfoundation/openfoodnetwork/blob/master/spec/helpers/admin/products_helper_spec.rb

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 add some tests, sorry if i did not start with a test, i am still learning Rails.

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 would like to ask if you guys know any good resource for rspec in rails? i will search for it as well, thanks in advance

Copy link
Collaborator

Choose a reason for hiding this comment

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

No worries 🙂
I find this playlist helpful, maybe it would help:
https://www.youtube.com/watch?v=2jX-FLcznDE&list=PLS6F722u-R6Ik3fbeLXbSclWkT6Qsp9ng

Please let us know you face any issues, no worries ⭐

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello, thank you. I will check it out.

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Thanks for adding the spec ! Just one last change and we should be good to go.

context 'when products do not require a tax category' do
it 'returns include_blank as the translated "none" string' do
options = helper.tax_category_dropdown_options(false)
expect(options[:include_blank]).to eq(I18n.t(:none))
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's better to actually check the translation is working,ie:

Suggested change
expect(options[:include_blank]).to eq(I18n.t(:none))
expect(options[:include_blank]).to eq("None")

@sigmundpetersen sigmundpetersen requested a review from rioug December 9, 2024 09:08
@rioug rioug requested a review from chahmedejaz December 9, 2024 22:47
Copy link
Collaborator

@chahmedejaz chahmedejaz left a comment

Choose a reason for hiding this comment

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

Thanks for your hardwork, @MrBowmanXD. Looks good to me. 👍

Now we will have a round of manual testing by our testing team before the merge. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user facing changes Thes pull requests affect the user experience
Projects
Status: Test Ready 🧪
Development

Successfully merging this pull request may close these issues.

Default tax category is not set
3 participants