Class: ProductsController

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

Instance Attribute Summary

Attributes inherited from ApplicationController

#req, #res

Instance Method Summary collapse

Methods inherited from ApplicationController

#csrf_token, #initialize, #log_activity, #params, #redirect_to, #render, #render_json, #session, #validate!

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#createObject



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

def create
  data = params['product']
  @product = Product.new(data)
  if @product.save
    log_activity("Created product: #{@product.name}", @product, "Price: #{@product.price}")
    redirect_to '/dashboard/products'
  else
    render 'cms/products_form'
  end
end

#destroyObject



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

def destroy
  @product = Product[params['id']]
  name = @product.name
  @product.destroy
  log_activity("Deleted product: #{name}")
  redirect_to '/dashboard/products'
end

#editObject



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

def edit
  @product = Product[params['id']]
  render 'cms/products_form'
end

#indexObject



4
5
6
7
8
9
10
11
12
# File 'app/controllers/products_controller.rb', line 4

def index
  if req.path.start_with?('/dashboard')
    @products = Product.order(Sequel.desc(:created_at)).all
    render 'cms/products_index'
  else
    @products = Product.where(is_active: true).order(Sequel.desc(:created_at)).all
    render 'products_index', title: "Shop - One-For-All"
  end
end

#newObject



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

def new
  @product = Product.new
  render 'cms/products_form'
end

#showObject



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

def show
  @product = Product.find(slug: params['slug'], is_active: true)
  if @product
    render 'product_show', title: "#{@product.name} - One-For-All"
  else
    @res.status = 404
    render 'error', message: "Product not found."
  end
end

#updateObject



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

def update
  @product = Product[params['id']]
  data = params['product']
  if @product.update(data)
    log_activity("Updated product: #{@product.name}", @product, "New Data: #{data.to_json}")
    redirect_to '/dashboard/products'
  else
    render 'cms/products_form'
  end
end