Integrate functionality from distributor into enterprise

This commit is contained in:
Rohan Mitchell
2012-10-28 17:46:45 +11:00
parent c700d9e71b
commit abe3feb996
4 changed files with 35 additions and 4 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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|

View File

@@ -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)