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



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

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



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

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



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

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

#indexObject

GET /forge_products



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

def index
  # @categories = ProductCategory.find(:all, :order => "list_order ASC")

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

#newObject

GET /forge_products/new



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

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



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

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
# File 'lib/forge/app/controllers/forge/products_controller.rb', line 19

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

#updateObject

PUT /forge_products/1



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

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