Class: SolidusAdmin::ProductsController

Inherits:
BaseController
  • Object
show all
Includes:
ControllerHelpers::Search
Defined in:
app/controllers/solidus_admin/products_controller.rb

Instance Method Summary collapse

Methods included from ComponentsHelper

#component

Instance Method Details

#activateObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/solidus_admin/products_controller.rb', line 86

def activate
  @products = Spree::Product.where(id: params[:id])

  Spree::Product.transaction do
    @products
      .where.not(discontinue_on: nil)
      .update_all(discontinue_on: nil)

    @products
      .where("available_on <= ?", Time.current)
      .or(@products.where(available_on: nil))
      .update_all(discontinue_on: nil)
  end

  flash[:notice] = t('.success')
  redirect_to products_path, status: :see_other
end

#destroyObject



63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/solidus_admin/products_controller.rb', line 63

def destroy
  @products = Spree::Product.where(id: params[:id])

  Spree::Product.transaction do
    @products.discard_all
  end

  flash[:notice] = t('.success')
  redirect_to products_path, status: :see_other
end

#discontinueObject



74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/solidus_admin/products_controller.rb', line 74

def discontinue
  @products = Spree::Product.where(id: params[:id])

  Spree::Product.transaction do
    @products
      .update_all(discontinue_on: Time.current)
  end

  flash[:notice] = t('.success')
  redirect_to products_path, status: :see_other
end

#editObject



32
33
34
# File 'app/controllers/solidus_admin/products_controller.rb', line 32

def edit
  redirect_to action: :show
end

#indexObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/solidus_admin/products_controller.rb', line 16

def index
  products = apply_search_to(
    Spree::Product.includes(:master, :variants),
    param: :q,
  )

  set_page_and_extract_portion_from(
    products,
    ordered_by: { updated_at: :desc, id: :desc },
  )

  respond_to do |format|
    format.html { render component('products/index').new(page: @page) }
  end
end

#showObject



36
37
38
39
40
41
42
# File 'app/controllers/solidus_admin/products_controller.rb', line 36

def show
  @product = Spree::Product.with_discarded.friendly.find(params[:id])

  respond_to do |format|
    format.html { render component('products/show').new(product: @product) }
  end
end

#split_paramsObject



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

def split_params
  if params[:product][:taxon_ids].present?
    params[:product][:taxon_ids] = params[:product][:taxon_ids].split(',')
  end
  if params[:product][:option_type_ids].present?
    params[:product][:option_type_ids] = params[:product][:option_type_ids].split(',')
  end
end

#updateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/solidus_admin/products_controller.rb', line 44

def update
  @product = Spree::Product.friendly.find(params[:id])

  if @product.update(params.require(:product).permit!)
    flash[:success] = t('spree.successfully_updated', resource: [
      Spree::Product.model_name.human,
      @product.name.inspect,
    ].join(' '))

    redirect_to action: :show, status: :see_other
  else
    flash.now[:error] = @product.errors.full_messages.join(", ")

    respond_to do |format|
      format.html { render component('products/show').new(product: @product), status: :unprocessable_entity }
    end
  end
end