Add scope: OrderCycle.with_distributor

This commit is contained in:
Rohan Mitchell
2013-09-17 07:59:11 +10:00
parent c2f1f0986b
commit a929312979
3 changed files with 33 additions and 8 deletions

View File

@@ -24,9 +24,8 @@ module OrderCyclesHelper
def order_cycle_options
@order_cycles.
select { |oc| oc.distributors.include? current_distributor }.
map { |oc| [order_cycle_close_to_s(oc.orders_close_at),
oc.id] }
with_distributor(current_distributor).
map { |oc| [order_cycle_close_to_s(oc.orders_close_at), oc.id] }
end
def order_cycle_close_to_s(orders_close_at)

View File

@@ -11,15 +11,21 @@ class OrderCycle < ActiveRecord::Base
validates_presence_of :name, :coordinator_id
scope :active, lambda { where('orders_open_at <= ? AND orders_close_at >= ?', Time.now, Time.now) }
scope :active_or_complete, lambda { where('orders_open_at <= ?', Time.now) }
scope :inactive, lambda { where('orders_open_at > ? OR orders_close_at < ?', Time.now, Time.now) }
scope :active, lambda { where('order_cycles.orders_open_at <= ? AND order_cycles.orders_close_at >= ?', Time.now, Time.now) }
scope :active_or_complete, lambda { where('order_cycles.orders_open_at <= ?', Time.now) }
scope :inactive, lambda { where('order_cycles.orders_open_at > ? OR order_cycles.orders_close_at < ?', Time.now, Time.now) }
scope :distributing_product, lambda { |product| joins(:exchanges => :variants).
where('exchanges.sender_id = order_cycles.coordinator_id').
scope :distributing_product, lambda { |product|
joins(:exchanges => :variants).
merge(Exchange.outgoing).
where('spree_variants.id IN (?)', product.variants_including_master.map(&:id)).
select('DISTINCT order_cycles.*') }
scope :with_distributor, lambda { |distributor|
joins(:exchanges).merge(Exchange.outgoing).where('exchanges.receiver_id = ?', distributor)
}
scope :managed_by, lambda { |user|
if user.has_spree_role?('admin')
scoped

View File

@@ -66,6 +66,26 @@ describe OrderCycle do
end
end
describe "finding order cycles with a particular distributor" do
let(:c) { create(:supplier_enterprise) }
let(:d) { create(:distributor_enterprise) }
it "returns order cycles with that distributor" do
oc = create(:simple_order_cycle, coordinator: c, distributors: [d])
OrderCycle.with_distributor(d).should == [oc]
end
it "does not return order cycles with that enterprise as supplier" do
oc = create(:simple_order_cycle, coordinator: c, suppliers: [d])
OrderCycle.with_distributor(d).should == []
end
it "does not return order cycles without that distributor" do
oc = create(:simple_order_cycle, coordinator: c)
OrderCycle.with_distributor(d).should == []
end
end
it "reports its suppliers" do
oc = create(:simple_order_cycle)