Class: Comable::Admin::ProductsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/comable/admin/products_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_ability, #respond_to_export_with

Instance Method Details

#createObject



23
24
25
26
27
28
29
30
# File 'app/controllers/comable/admin/products_controller.rb', line 23

def create
  if @product.save
    redirect_to comable.admin_product_path(@product), notice: Comable.t('successful')
  else
    flash.now[:alert] = Comable.t('failure')
    render :new
  end
end

#destroyObject



45
46
47
48
49
50
51
52
# File 'app/controllers/comable/admin/products_controller.rb', line 45

def destroy
  if @product.destroy
    redirect_to comable.admin_products_path, notice: Comable.t('successful')
  else
    flash.now[:alert] = Comable.t('failure')
    render :edit
  end
end

#editObject



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

def edit
  set_preview_session
end

#exportObject



54
55
56
57
58
59
# File 'app/controllers/comable/admin/products_controller.rb', line 54

def export
  q = Comable::Product.ransack(params[:q])
  products = q.result.accessible_by(current_ability)

  respond_to_export_with products
end

#importObject



61
62
63
64
65
66
67
68
# File 'app/controllers/comable/admin/products_controller.rb', line 61

def import
  ActiveRecord::Base.transaction do
    Comable::Product.import_from(params[:file])
  end
  redirect_to comable.admin_products_path, notice: Comable.t('successful')
rescue Comable::Importable::Exception => e
  redirect_to comable.admin_products_path, alert: e.message
end

#indexObject



8
9
10
11
# File 'app/controllers/comable/admin/products_controller.rb', line 8

def index
  @q = Comable::Product.ransack(params[:q])
  @products = @q.result(distinct: true).includes(:images, variants: [:option_values, :stocks]).page(params[:page]).accessible_by(current_ability).by_newest
end

#newObject



18
19
20
21
# File 'app/controllers/comable/admin/products_controller.rb', line 18

def new
  @product.variants.build
  @product.published_at = Date.today
end

#showObject



13
14
15
16
# File 'app/controllers/comable/admin/products_controller.rb', line 13

def show
  edit
  render :edit
end

#updateObject



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

def update
  if @product.update_attributes(product_params)
    redirect_to comable.admin_product_path(@product), notice: Comable.t('successful')
  else
    flash.now[:alert] = Comable.t('failure')
    render :edit
  end
end