From 27bc28ffa4b2837aa2336e12bc66cb4b06da9e4e Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 4 Jun 2015 12:09:34 +1000 Subject: [PATCH] Index variants by id --- app/models/spree/variant_decorator.rb | 7 +++++++ spec/models/spree/variant_spec.rb | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 72a7fb4dec..a1a68afd9e 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -46,6 +46,13 @@ Spree::Variant.class_eval do } + def self.indexed + Hash[ + scoped.map { |v| [v.id, v] } + ] + end + + def price_with_fees(distributor, order_cycle) price + fees_for(distributor, order_cycle) end diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 73aa8900d1..77e50af4a8 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -104,6 +104,17 @@ module Spree end end + describe "indexing variants by id" do + let!(:v1) { create(:variant) } + let!(:v2) { create(:variant) } + let!(:v3) { create(:variant) } + + it "indexes variants by id" do + Variant.where(id: [v1, v2, v3]).indexed.should == + {v1.id => v1, v2.id => v2, v3.id => v3} + end + end + describe "generating the full name" do let(:v) { Variant.new }