Class: ProductsController

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

Instance Method Summary collapse

Methods inherited from AdminController

#clean_search, #require_admin

Methods inherited from OfficeController

#error

Methods included from OfficeHelper

#best_euros, #current_basket, #current_basket_or_nil, #current_clerk, #date, #euros, #has_ssl?, #markdown, #new_basket, #paginate, #shipping_method

Instance Method Details

#createObject



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

def create
  @product = Product.create(params_for_model)
  if @product.save
    flash.notice = t(:create_success, :model => "product")
    show = @product.product_item? ? @product.product : @product
    redirect_to product_path(show)
  else
    flash.alert = t(:fix_errors, :model => "product")
    render :action => :edit
  end
end

#destroyObject



59
60
61
62
63
64
65
66
# File 'app/controllers/products_controller.rb', line 59

def destroy
  @product.delete
  if @product.save
    redirect_to products_url , :notice => t("deleted") + ": " + @product.full_name
  else
    redirect_to product_url(@product) , :notice => "#{t(:error)} : #{t(:product_has_inventory)}"
  end
end

#editObject



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

def edit
end

#indexObject

Uncomment for check abilities with CanCan authorize_resource



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

def index
  param = params[:q] || {}
  param.merge!(:product_id_null => 1)    unless( params[:basket])
  @q = Product.search( param )
  @product_scope = @q.result(:distinct => true)
  @products = @product_scope.includes(:products , :supplier , :category).paginate( :page => params[:page], :per_page => 20 ).to_a
end

#load_productObject



68
69
70
# File 'app/controllers/products_controller.rb', line 68

def load_product
  @product = Product.find(params[:id])
end

#newObject



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

def new
  if params[:parent_id]
    parent = Product.find params[:parent_id]
    @product = parent.new_product_item
  else
    @product = Product.new :tax => OfficeClerk.config("defaults.tax")
  end
  render :edit
end

#params_for_modelObject



72
73
74
75
76
# File 'app/controllers/products_controller.rb', line 72

def params_for_model
  params.require(:product).permit(:price,:cost,:weight,:name,:description, :online, :summary,:stock_level,
    :link,:ean,:tax,:properties,:scode,:product_id,:category_id,:supplier_id, :main_picture,:extra_picture
)
end

#showObject



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

def show
  gon.product_id = @product.id
end

#updateObject



47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/products_controller.rb', line 47

def update
  respond_to do |format|
    if @product.update_attributes(params_for_model)
      format.html { redirect_to(@product, :notice => t(:update_success, :model => "product")) }
      format.json { respond_with_bip(@product) }
    else
      format.html { render :action => :edit }
      format.json { respond_with_bip(@product) }
    end
  end
end