Class: Spree::Admin::VariantsController

Inherits:
ResourceController show all
Includes:
ProductsBreadcrumbConcern, StockLocationsHelper
Defined in:
app/controllers/spree/admin/variants_controller.rb

Instance Method Summary collapse

Methods included from StockLocationsHelper

#available_stock_locations, #available_stock_locations_for_product, #available_stock_locations_list, #default_stock_location_for_product

Methods inherited from ResourceController

belongs_to, #create, #destroy, #edit, #index, #new, #update

Methods included from TableConcern

#apply_table_sort, #custom_sort_active?, #process_table_query_state, #table, #table_key, #table_registered?

Methods included from BreadcrumbConcern

#add_breadcrumb_icon_instance_var

Instance Method Details

#searchObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/spree/admin/variants_controller.rb', line 16

def search
  query = params[:q]&.strip

  if query.blank? || query.length < 3
    respond_to do |format|
      format.turbo_stream { head :ok }
      format.json { render json: [] }
    end
  else
    access_action = request.post? ? :manage : :index

    scope = current_store.variants.accessible_by(current_ability, access_action)
    unless params[:all].to_b.present?
      scope = if params[:currency].present?
                scope.active(params[:currency]).merge(current_store.products.published)
              else
                scope.active.merge(current_store.products.published)
              end
    end
    scope = scope.where.not(id: params[:omit_ids].split(',')) if params[:omit_ids].present?
    scope = scope.where(spree_stock_items: { stock_location_id: params[:stock_location_id] }) if params[:stock_location_id].present?

    @variants = scope.
                eligible.
                search(query).
                includes(:images, :prices, :stock_items, :stock_locations, :product, option_values: :option_type).
                limit(params[:limit] || 20).
                reorder('').
                distinct(false)

    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: [
          turbo_stream.replace(
            "variants_search_results",
            partial: "spree/admin/variants/search_results",
            locals: { variants: @variants }
          )
        ]
      end

      format.json do
        # we cannot use pluck here, as `descriptive_name` is not a column
        render json: @variants.map { |v| { id: v.id, name: v.descriptive_name } }
      end
    end
  end
end