Class: Spree::Admin::ProductsController

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

Instance Method Summary collapse

Methods included from AssetsHelper

#clear_session_for_uploaded_assets, #ensure_session_uploaded_assets_uuid, #media_form_assets, #session_uploaded_assets, #session_uploaded_assets_uuid_key, #store_uploaded_asset_in_session

Methods included from BulkOperationsConcern

#bulk_add_tags, #bulk_remove_tags

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

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

#bulk_add_to_taxonsObject



112
113
114
115
116
117
118
# File 'app/controllers/spree/admin/products_controller.rb', line 112

def bulk_add_to_taxons
  taxons = current_store.taxons.accessible_by(current_ability, :manage).where(id: params[:taxon_ids])
  Spree::Taxons::AddProducts.call(taxons: taxons, products: bulk_collection)
  Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)

  handle_bulk_operation_response
end

#bulk_remove_from_taxonsObject



104
105
106
107
108
109
110
# File 'app/controllers/spree/admin/products_controller.rb', line 104

def bulk_remove_from_taxons
  taxons = current_store.taxons.accessible_by(current_ability, :manage).where(id: params[:taxon_ids])
  Spree::Taxons::RemoveProducts.call(taxons: taxons, products: bulk_collection)
  Spree::Product.bulk_auto_match_taxons(current_store, bulk_collection.ids)

  handle_bulk_operation_response
end

#bulk_status_updateObject



96
97
98
99
100
101
102
# File 'app/controllers/spree/admin/products_controller.rb', line 96

def bulk_status_update
  bulk_collection.update_all(status: params[:status], updated_at: Time.current)
  bulk_collection.each(&:enqueue_search_index) # reindex products
  invoke_callbacks(:bulk_status_update, :after)

  handle_bulk_operation_response
end

#cloneObject



84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/spree/admin/products_controller.rb', line 84

def clone
  clone_result = @product.duplicate

  if clone_result.success?
    flash[:success] = Spree.t('notice_messages.product_cloned')
    redirect_to spree.edit_admin_product_path(clone_result.value)
  else
    flash[:error] = Spree.t('notice_messages.product_not_cloned', error: clone_result.error.value)
    redirect_to spree.edit_admin_product_path(@product)
  end
end

#searchObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/spree/admin/products_controller.rb', line 31

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

  head :ok and return if query.blank? || query.length < 3

  scope = current_store.products.not_archived.accessible_by(current_ability, :index)
  scope = scope.where.not(id: params[:omit_ids].split(',')) if params[:omit_ids].present?
  @products = scope.includes(:primary_media).search(query).limit(params[:limit] || 10)

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

#select_optionsObject



120
121
122
# File 'app/controllers/spree/admin/products_controller.rb', line 120

def select_options
  render json: current_store.products.not_archived.accessible_by(current_ability, :index).to_tom_select_json
end

#showObject



53
54
55
# File 'app/controllers/spree/admin/products_controller.rb', line 53

def show
  redirect_to action: :edit
end

#updateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/spree/admin/products_controller.rb', line 57

def update
  invoke_callbacks(:update, :before)

  success = ActiveRecord::Base.transaction do
    @prepare_params_service&.variants_to_discontinue&.each(&:discontinue!)

    unless @product.update(permitted_resource_params)
      raise ActiveRecord::Rollback
    end

    true
  end

  if success
    set_current_store
    invoke_callbacks(:update, :after)
    flash[:success] = flash_message_for(@product, :successfully_updated)
    redirect_to location_after_save
  else
    # Stops people submitting blank slugs, causing errors when they try to
    # update the product again
    @product.slug = @product.slug_was if @product.slug.blank?
    invoke_callbacks(:update, :fails)
    render :edit, status: :unprocessable_content
  end
end