-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify
Variant#default_price
logic
Before this work, `#default_price` was a `has_one` association between `Variant` and `Product`. As we already have a `has_many` association between both models, this redundancy was a source of inconsistencies. E.g.: ```ruby include Spree Variant.new(price: Price.new) # price delegates to default_price Variant.prices # => [] ``` From now on, `#default_price` is a regular method that searches within `#prices` taking into account the criteria for a price to be considered the default one. We have renamed previous method `#find_default_price_or_build` to `default_price_or_build`. The latter feels less standard according to Rails conventions, but the intention here is, precisely, to communicate that this is not a Rails association. No longer being a Rails association makes it impossible to use the default ransack conventions to build the sort-by-price link on the products listing page. For this reason, we added `sort_by_master_default_price_amount_{asc, desc}` scopes to the `Price` model, which is automatically picked up by ransack. As it's now explicit, these queries ignore the `ORDER BY` clauses implicit in the `currently_valid_prices` method, but this was also happening in the query built by ransack from the `has_one` association: ```SQL SELECT "spree_products".* FROM "spree_products" LEFT OUTER JOIN "spree_variants" ON "spree_variants"."is_master" = ? AND "spree _variants"."product_id" = "spree_products"."id" LEFT OUTER JOIN "spree_prices" ON "spree_prices"."currency" = ? AND "spree_pric es"."country_iso" IS NULL AND "spree_prices"."variant_id" = "spree_variants"."id" WHERE "spree_products"."deleted_at" IS NULL O RDER BY "spree_prices"."amount" ASC, "spree_products"."id" ASC LIMIT ? OFFSET ? [["is_master", 1], ["currency", "USD"], ["LIMI T", 10], ["OFFSET", 0]] ``` However, the new query doesn't include discarded prices on the result, but I think it's something desirable: ```SQL SELECT "spree_products".* FROM "spree_products" LEFT OUTER JOIN "spree_variants" "master" ON "master"."is_master" = ? AND "mast er"."product_id" = "spree_products"."id" LEFT OUTER JOIN "spree_prices" "prices" ON "prices"."deleted_at" IS NULL AND "prices". "variant_id" = "master"."id" LEFT OUTER JOIN "spree_variants" "masters_spree_products" ON "masters_spree_products"."is_master" = ? AND "masters_spree_products"."product_id" = "spree_products"."id" WHERE "spree_products"."deleted_at" IS NULL AND "prices". "currency" = ? AND "prices"."country_iso" IS NULL AND "prices"."deleted_at" IS NULL ORDER BY prices.amount DESC, "spree_product s"."id" ASC LIMIT ? OFFSET ? [["is_master", 1], ["is_master", 1], ["currency", "USD"], ["LIMIT", 10], ["OFFSET", 0]] ```
- Loading branch information
1 parent
8053d89
commit 6420b10
Showing
14 changed files
with
210 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.