diff --git a/app/controllers/admin/variant_overrides_controller.rb b/app/controllers/admin/variant_overrides_controller.rb index 4a49f53c7c2..f7145e84c70 100644 --- a/app/controllers/admin/variant_overrides_controller.rb +++ b/app/controllers/admin/variant_overrides_controller.rb @@ -61,10 +61,8 @@ def load_data def inventory_import_dates import_dates = VariantOverride. - select('DISTINCT variant_overrides.import_date'). - where('variant_overrides.hub_id IN (?) - AND variant_overrides.import_date IS NOT NULL', editable_enterprises.collect(&:id)). - order('import_date DESC') + distinct_import_dates. + for_hubs(editable_enterprises.collect(&:id)) options = [{ id: '0', name: 'All' }] import_dates.collect(&:import_date).map { |i| options.push(id: i.to_date, name: i.to_date.to_formatted_s(:long)) } diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index a6a5dfe8edf..89d2fdcd134 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -3,8 +3,6 @@ class VariantOverride < ActiveRecord::Base acts_as_taggable - attr_accessor :import_date - belongs_to :hub, class_name: 'Enterprise' belongs_to :variant, class_name: 'Spree::Variant' @@ -21,6 +19,12 @@ class VariantOverride < ActiveRecord::Base where(hub_id: hubs) } + scope :distinct_import_dates, lambda { + select('DISTINCT variant_overrides.import_date'). + where('variant_overrides.import_date IS NOT NULL'). + order('import_date DESC') + } + localize_number :price def self.indexed(hub) diff --git a/spec/models/variant_override_spec.rb b/spec/models/variant_override_spec.rb index 0a07fdd1af6..bb799f86035 100644 --- a/spec/models/variant_override_spec.rb +++ b/spec/models/variant_override_spec.rb @@ -7,10 +7,9 @@ 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) } - let!(:vo3) { create(:variant_override, hub: hub1, variant: v, permission_revoked_at: Time.now) } + let!(:vo1) { create(:variant_override, hub: hub1, variant: variant, import_date: Time.zone.now.yesterday) } + let!(:vo2) { create(:variant_override, hub: hub2, variant: variant, import_date: Time.zone.now) } + let!(:vo3) { create(:variant_override, hub: hub1, variant: variant, permission_revoked_at: Time.now) } it "ignores variant_overrides with revoked_permissions by default" do expect(VariantOverride.all).to_not include vo3 @@ -21,10 +20,17 @@ VariantOverride.for_hubs([hub1, hub2]).should match_array [vo1, vo2] end + it "fetches import dates for hubs in descending order" do + import_dates = VariantOverride.distinct_import_dates.pluck :import_date + + expect(import_dates[0].to_i).to eq(vo2.import_date.to_i) + expect(import_dates[1].to_i).to eq(vo1.import_date.to_i) + end + describe "fetching variant overrides indexed by variant" do it "gets indexed variant overrides for one hub" do - VariantOverride.indexed(hub1).should == {v => vo1} - VariantOverride.indexed(hub2).should == {v => vo2} + VariantOverride.indexed(hub1).should == {variant => vo1} + VariantOverride.indexed(hub2).should == {variant => vo2} end end end