diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 77b49a9110..c4ba0a401c 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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, diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index 5c3412199b..4b698f3dbd 100644 --- a/app/models/spree/line_item.rb +++ b/app/models/spree/line_item.rb @@ -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', diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 261a2f635d..f6db90d611 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -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