Add spec for viewing product counts when both a distributor and an order cycle are selected, scope Spree::Product.with_order_cycles_outer joins to avoid clash when using both in_distributor and in_order_cycle in the same query

This commit is contained in:
Rohan Mitchell
2013-03-15 12:17:29 +11:00
parent f349628202
commit a9a58db137
2 changed files with 44 additions and 7 deletions

View File

@@ -14,10 +14,10 @@ Spree::Product.class_eval do
# -- Joins
scope :with_product_distributions_outer, joins('LEFT OUTER JOIN product_distributions ON product_distributions.product_id = spree_products.id')
scope :with_order_cycles_outer, joins('LEFT OUTER JOIN spree_variants AS pd_woco_spree_variants ON (pd_woco_spree_variants.product_id = spree_products.id)').
joins('LEFT OUTER JOIN exchange_variants ON (exchange_variants.variant_id = pd_woco_spree_variants.id)').
joins('LEFT OUTER JOIN exchanges ON (exchanges.id = exchange_variants.exchange_id)').
joins('LEFT OUTER JOIN order_cycles ON (order_cycles.id = exchanges.order_cycle_id)')
scope :with_order_cycles_outer, joins('LEFT OUTER JOIN spree_variants AS o_spree_variants ON (o_spree_variants.product_id = spree_products.id)').
joins('LEFT OUTER JOIN exchange_variants AS o_exchange_variants ON (o_exchange_variants.variant_id = o_spree_variants.id)').
joins('LEFT OUTER JOIN exchanges AS o_exchanges ON (o_exchanges.id = o_exchange_variants.exchange_id)').
joins('LEFT OUTER JOIN order_cycles AS o_order_cycles ON (o_order_cycles.id = o_exchanges.order_cycle_id)')
scope :with_order_cycles_inner, joins('INNER JOIN spree_variants ON (spree_variants.product_id = spree_products.id)').
joins('INNER JOIN exchange_variants ON (exchange_variants.variant_id = spree_variants.id)').
@@ -32,7 +32,7 @@ Spree::Product.class_eval do
distributor = distributor.respond_to?(:id) ? distributor.id : distributor.to_i
with_product_distributions_outer.with_order_cycles_outer.
where('product_distributions.distributor_id = ? OR (exchanges.sender_id = order_cycles.coordinator_id AND exchanges.receiver_id = ?)', distributor, distributor).
where('product_distributions.distributor_id = ? OR (o_exchanges.sender_id = o_order_cycles.coordinator_id AND o_exchanges.receiver_id = ?)', distributor, distributor).
select('distinct spree_products.*')
}
@@ -41,7 +41,7 @@ Spree::Product.class_eval do
enterprise = enterprise.respond_to?(:id) ? enterprise.id : enterprise.to_i
with_product_distributions_outer.with_order_cycles_outer.
where('spree_products.supplier_id = ? OR product_distributions.distributor_id = ? OR (exchanges.sender_id = order_cycles.coordinator_id AND exchanges.receiver_id = ?)', enterprise, enterprise, enterprise).
where('spree_products.supplier_id = ? OR product_distributions.distributor_id = ? OR (o_exchanges.sender_id = o_order_cycles.coordinator_id AND o_exchanges.receiver_id = ?)', enterprise, enterprise, enterprise).
select('distinct spree_products.*')
}

View File

@@ -103,7 +103,44 @@ feature %q{
page.should have_selector 'nav#taxonomies li', :text => 'Taxon three (2)'
end
scenario "viewing product counts when both a distributor and an order cycle are selected"
scenario "viewing product counts when both a distributor and an order cycle are selected" do
# Given some taxons and some products under distributors
taxonomy = Spree::Taxonomy.find_by_name('Products') || create(:taxonomy, :name => 'Products')
taxonomy_root = taxonomy.root
taxon_one = create(:taxon, :name => 'Taxon one', :parent_id => taxonomy_root.id)
taxon_two = create(:taxon, :name => 'Taxon two', :parent_id => taxonomy_root.id)
taxon_three = create(:taxon, :name => 'Taxon three', :parent_id => taxonomy_root.id)
supplier = create(:supplier_enterprise, :name => 'My Supplier')
my_distributor = create(:distributor_enterprise, :name => 'My Distributor')
other_distributor = create(:distributor_enterprise, :name => 'Other Distributor')
p1 = create(:product, :taxons => [taxon_one])
p2 = create(:product, :taxons => [taxon_two])
p3 = create(:product, :taxons => [taxon_three])
p4 = create(:product, :taxons => [taxon_one])
p5 = create(:product, :taxons => [taxon_two])
oc1 = create(:simple_order_cycle, suppliers: [supplier])
oc2 = create(:simple_order_cycle, suppliers: [supplier])
create(:exchange, order_cycle: oc1, sender: oc1.coordinator, receiver: my_distributor, variants: [p1.master])
create(:exchange, order_cycle: oc2, sender: oc2.coordinator, receiver: my_distributor, variants: [p2.master])
create(:exchange, order_cycle: oc1, sender: oc1.coordinator, receiver: other_distributor, variants: [p3.master])
create(:exchange, order_cycle: oc2, sender: oc2.coordinator, receiver: other_distributor, variants: [p4.master, p5.master])
# When I visit the home page and select my distributor and order cycle
visit spree.select_distributor_order_path(my_distributor)
within('nav#filters') { click_link my_distributor.name }
page.should have_content 'You are shopping at My Distributor'
visit root_path
choose oc2.name
click_button 'Choose Order Cycle'
page.should have_content 'Your order cycle has been selected.'
# Then I should see distributor- and order-cycle-scoped product counts next to the taxons
page.should have_selector 'nav#taxonomies li', :text => 'Taxon one (0)'
page.should have_selector 'nav#taxonomies li', :text => 'Taxon two (1)'
page.should have_selector 'nav#taxonomies li', :text => 'Taxon three (0)'
end
end