Class: Tienda::ProductsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
# File 'app/controllers/tienda/products_controller.rb', line 19

def create
  @product = Tienda::Product.new(safe_params)
  if @product.save
    redirect_to :products, flash: { notice: t('tienda.products.create_notice') }
  else
    render :new
  end
end

#destroyObject



39
40
41
42
# File 'app/controllers/tienda/products_controller.rb', line 39

def destroy
  @product.destroy
  redirect_to :products, flash: { notice:  t('tienda.products.destroy_notice')}
end

#editObject



28
29
# File 'app/controllers/tienda/products_controller.rb', line 28

def edit
end

#importObject



44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/tienda/products_controller.rb', line 44

def import
  if request.post?
    if params[:import].nil?
      redirect_to import_products_path, flash: { alert: I18n.t('tienda.imports.errors.no_file') }
    else
      Tienda::Product.import(params[:import][:import_file])
      redirect_to products_path, flash: { notice: I18n.t("tienda.products.imports.success") }
    end
  end
end

#indexObject



7
8
9
10
11
12
13
# File 'app/controllers/tienda/products_controller.rb', line 7

def index
  if params[:filter].blank?
    @products = Tienda::Product.root.includes(:stock_level_adjustments, :product_images, :product_category, :variants).order(:name).group_by(&:product_category).sort_by { |cat, pro| cat.name }
  else
    @products = Tienda::Product.root.send(params[:filter]).includes(:stock_level_adjustments, :product_images, :product_category, :variants).order(:name).group_by(&:product_category).sort_by { |cat, pro| cat.name }
  end
end

#newObject



15
16
17
# File 'app/controllers/tienda/products_controller.rb', line 15

def new
  @product = Tienda::Product.new
end

#updateObject



31
32
33
34
35
36
37
# File 'app/controllers/tienda/products_controller.rb', line 31

def update
  if @product.update(safe_params)
    redirect_to [:edit, @product], flash: { notice: t('tienda.products.update_notice') }
  else
    render :edit
  end
end