From 4e7f96642911e690d7731c34dc974e2db081dbec Mon Sep 17 00:00:00 2001 From: vagrant Date: Wed, 22 May 2013 13:07:30 +1000 Subject: [PATCH] add suppliers scope, limit by 5, test --- .gitignore | 1 + app/controllers/application_controller.rb | 6 ++++-- app/models/enterprise.rb | 1 + .../spree/products/_source_sidebar.html.haml | 20 ++++++++++++------- db/schema.rb | 1 + spec/models/enterprises_spec.rb | 12 +++++++++++ 6 files changed, 32 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 6cb7536f07..d3e283ca3a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ config/abr.yml config/heroku_env.rb NERD_tree* coverage +config/database.yml diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b3ef2dff28..30c59f30cb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 17c9380097..6b7d133970 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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.*)') } diff --git a/app/views/spree/products/_source_sidebar.html.haml b/app/views/spree/products/_source_sidebar.html.haml index 03fc78ac84..11f62f90cb 100644 --- a/app/views/spree/products/_source_sidebar.html.haml +++ b/app/views/spree/products/_source_sidebar.html.haml @@ -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 diff --git a/db/schema.rb b/db/schema.rb index a35dd39545..b99bd0a260 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 051533144e..30a518b8b7 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -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