mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #2818 from nikobozi/refactor-variant-overrides-query
Move query from variant_overrides_controller to its model scope
This commit is contained in:
@@ -61,10 +61,8 @@ module Admin
|
||||
|
||||
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)) }
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -7,10 +7,9 @@ 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) }
|
||||
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 @@ describe VariantOverride do
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user