diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 656082bb79..db0565a73d 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -27,8 +27,6 @@ class Enterprise < ActiveRecord::Base has_many :supplied_products, :class_name => 'Spree::Product', :foreign_key => 'supplier_id', :dependent => :destroy has_many :distributed_orders, :class_name => 'Spree::Order', :foreign_key => 'distributor_id' belongs_to :address, :class_name => 'Spree::Address' - has_many :product_distributions, :foreign_key => 'distributor_id', :dependent => :destroy - has_many :distributed_products, :through => :product_distributions, :source => :product has_many :enterprise_fees has_many :enterprise_roles, :dependent => :destroy has_many :users, through: :enterprise_roles @@ -109,11 +107,11 @@ class Enterprise < ActiveRecord::Base scope :is_primary_producer, where(:is_primary_producer => true) scope :is_distributor, where('sells != ?', 'none') scope :is_hub, where(sells: 'any') - scope :supplying_variant_in, lambda { |variants| joins(:supplied_products => :variants_including_master).where('spree_variants.id IN (?)', variants).select('DISTINCT enterprises.*') } - - scope :with_distributed_products_outer, - joins('LEFT OUTER JOIN product_distributions ON product_distributions.distributor_id = enterprises.id'). - joins('LEFT OUTER JOIN spree_products ON spree_products.id = product_distributions.product_id') + scope :supplying_variant_in, lambda { |variants| + joins(:supplied_products => :variants_including_master). + where('spree_variants.id IN (?)', variants). + select('DISTINCT enterprises.*') + } scope :with_order_cycles_as_supplier_outer, joins("LEFT OUTER JOIN exchanges ON (exchanges.sender_id = enterprises.id AND exchanges.incoming = 't')"). @@ -139,16 +137,15 @@ class Enterprise < ActiveRecord::Base } scope :distributing_products, lambda { |products| - # TODO: remove this when we pull out product distributions - pds = joins("INNER JOIN product_distributions ON product_distributions.distributor_id = enterprises.id"). - where("product_distributions.product_id IN (?)", products).select('DISTINCT enterprises.id') - - exs = joins("INNER JOIN exchanges ON (exchanges.receiver_id = enterprises.id AND exchanges.incoming = 'f')"). + exchanges = joins(" + INNER JOIN exchanges + ON (exchanges.receiver_id = enterprises.id AND exchanges.incoming = 'f') + "). joins('INNER JOIN exchange_variants ON (exchange_variants.exchange_id = exchanges.id)'). joins('INNER JOIN spree_variants ON (spree_variants.id = exchange_variants.variant_id)'). where('spree_variants.product_id IN (?)', products).select('DISTINCT enterprises.id') - where(id: pds | exs) + where(id: exchanges) } scope :managed_by, lambda { |user| @@ -250,14 +247,6 @@ class Enterprise < ActiveRecord::Base Spree::Variant.joins(:product).merge(Spree::Product.in_distributor(self)).select('spree_variants.*') end - def product_distribution_variants - Spree::Variant.joins(:product).merge(Spree::Product.in_product_distribution_by(self)).select('spree_variants.*') - end - - def available_variants - Spree::Variant.joins(:product => :product_distributions).where('product_distributions.distributor_id=?', self.id) - end - def is_distributor self.sells != "none" end diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index eedcb5ad16..fb06d69c46 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -81,19 +81,6 @@ describe EnterprisesController, type: :controller do controller.current_order.line_items.size.should == 0 end - it "should not empty an order if returning to the same distributor" do - product = create(:product) - create(:product_distribution, product: product, distributor: current_distributor) - line_item = create(:line_item, variant: product.master) - controller.current_order.line_items << line_item - - spree_get :shop, {id: current_distributor} - - controller.current_order.distributor.should == current_distributor - controller.current_order.order_cycle.should be_nil - controller.current_order.line_items.size.should == 1 - end - describe "when an out of stock item is in the cart" do let(:variant) { create(:variant, on_demand: false, on_hand: 10) } let(:line_item) { create(:line_item, variant: variant) } diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 73cd3e1c5b..88dd00fac8 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -21,7 +21,6 @@ describe Enterprise do it { should have_many(:supplied_products) } it { should have_many(:distributed_orders) } it { should belong_to(:address) } - it { should have_many(:product_distributions) } it "destroys enterprise roles upon its own demise" do e = create(:enterprise) @@ -356,37 +355,18 @@ describe Enterprise do end describe "distributing_products" do - it "returns enterprises distributing via a product distribution" do - d = create(:distributor_enterprise) - p = create(:product, distributors: [d]) - Enterprise.distributing_products(p).should == [d] - end + let(:distributor) { create(:distributor_enterprise) } + let(:product) { create(:product) } it "returns enterprises distributing via an order cycle" do - d = create(:distributor_enterprise) - p = create(:product) - oc = create(:simple_order_cycle, distributors: [d], variants: [p.master]) - Enterprise.distributing_products(p).should == [d] - end - - it "returns enterprises distributing via a product distribution" do - d = create(:distributor_enterprise) - p = create(:product, distributors: [d]) - Enterprise.distributing_products([p]).should == [d] - end - - it "returns enterprises distributing via an order cycle" do - d = create(:distributor_enterprise) - p = create(:product) - oc = create(:simple_order_cycle, distributors: [d], variants: [p.master]) - Enterprise.distributing_products([p]).should == [d] + order_cycle = create(:simple_order_cycle, distributors: [distributor], variants: [product.master]) + Enterprise.distributing_products(product).should == [distributor] end it "does not return duplicate enterprises" do - d = create(:distributor_enterprise) - p1 = create(:product, distributors: [d]) - p2 = create(:product, distributors: [d]) - Enterprise.distributing_products([p1, p2]).should == [d] + another_product = create(:product) + order_cycle = create(:simple_order_cycle, distributors: [distributor], variants: [product.master, another_product.master]) + Enterprise.distributing_products([product, another_product]).should == [distributor] end end @@ -490,39 +470,13 @@ describe Enterprise do end describe "finding variants distributed by the enterprise" do - it "finds master and other variants" do - d = create(:distributor_enterprise) - p = create(:product, distributors: [d]) - v = p.variants.first - d.distributed_variants.should match_array [p.master, v] - end + it "finds variants, including master, distributed by order cycle" do + distributor = create(:distributor_enterprise) + product = create(:product) + variant = product.variants.first + create(:simple_order_cycle, distributors: [distributor], variants: [variant]) - pending "finds variants distributed by order cycle" do - # there isn't actually a method for this on Enterprise? - d = create(:distributor_enterprise) - p = create(:product) - v = p.variants.first - oc = create(:simple_order_cycle, distributors: [d], variants: [v]) - - # This method doesn't do what this test says it does... - d.distributed_variants.should match_array [v] - end - end - - describe "finding variants distributed by the enterprise in a product distribution only" do - it "finds master and other variants" do - d = create(:distributor_enterprise) - p = create(:product, distributors: [d]) - v = p.variants.first - d.product_distribution_variants.should match_array [p.master, v] - end - - it "does not find variants distributed by order cycle" do - d = create(:distributor_enterprise) - p = create(:product) - v = p.variants.first - oc = create(:simple_order_cycle, distributors: [d], variants: [v]) - d.product_distribution_variants.should == [] + distributor.distributed_variants.should match_array [product.master, variant] end end