Class: Forge::ProductsController

Inherits:
ForgeController show all
Defined in:
lib/forge/app/controllers/forge/products_controller.rb

Instance Method Summary collapse

Methods inherited from ForgeController

#get_menu_items, #load_help, #set_crumbs, #set_title, #uses_ckeditor

Methods inherited from ApplicationController

#app_init

Instance Method Details

#createObject

POST /forge_products



64
65
66
67
68
69
70
71
72
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 64

def create
  @product = Product.new(params[:product])
  if @product.save
    flash[:notice] = 'Your product has been added.  You can add images to this product, or go back and <a href="/forge/products/new">add another one</a>.'
    redirect_to edit_forge_product_path(@product)
  else
    render :action => "new"
  end
end

#destroyObject

DELETE /forge_products/1



41
42
43
44
45
46
47
48
49
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 41

def destroy
  @product = Product.find(params[:id])
  if @product.destroy
    flash[:notice] = "Product deleted"
  else
    flash[:warning] = "Product could not be deleted"
  end
  redirect_to(forge_products_url)
end

#editObject

GET /forge_products/1/edit



35
36
37
38
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 35

def edit
  @help = HelpTopic.where(:slug => "products_new").first
  @product = Product.find_with_images(params[:id])
end

#indexObject

TODO: at the moment, only up to 100 products are shown. This is so that product reordering can work properly (it doesn’t work well with pagination). A better solution needs to be put in place for this.



8
9
10
11
12
13
14
15
16
17
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 8

def index
  respond_to do |format|
    format.html { @products = Product.limit(100).order("list_order ASC") }
    format.js  {
      params[:q] ||= ''
      @products = Product.where("LOWER(title) LIKE :q OR LOWER(description) LIKE :q", {:q => "%#{params[:q].downcase}%"}).limit(20)
      render :partial => "product_list"
    }
  end
end

#newObject

GET /forge_products/new



25
26
27
28
29
30
31
32
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 25

def new
  @product = Product.new
  @help = HelpTopic.where(:slug => "products_new").first
  if @categories.length == 0
    flash[:notice] = "You need to create at least one category before adding products.  You can do that right here."
    redirect_to forge_product_categories_path
  end
end

#reorderObject



74
75
76
77
78
79
80
81
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 74

def reorder
  list = params[:product_list].map {|id| id.gsub('menu_item_', '')}
  params[:parent_id] ? Product.reorder!(list) : ProductCategory.reorder!(list)
  render :nothing => true
  respond_to do |format|
    format.js { render :status => 200, :text => 'Success' }
  end
end

#searchObject



19
20
21
22
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 19

def search
  params[:search] ||= ''
  @products = Product.find(:all, :order => "title", :conditions => ["LOWER(title) LIKE ? OR LOWER(description) LIKE ?", "%#{params[:search].downcase}%", "%#{params[:search].downcase}%"])
end

#updateObject

PUT /forge_products/1



52
53
54
55
56
57
58
59
60
61
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 52

def update
  @product = Product.find(params[:id])
  if @product.update_attributes(params[:product])
    flash[:notice] = 'Your product has been saved.  You can continue making changes, <a href="/forge/products/new">add another one</a>, or <a href="/forge/products">browse the product list</a>.'
    redirect_to edit_forge_product_path(@product)
  else
    flash[:warning] = "There was an error saving your product."
    render :action => "edit"
  end
end