Last active
August 29, 2015 14:11
-
-
Save chinshr/b475b0b8398ca2d9f5a2 to your computer and use it in GitHub Desktop.
Orderer collection variants
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
Spree::Variant.class_eval do | |
scope :with_distinct_images, -> { | |
variant_assets_sql = "SELECT MD5(GROUP_CONCAT(`spree_assets`.`image_type`, `spree_assets`.`attachment_file_name`)) FROM `spree_assets` | |
WHERE `spree_assets`.`type` IN ('Spree::Image') AND `spree_assets`.`viewable_id` = `spree_variants`.`id` AND `spree_assets`.`viewable_type` = 'Spree::Variant' | |
ORDER BY `spree_assets`.`image_type` ASC, `spree_assets`.`attachment_file_name` ASC" | |
variant_product_assets_sql = "SELECT MD5(GROUP_CONCAT(`spree_assets`.`image_type`, `spree_assets`.`attachment_file_name`)) FROM `spree_assets` | |
INNER JOIN `spree_products` ON `spree_products`.`deleted_at` IS NULL | |
INNER JOIN `spree_variants` `variants_including_masters_spree_products_join` ON `variants_including_masters_spree_products_join`.`product_id` = `spree_products`.`id` AND `variants_including_masters_spree_products_join`.`is_master` = 1 AND `variants_including_masters_spree_products_join`.`deleted_at` IS NULL | |
WHERE `spree_products`.`id` = `spree_variants`.`product_id` AND `spree_assets`.`type` IN ('Spree::Image') AND `spree_assets`.`viewable_id` = `variants_including_masters_spree_products_join`.`id` AND `spree_assets`.`viewable_type` = 'Spree::Variant' | |
ORDER BY `spree_assets`.`image_type` ASC, `spree_assets`.`attachment_file_name` ASC" | |
select("`spree_variants`.*") | |
.select("IFNULL((#{variant_assets_sql}), (#{variant_product_assets_sql})) AS md5") | |
.group("md5") | |
} | |
end | |
# c=Spree::Collection.find_by permalink: "rise-and-shine" | |
# c.variants.with_distinct_images |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment