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



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

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



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

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



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

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



30
31
32
# File 'app/controllers/solidus_admin/products_controller.rb', line 30

def edit
  redirect_to action: :show
end

#indexObject



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

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



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

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

#updateObject



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

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