From ff28da345d57dcd81023ef8d23043ee68c856bfd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 26 Nov 2014 13:07:18 +1100 Subject: [PATCH] Find variant overrides for some hubs --- app/models/variant_override.rb | 4 ++++ spec/models/variant_override_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index 6d20a95571..4c6c29a5ec 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -2,6 +2,10 @@ class VariantOverride < ActiveRecord::Base belongs_to :variant, class_name: 'Spree::Variant' belongs_to :hub, class_name: 'Enterprise' + scope :for_hubs, lambda { |hubs| + where(hub_id: hubs) + } + def self.price_for(variant, hub) VariantOverride.where(variant_id: variant, hub_id: hub).first.andand.price end diff --git a/spec/models/variant_override_spec.rb b/spec/models/variant_override_spec.rb index 093ed6007e..082a7fb135 100644 --- a/spec/models/variant_override_spec.rb +++ b/spec/models/variant_override_spec.rb @@ -1,6 +1,18 @@ require 'spec_helper' describe VariantOverride do + describe "scopes" do + let(:hub1) { create(:distributor_enterprise) } + let(:hub2) { create(:distributor_enterprise) } + let(:v) { create(:variant) } + let!(:vo1) { create(:variant_override, hub: hub1, variant: v) } + let!(:vo2) { create(:variant_override, hub: hub2, variant: v) } + + it "finds variant overrides for a set of hubs" do + VariantOverride.for_hubs([hub1, hub2]).sort.should == [vo1, vo2].sort + end + end + describe "looking up prices" do let(:variant) { create(:variant) } let(:hub) { create(:distributor_enterprise) }