mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Reorganise :though associations on line_item and allow searching/filtering on product, supplier, order_cycle
This commit is contained in:
@@ -5,6 +5,7 @@ require 'spree/core/s3_support'
|
||||
class Enterprise < ApplicationRecord
|
||||
SELLS = %w(unspecified none own any).freeze
|
||||
ENTERPRISE_SEARCH_RADIUS = 100
|
||||
|
||||
searchable_attributes :sells, :is_primary_producer
|
||||
searchable_associations :properties
|
||||
searchable_scopes :is_primary_producer, :is_distributor, :is_hub, :activated, :visible,
|
||||
|
||||
@@ -9,14 +9,17 @@ module Spree
|
||||
include LineItemStockChanges
|
||||
|
||||
searchable_attributes :price, :quantity, :order_id, :variant_id, :tax_category_id
|
||||
searchable_associations :order, :variant, :tax_category, :option_values
|
||||
searchable_associations :order, :order_cycle, :variant, :product, :supplier, :tax_category, :option_values
|
||||
searchable_scopes :with_tax, :without_tax
|
||||
|
||||
belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items
|
||||
has_one :order_cycle, through: :order
|
||||
|
||||
belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant"
|
||||
has_one :product, through: :variant
|
||||
has_one :supplier, through: :product
|
||||
belongs_to :tax_category, class_name: "Spree::TaxCategory"
|
||||
|
||||
has_one :product, through: :variant
|
||||
has_many :adjustments, as: :adjustable, dependent: :destroy
|
||||
|
||||
has_and_belongs_to_many :option_values, join_table: 'spree_option_values_line_items',
|
||||
|
||||
@@ -822,4 +822,36 @@ module Spree
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "searching with ransack" do
|
||||
let(:order_cycle1) { create(:order_cycle) }
|
||||
let(:order_cycle2) { create(:order_cycle) }
|
||||
let(:product1) { create(:product, supplier: create(:supplier_enterprise)) }
|
||||
let(:product2) { create(:product, supplier: create(:supplier_enterprise)) }
|
||||
let!(:line_item1) { create(:line_item, variant: product1.variants.first) }
|
||||
let!(:line_item2) { create(:line_item, variant: product2.variants.first) }
|
||||
|
||||
let(:search_result) { Spree::LineItem.ransack(query).result }
|
||||
|
||||
before do
|
||||
line_item1.order.update_attribute :order_cycle, order_cycle1
|
||||
line_item2.order.update_attribute :order_cycle, order_cycle2
|
||||
end
|
||||
|
||||
context "searching by supplier" do
|
||||
let(:query) { { supplier_id_eq: line_item1.variant.product.supplier_id } }
|
||||
|
||||
it "filters results" do
|
||||
expect(search_result.to_a).to eq [line_item1]
|
||||
end
|
||||
end
|
||||
|
||||
context "searching by order_cycle" do
|
||||
let(:query) { { order_cycle_id_eq: order_cycle1.id } }
|
||||
|
||||
it "filters results" do
|
||||
expect(search_result.to_a).to eq [line_item1]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user