Class: SolidusAdmin::ProductsController

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

Instance Method Summary collapse

Methods included from ComponentsHelper

#component

Instance Method Details

#activateObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/solidus_admin/products_controller.rb', line 75

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



52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/solidus_admin/products_controller.rb', line 52

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



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

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



5
6
7
# File 'app/controllers/solidus_admin/products_controller.rb', line 5

def edit
  redirect_to action: :show
end

#indexObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/solidus_admin/products_controller.rb', line 36

def index
  products = Spree::Product
    .order(created_at: :desc, id: :desc)
    .ransack(params[:q])
    .result(distinct: true)

  set_page_and_extract_portion_from(
    products,
    per_page: SolidusAdmin::Config[:products_per_page]
  )

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

#showObject



9
10
11
12
13
14
15
# File 'app/controllers/solidus_admin/products_controller.rb', line 9

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

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

#updateObject



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

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