-
-
Notifications
You must be signed in to change notification settings - Fork 708
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
Show any tax category errors when saving variants #12486
Conversation
Now you can click on the text to tick/untick the box. I've been wanting to do that for ages.
a2cd1a5
to
8f6f85d
Compare
Need to fix this spec: I think this spec is expecting the tax category to be chosen automatically, but I think my change here revealed that it's not. Hmm, it is "chosen" here: https://github.com/openfoodfoundation/openfoodnetwork/blob/master/app/models/spree/variant.rb#L172-L174 def tax_category
super || TaxCategory.find_by(is_default: true)
end This is why the spec was passing before. But I guess it's not saved yet. |
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 think this spec is expecting the tax category to be chosen automatically, but I think my change here revealed that it's not.
No, the default tax category is not saved on the variant. But it's loaded when needed. Before, the validation was checking that it had a tax category. And it did "have" the default tax category without being associated to it. But it doesn't have a tax category id. I know it's confusing. I worked on it last and just kept the existing behaviour because I was worried about breaking anything.
app/models/spree/variant.rb
Outdated
@@ -64,7 +64,7 @@ class Variant < ApplicationRecord | |||
validates_lengths_from_database | |||
validate :check_currency | |||
validates :price, numericality: { greater_than_or_equal_to: 0 }, presence: true | |||
validates :tax_category, presence: true, | |||
validates :tax_category_id, presence: true, |
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.
That's a shame. This format has a few downsides, for example a new tax category can't be associated to a variant before it's saved. So instead of letting Rails save the record, we have to save it first. That shouldn't be in issue in our model but may be for test data.
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.
Yes, I thought it was a shame too, but I thought it's also more compatible with rails forms which is a good thing.
The test data issue is the only downside I can think of, can you think of any others?
@@ -48,6 +48,7 @@ | |||
selected_option: variant.primary_taxon_id, | |||
aria_label: t('.category_field_name'), | |||
placeholder_value: t('admin.products_v3.filters.search_for_categories'))) | |||
= error_message_on variant, :primary_taxon_id |
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.
We don't have a spec on this, do we?
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.
🙈 You're right, I need to add that.
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.
Wait, for primary_taxon_id
, I don't think there is a way to trigger an error.
I put this in for future-proofing, which I realise is generally a bad idea, but it seemed likely that if someone added a validation condition, they wouldn't think to add this.
It's important on this screen, because we don't have an error summary at the top of the screen.
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.
Given that it's untested, there's a very real possibility it doesn't work (eg, any validations would be on the association, not the id!).
So I will remove this.
@@ -56,6 +57,7 @@ | |||
include_blank: t('.none_tax_category'), | |||
aria_label: t('.tax_category_field_name'), | |||
placeholder_value: t('.search_for_tax_categories'))) | |||
= error_message_on variant, :tax_category_id |
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 just had a play with this and it seemed to work to go back to validating tax_category and just changing the error message here, without changing the input above:
= error_message_on variant, :tax_category_id | |
= error_message_on variant, :tax_category |
If I require a tax category but don't have a default then this will display an error.
The grey zone is to require a tax category, set a default and then select "None". Then it will look like it doesn't have a tax category but the default will be applied during checkout. That's another problem though.
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.
Nice work, I didn't think of that.
It won't work for the above select (ie <select name="...variant[tax_category_id]">
). That means we won't get the field_with_errors
wrapper class to add a red border on the field.
But that's an acceptable tradeoff 👍
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.
Note to self: also add spec for this error.
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.
Thanks for your help, I'll try proceeding with error_message_on variant, :tax_category
@@ -48,6 +48,7 @@ | |||
selected_option: variant.primary_taxon_id, | |||
aria_label: t('.category_field_name'), | |||
placeholder_value: t('admin.products_v3.filters.search_for_categories'))) | |||
= error_message_on variant, :primary_taxon_id |
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.
Wait, for primary_taxon_id
, I don't think there is a way to trigger an error.
I put this in for future-proofing, which I realise is generally a bad idea, but it seemed likely that if someone added a validation condition, they wouldn't think to add this.
It's important on this screen, because we don't have an error summary at the top of the screen.
@@ -56,6 +57,7 @@ | |||
include_blank: t('.none_tax_category'), | |||
aria_label: t('.tax_category_field_name'), | |||
placeholder_value: t('.search_for_tax_categories'))) | |||
= error_message_on variant, :tax_category_id |
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.
Nice work, I didn't think of that.
It won't work for the above select (ie <select name="...variant[tax_category_id]">
). That means we won't get the field_with_errors
wrapper class to add a red border on the field.
But that's an acceptable tradeoff 👍
@@ -56,6 +57,7 @@ | |||
include_blank: t('.none_tax_category'), | |||
aria_label: t('.tax_category_field_name'), | |||
placeholder_value: t('.search_for_tax_categories'))) | |||
= error_message_on variant, :tax_category_id |
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.
Note to self: also add spec for this error.
The validation message is on "tax_category", so labels and error messages can use that to show the error state. But the select field has to be "tax_category_id" to work.
I developed this while going down a slightly different path. I tested it visually, but the case I tested doesn't exist. I'm confident it will work if we ever have an error on another select though.
8f6f85d
to
e0c60aa
Compare
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.
Nice!
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.
Nice !
Hey @dacook , Thank you for working on this one. First, I've followed your suggested tests. After staging the PR, and with a Requires tax category option ticked: And no default tax category (all set to with
This works as expected: the changed variant is highlighted. In fact, there are several other variants with None as a tax category, and these are displayed as well: Clicking on Discard changes reverts the change for the edited variant, and the error messages disappear. It should be noticed though, that the other 7 variants are left unchanged (they are still having a Then, repeated this for In this case, the only offending variant is the edited variant, although other variants also have Summary I think this is because of variants on a previous state, which I'm not sure about. So, after this test, I've tried to look for the related scenario, in which we had the warning about edited variants, but no possibility to remove the banner. This corresponds to bug #12473, i.e., with admin_style_v3 enabled.
Going through pagination I spotted the Clicking
After setting all tax categories as non-default, indeed there is a bug fix:
Summary i) I think this PR highlights variants which have a None tax category. So I'd say it's good to merge. I'd vote for merging. Let me know what you think @dacook . Thanks! 🙏 |
Hi @filipefurtad0 , thanks for looking into this. I agree, this change doesn't solve the root cause of #12473. You also revealed that the old bulk screen only validates changed records, while the new screen validates all records on the screen. I've been wanting to optimise that since the beginning, will add a tech debt issue to track it. So I think still need to solve that issue #12473. If it is due to corrupt data, then we should still try to handle it gracefully. I will merge this one. |
What? Why?
If the tax category is required, but not chosen on a variant, the error is now clearer so you know what's wrong:
I now realise this was due to bad setup (see below), and would like to update the interface to avoid the root cause. But until then at least we have better error messaging.
It would have been nice to highlight the Tomselect and Select2 dropdowns too, but there's a couple of problems with that..
.. And we'll have to fix unit value another day.
What should we test?
Setup
I now realise that this is bad setup, but it's currently possible, and it's not intuitive:
/admin/tax_settings/edit
, select "products require tax category" and save/admin/tax_categories
and ensure there is no "default"Test with
admin_style_v3
enabled4. Visit
/admin/products
5. select "None" for tax category on a variant, and attempt save
6. Error message should explain why it did not save.
Try both: with
admin_style_v3
enabled, also without.7. Visit
/admin/products/x/variants/x/edit
8. select "None" for tax category, and attempt save.
Release notes
It's only really noticeable on admin_style_v3
Changelog Category (reviewers may add a label for the release notes):
The title of the pull request will be included in the release notes.
Documentation updates
The config requirements may already be documented in the guide, I don't know. But we really need to make the app more intuitive anyway.