diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index d0a40c3826..0d66f83f8e 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -1,15 +1,25 @@ class Enterprise < ActiveRecord::Base has_many :supplied_products, :class_name => 'Spree::Product', :foreign_key => 'supplier_id' + 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 accepts_nested_attributes_for :address - validates_presence_of :name, :address + validates_presence_of :name + validates_presence_of :address validates_associated :address after_initialize :initialize_country before_validation :set_unused_address_fields + scope :by_name, order('name') + scope :is_supplier, where(:is_primary_producer => true) + scope :is_distributor, where(:is_distributor => true) + scope :with_distributed_active_products_on_hand, lambda { joins(:distributed_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(enterprises.*)') } + + def has_supplied_products_on_hand? self.supplied_products.where('count_on_hand > 0').present? end diff --git a/app/models/product_distribution.rb b/app/models/product_distribution.rb index 19305c2685..026322afd6 100644 --- a/app/models/product_distribution.rb +++ b/app/models/product_distribution.rb @@ -1,6 +1,6 @@ class ProductDistribution < ActiveRecord::Base belongs_to :product, :class_name => 'Spree::Product' - belongs_to :distributor + belongs_to :distributor, :class_name => 'Enterprise' belongs_to :shipping_method, :class_name => 'Spree::ShippingMethod' validates_presence_of :product_id, :on => :update diff --git a/spec/factories.rb b/spec/factories.rb index 75cdcec18a..eded25b43c 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -15,6 +15,11 @@ FactoryGirl.define do is_distributor false end + factory :distributor_enterprise, :parent => :enterprise do + is_primary_producer false + is_distributor true + end + factory :supplier, :class => Supplier do sequence(:name) { |n| "Supplier #{n}" } description 'supplier' @@ -59,7 +64,7 @@ FactoryGirl.modify do # When this fix has been merged into a version of Spree that we're using, this line can be removed. sequence(:name) { |n| "Product ##{n} - #{Kernel.rand(9999)}" } - supplier { Supplier.first || FactoryGirl.create(:supplier) } + supplier { Enterprise.is_supplier.first || FactoryGirl.create(:supplier_enterprise) } on_hand 3 # before(:create) do |product, evaluator| diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 6e7930e355..051533144e 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -4,17 +4,33 @@ describe Enterprise do describe "associations" do it { should have_many(:supplied_products) } + it { should have_many(:distributed_orders) } it { should belong_to(:address) } + it { should have_many(:product_distributions) } end describe "validations" do it { should validate_presence_of(:name) } end - it "should default country to system country" do + it "should default address country to system country" do subject.address.country.should == Spree::Country.find_by_id(Spree::Config[:default_country_id]) end + describe "scopes" do + it "returns distributors with products in stock" do + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + d3 = create(:distributor_enterprise) + d4 = create(:distributor_enterprise) + create(:product, :distributors => [d1, d2], :on_hand => 5) + create(:product, :distributors => [d1], :on_hand => 5) + create(:product, :distributors => [d3], :on_hand => 0) + + Enterprise.with_distributed_active_products_on_hand.sort.should == [d1, d2] + end + end + context "has_supplied_products_on_hand?" do before :each do @supplier = create(:supplier_enterprise)