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

Move query from variant_overrides_controller to its model scope #2818

Merged

Conversation

nikobozi
Copy link
Contributor

@nikobozi nikobozi commented Oct 4, 2018

What? Why?

This PR moves query code from VariantOverrideController into VariantOverride model as a new scope query, in order to extract extra logic from controller and also to reuse an existing scope for_hubs

What should we test?

The existing rspec tests should pass along with the new one

Release notes

Move query code from VariantOverrideController into its Model scope and add a spec

Changelog Category: Changed

@@ -3,8 +3,6 @@ class VariantOverride < ActiveRecord::Base

acts_as_taggable

attr_accessor :import_date
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was overriding the ActiveRecord getter/setter, which is why querying didn't work for me. When defining new accessor methods, shouldn't they have a different name than the database field?

Copy link
Contributor

Choose a reason for hiding this comment

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

Great question, I got the opportunity to improve my activerecord basics...
Because import_date is a database column in the variant_override table, this line is useless and I can be deleted because AR already does this for each db table field.
@Matt-Yorkley will confirm if there's any magical stuff going on here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did we mean attr_accessible instead, perhaps 🤔 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I dont think so, attr_accessible is for mass-assignment. attr_accessor is what you get out of the box in active records. AR adds the getter and setter (like attr_accessor) for each db column.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks fine to delete 👍

Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

Great change @nikobozi
Thanks!

scope :distinct_import_dates, lambda {
select('DISTINCT variant_overrides.import_date').
where('variant_overrides.import_date IS NOT NULL').
order('import_date DESC')
Copy link
Contributor

Choose a reason for hiding this comment

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

just as a minor detail, could we rely on ActiveRecord's hash style? So it'd be like order(import_date: :desc). It decouples the scope from SQL a bit.

See the examples in https://apidock.com/rails/ActiveRecord/QueryMethods/order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently this also works only after Rails 4 :/ as well as .where.not() which could have been used in the line above

Copy link
Contributor

Choose a reason for hiding this comment

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

Really? 😭

@@ -21,6 +19,12 @@ class VariantOverride < ActiveRecord::Base
where(hub_id: hubs)
}

scope :distinct_import_dates, lambda {
select('DISTINCT variant_overrides.import_date').
Copy link
Contributor

Choose a reason for hiding this comment

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

A pity that we're still on Rails 3.2 otherwise we could make use of AR's distinct

@sauloperez
Copy link
Contributor

neat @nikobozi 👏 ! Keep them coming!

@luisramos0
Copy link
Contributor

What screens could we possible break with this? Do we need some manual testing in staging?

@sauloperez
Copy link
Contributor

Definitely the inventory page. Perhaps you @RachL know about those import dates. I don't.

@myriamboure
Copy link
Contributor

"import dates" was something added by @Matt-Yorkley for product import but we are not using this info anymore. Not sure we will use it later either, the filter on import dates should have disappeared as it's not anymore in any place of the product import feature as we just redirect to product or inventory page after import. But as an admin I still see it on product page, so maybe it was not removed. Maybe it's safer to keep the product import info for now and we will see if we delete later on if we don't do anything with it?

@sauloperez
Copy link
Contributor

According to the code we show an import date filter in the inventory (variant overrides) page.

@luisramos0
Copy link
Contributor

I am not sure it's clear to everyone, I suggested deleting that import_date line of code because it's technically redundant (the line of code, not the db field). This would not impact on functionality at all.

@luisramos0 luisramos0 added the pr-staged-fr staging.coopcircuits.fr label Oct 9, 2018
@luisramos0
Copy link
Contributor

staged https://staging.openfoodfrance.org

The inventory (variant overrides) page needs to be tested.

@myriamboure
Copy link
Contributor

@sauloperez @luisramos0 is there any issue attached? hard to test without understanding the problem... @RachL Luis said the inventory page (variant override) needs to be tested. Make sure you unhide columns to test all overrides possible. @Matt-Yorkley do you think we need to keep that "import date" at all? I am not sure there will be any use for it, it was useful when you first designed it to display what had been imported but UX was floady aspecially for inventory... Anyway here Rachel you know what to test.

@sauloperez
Copy link
Contributor

This didn't arise from an existing issue but from an improvement that @nikobozi contributed with which is worth adding @myriamboure . There's no issue.

@RachL RachL self-assigned this Oct 23, 2018
@RachL
Copy link
Contributor

RachL commented Oct 23, 2018

I've been a bit long to come back on this one, sorry. Ready to go!

My testing notes:

https://docs.google.com/document/d/1kQXbx_BJb373YBk3U60TzKyJe4owjgP1DRWwJC0eoHw/edit#

@RachL RachL removed the pr-staged-fr staging.coopcircuits.fr label Oct 23, 2018
@sauloperez sauloperez merged commit 9ef4852 into openfoodfoundation:master Oct 24, 2018
@sauloperez
Copy link
Contributor

Thanks again @nikobozi ! keep 'em coming! 🤘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants