Merge pull request #8129 from Matt-Yorkley/searchable

Searchable attributes
This commit is contained in:
Matt-Yorkley
2021-09-03 09:43:25 +02:00
committed by GitHub
12 changed files with 91 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ class ApplicationRecord < ActiveRecord::Base
include DelegateBelongsTo
include Spree::Core::Permalinks
include Spree::Preferences::Preferable
include Searchable
self.abstract_class = true
end

View File

@@ -0,0 +1,53 @@
# frozen_string_literal: true
# Whitelists model attributes, scopes, and associations that can be searched on with Ransack.
# Exposes methods for defining the whitelists, eg:
#
# class Widget < ApplicationRecord
# searchable_attributes :number, :state
# searchable_scopes :activated, :disabled
#
# ...
# end
module Searchable
extend ActiveSupport::Concern
DEFAULT_SEARCHABLE_ATTRIBUTES = [
:id, :name, :description, :created_at, :updated_at, :completed_at, :deleted_at
].freeze
included do
class_attribute :whitelisted_search_attributes, instance_accessor: false, default: []
class_attribute :whitelisted_search_associations, instance_accessor: false, default: []
class_attribute :whitelisted_search_scopes, instance_accessor: false, default: []
end
class_methods do
def ransackable_associations(*_args)
self.whitelisted_search_associations.map(&:to_s)
end
def ransackable_attributes(*_args)
(DEFAULT_SEARCHABLE_ATTRIBUTES | self.whitelisted_search_attributes).map(&:to_s)
end
def ransackable_scopes(*_args)
self.whitelisted_search_scopes.map(&:to_s)
end
private
def searchable_attributes(*attrs)
self.whitelisted_search_attributes = attrs
end
def searchable_associations(*attrs)
self.whitelisted_search_associations = attrs
end
def searchable_scopes(*attrs)
self.whitelisted_search_scopes = attrs
end
end
end

View File

@@ -3,6 +3,8 @@
class Customer < ApplicationRecord
acts_as_taggable
searchable_attributes :name, :email, :code
belongs_to :enterprise
belongs_to :user, class_name: Spree.user_class.to_s
has_many :orders, class_name: "Spree::Order"

View File

@@ -6,6 +6,11 @@ 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,
:ready_for_checkout, :not_ready_for_checkout
preference :shopfront_message, :text, default: ""
preference :shopfront_closed_message, :text, default: ""
preference :shopfront_taxon_order, :string, default: ""

View File

@@ -3,6 +3,10 @@
require 'open_food_network/scope_variant_to_hub'
class OrderCycle < ApplicationRecord
searchable_attributes :orders_open_at, :orders_close_at, :coordinator_id
searchable_scopes :active, :inactive, :active_or_complete, :upcoming, :closed, :not_closed,
:dated, :undated, :soonest_opening, :soonest_closing, :most_recently_closed
belongs_to :coordinator, class_name: 'Enterprise'
has_many :coordinator_fee_refs, class_name: 'CoordinatorFee'

View File

@@ -4,6 +4,9 @@ module Spree
class Address < ApplicationRecord
include AddressDisplay
searchable_attributes :firstname, :lastname
searchable_associations :country, :state
belongs_to :country, class_name: "Spree::Country"
belongs_to :state, class_name: "Spree::State"

View File

@@ -8,6 +8,10 @@ module Spree
include VariantUnits::VariantAndLineItemNaming
include LineItemStockChanges
searchable_attributes :price, :quantity, :order_id, :variant_id, :tax_category_id
searchable_associations :order, :variant, :tax_category, :option_values
searchable_scopes :with_tax, :without_tax
belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items
belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant"
belongs_to :tax_category, class_name: "Spree::TaxCategory"

View File

@@ -9,10 +9,14 @@ require 'concerns/order_shipment'
module Spree
class Order < ApplicationRecord
prepend OrderShipment
include Checkout
include Balance
searchable_attributes :number, :state, :shipment_state, :payment_state, :distributor_id,
:order_cycle_id, :email
searchable_associations :shipping_method, :bill_address
searchable_scopes :complete, :incomplete
checkout_flow do
go_to_state :address
go_to_state :delivery

View File

@@ -30,6 +30,10 @@ module Spree
acts_as_paranoid
searchable_attributes :supplier_id, :primary_taxon_id, :meta_keywords
searchable_associations :supplier, :properties, :primary_taxon, :variants, :master
searchable_scopes :active
has_many :product_option_types, dependent: :destroy
# We have an after_destroy callback on Spree::ProductOptionType. However, if we
# don't specify dependent => destroy on this association, it is not called. See:

View File

@@ -2,6 +2,8 @@
module Spree
class User < ApplicationRecord
searchable_attributes :email
devise :database_authenticatable, :token_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable,
:encryptable, :confirmable, encryptor: 'authlogic_sha512', reconfirmable: true

View File

@@ -13,6 +13,10 @@ module Spree
acts_as_paranoid
searchable_attributes :sku, :display_as, :display_name
searchable_associations :product, :option_values, :default_price
searchable_scopes :active, :deleted
belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product'
delegate_belongs_to :product, :name, :description, :permalink, :available_on,

View File

@@ -5,6 +5,10 @@ class Subscription < ApplicationRecord
"Spree::Gateway::StripeConnect",
"Spree::Gateway::StripeSCA"].freeze
searchable_attributes :shop_id, :canceled_at, :paused_at
searchable_associations :shop
searchable_scopes :active, :not_ended, :not_paused, :not_canceled
belongs_to :shop, class_name: 'Enterprise'
belongs_to :customer
belongs_to :schedule