diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb new file mode 100644 index 0000000000..d0a40c3826 --- /dev/null +++ b/app/models/enterprise.rb @@ -0,0 +1,32 @@ +class Enterprise < ActiveRecord::Base + has_many :supplied_products, :class_name => 'Spree::Product', :foreign_key => 'supplier_id' + belongs_to :address, :class_name => 'Spree::Address' + + accepts_nested_attributes_for :address + + validates_presence_of :name, :address + validates_associated :address + + after_initialize :initialize_country + before_validation :set_unused_address_fields + + def has_supplied_products_on_hand? + self.supplied_products.where('count_on_hand > 0').present? + end + + def to_param + "#{id}-#{name.parameterize}" + end + + + private + + def initialize_country + self.address ||= Spree::Address.new + self.address.country = Spree::Country.find_by_id(Spree::Config[:default_country_id]) if self.address.new_record? + end + + def set_unused_address_fields + address.firstname = address.lastname = address.phone = 'unused' if address.present? + end +end diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 91ed6e57ee..58a7c93346 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -1,5 +1,5 @@ Spree::Product.class_eval do - belongs_to :supplier + belongs_to :supplier, :class_name => 'Enterprise' has_many :product_distributions, :dependent => :destroy has_many :distributors, :through => :product_distributions diff --git a/spec/factories.rb b/spec/factories.rb index bd2a08d09f..75cdcec18a 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -2,6 +2,19 @@ require 'faker' require 'spree/core/testing_support/factories' FactoryGirl.define do + factory :enterprise, :class => Enterprise do + sequence(:name) { |n| "Enterprise #{n}" } + description 'enterprise' + long_description '

Hello, world!

This is a paragraph.

' + email 'enterprise@example.com' + address { Spree::Address.first || FactoryGirl.create(:address) } + end + + factory :supplier_enterprise, :parent => :enterprise do + is_primary_producer true + is_distributor false + end + factory :supplier, :class => Supplier do sequence(:name) { |n| "Supplier #{n}" } description 'supplier' diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb new file mode 100644 index 0000000000..6e7930e355 --- /dev/null +++ b/spec/models/enterprises_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe Enterprise do + + describe "associations" do + it { should have_many(:supplied_products) } + it { should belong_to(:address) } + end + + describe "validations" do + it { should validate_presence_of(:name) } + end + + it "should default country to system country" do + subject.address.country.should == Spree::Country.find_by_id(Spree::Config[:default_country_id]) + end + + context "has_supplied_products_on_hand?" do + before :each do + @supplier = create(:supplier_enterprise) + end + + it "returns false when no products" do + @supplier.should_not have_supplied_products_on_hand + end + + it "returns false when the product is out of stock" do + create(:product, :supplier => @supplier, :on_hand => 0) + @supplier.should_not have_supplied_products_on_hand + end + + it "returns true when the product is in stock" do + create(:product, :supplier => @supplier, :on_hand => 1) + @supplier.should have_supplied_products_on_hand + end + end +end