add suppliers scope, limit by 5, test

This commit is contained in:
vagrant
2013-05-22 13:07:30 +10:00
parent aa85d20173
commit 4e7f966429
6 changed files with 32 additions and 9 deletions

1
.gitignore vendored
View File

@@ -30,3 +30,4 @@ config/abr.yml
config/heroku_env.rb
NERD_tree*
coverage
config/database.yml

View File

@@ -10,8 +10,10 @@ class ApplicationController < ActionController::Base
end
def load_data_for_sidebar
@suppliers = Enterprise.is_primary_producer
@distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name
@suppliers = Enterprise.is_primary_producer.with_supplied_active_products_on_hand.limit(5)
@supplier_count = Enterprise.is_primary_producer.with_supplied_active_products_on_hand.count
@distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name.limit(5)
@distributor_count = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name.count
end
# All render calls within the block will be performed with the specified format

View File

@@ -17,6 +17,7 @@ class Enterprise < ActiveRecord::Base
scope :by_name, order('name')
scope :is_primary_producer, where(:is_primary_producer => true)
scope :is_distributor, where(:is_distributor => true)
scope :with_supplied_active_products_on_hand, lambda { joins(:supplied_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.*)') }
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.*)') }

View File

@@ -1,11 +1,4 @@
%nav#filters
%h6.filter_name Shop by Supplier
%ul.filter_choices
- @suppliers.each do |supplier|
- if supplier.has_supplied_products_on_hand?
%li.nowrap= link_to supplier.name, [main_app, supplier]
= button_to 'Browse All Suppliers', main_app.suppliers_enterprises_path, :method => :get
%h6.filter_name Shop by Distributor
%ul.filter_choices
- order = current_order(false)
@@ -18,5 +11,18 @@
= link_to distributor.name, [main_app, distributor]
- else
%abbr(title="One or more of the products in your cart is not available from this distributor")= distributor.name
- if @distributor_count > @distributors.count
- distributors_more = @distributors.count - @distributor_count
= "#{distributors_more} more..."
- if current_distributor && validator.can_change_distributor?
= button_to 'Browse All Distributors', deselect_distributor_orders_path, :method => :get
%h6.filter_name Shop by Supplier
%ul.filter_choices
- @suppliers.each do |supplier|
-# - if supplier.has_supplied_products_on_hand? -#d.cook: this is filtered in the controller now
%li.nowrap= link_to supplier.name, [main_app, supplier]
- if @supplier_count > @suppliers.count
- suppliers_more = @suppliers.count - @supplier_count
= "#{suppliers_more} more..."
= button_to 'Browse All Suppliers', main_app.suppliers_enterprises_path, :method => :get

View File

@@ -422,6 +422,7 @@ ActiveRecord::Schema.define(:version => 20130207043555) do
t.string "email"
t.text "special_instructions"
t.integer "distributor_id"
t.integer "order_cycle_id"
t.string "currency"
t.string "last_ip_address"
end

View File

@@ -29,6 +29,18 @@ describe Enterprise do
Enterprise.with_distributed_active_products_on_hand.sort.should == [d1, d2]
end
it "returns suppliers with products in stock" do
d1 = create(:supplier_enterprise)
d2 = create(:supplier_enterprise)
d3 = create(:supplier_enterprise)
d4 = create(:supplier_enterprise)
create(:product, :supplier => d1, :on_hand => 5)
create(:product, :supplier => d2, :on_hand => 5, :available_on => 1.week.from_now)
create(:product, :supplier => d3, :on_hand => 0)
# supplier with no products, supplier with product out of stock, suplier with product thats unavailable, supplier with active product on hand
Enterprise.with_supplied_active_products_on_hand.sort.should == [d1]
end
end
context "has_supplied_products_on_hand?" do