diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index 2ac35fa3ad..88c0f89aec 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -13,6 +13,6 @@ class BaseController < ApplicationController before_filter :check_order_cycle_expiry def load_active_distributors - @active_distributors ||= Enterprise.distributors_with_active_order_cycles + @active_distributors ||= Enterprise.distributors_with_active_order_cycles.ready_for_checkout end end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 3f98e01eb4..fc95d32613 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -65,7 +65,13 @@ class Enterprise < ActiveRecord::Base scope :visible, where(:visible => true) scope :confirmed, where('confirmed_at IS NOT NULL') scope :unconfirmed, where('confirmed_at IS NULL') - scope :activated, where('confirmed_at IS NOT NULL AND sells <> \'unspecified\'') + scope :activated, where("confirmed_at IS NOT NULL AND sells != 'unspecified'") + scope :ready_for_checkout, lambda { + joins(:shipping_methods). + joins(:payment_methods). + merge(Spree::PaymentMethod.available). + select('DISTINCT enterprises.*') + } scope :is_primary_producer, where(:is_primary_producer => true) scope :is_distributor, where('sells != ?', 'none') scope :supplying_variant_in, lambda { |variants| joins(:supplied_products => :variants_including_master).where('spree_variants.id IN (?)', variants).select('DISTINCT enterprises.*') } diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 01f6e2f0e8..fd513cc0d3 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -24,6 +24,13 @@ Spree::PaymentMethod.class_eval do scope :by_name, order('spree_payment_methods.name ASC') + # Rewrite Spree's ruby-land class method as a scope + scope :available, lambda { |display_on='both'| + where(active: true). + where('spree_payment_methods.display_on=? OR spree_payment_methods.display_on=? OR spree_payment_methods.display_on IS NULL', display_on, ''). + where('spree_payment_methods.environment=? OR spree_payment_methods.environment=? OR spree_payment_methods.environment IS NULL', Rails.env, '') + } + def has_distributor?(distributor) self.distributors.include?(distributor) end diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 31c7af9f7c..f6cc94b8ce 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -167,6 +167,32 @@ describe Enterprise do end end + describe "ready_for_checkout" do + let!(:e) { create(:enterprise) } + + it "does not show enterprises with no payment methods" do + create(:shipping_method, distributors: [e]) + Enterprise.ready_for_checkout.should_not include e + end + + it "does not show enterprises with no shipping methods" do + create(:payment_method, distributors: [e]) + Enterprise.ready_for_checkout.should_not include e + end + + it "does not show enterprises with unavailable payment methods" do + create(:shipping_method, distributors: [e]) + create(:payment_method, distributors: [e], active: false) + Enterprise.ready_for_checkout.should_not include e + end + + it "shows enterprises with available payment and shipping methods" do + create(:shipping_method, distributors: [e]) + create(:payment_method, distributors: [e]) + Enterprise.ready_for_checkout.should include e + end + end + describe "distributors_with_active_order_cycles" do it "finds active distributors by order cycles" do s = create(:supplier_enterprise)