Added category deletion confirmation
authorNguyễn Thái Ngọc Duy <[email protected]>
Sat, 19 Aug 2006 07:21:19 +0000 (19 14:21 +0700)
committerNguyễn Thái Ngọc Duy <[email protected]>
Sat, 19 Aug 2006 07:21:19 +0000 (19 14:21 +0700)
app/controllers/admin_controller.rb
app/views/admin/categories.rhtml
app/views/admin/categories_confirm.rhtml
test/functional/admin_controller_test.rb [new file with mode: 0644]

index a1f212c..1283ace 100644 (file)
@@ -2,6 +2,20 @@ class AdminController < ApplicationController
   before_filter :login_required
 
   def categories
+    if params.key? 'add_cat'
+      add_category
+    elsif params.key? 'del_cat_comply'
+      delete_category
+    elsif params.key? 'del_cat'
+      if params['cat_to_delete'].nil?
+        flash[:error] = 'You must choose a category to delete'
+        return
+      end
+      @category = Category.find_by_id params[:cat_to_delete]
+      render :action => 'categories_confirm'
+    elsif params.key? 'update'
+      update_categories
+    end
     @categories = Category.find :all, :order => 'disp_position'
   end
 
@@ -22,19 +36,6 @@ class AdminController < ApplicationController
     end
   end
 
-  def manipulate_category
-    if params.key? 'add_cat'
-      add_category
-    elsif params.key? 'del_cat'
-      delete_category
-    elsif params.key? 'update'
-      update_categories
-    else
-      flash[:error] = 'Unknown action'
-    end
-    redirect_to :action => 'categories'
-  end
-
   def forums
     @categories = Category.find :all, :order => :disp_position
     @forums = Forum.find :all, :include => :category, :order => "categories.disp_position, forums.disp_position"
@@ -253,36 +254,26 @@ class AdminController < ApplicationController
   private
 
   def add_category
-    if params['new_cat_name'].nil? or params['new_cat_name'] == ''
-      flash[:error] = 'You must enter a name for the category'
+    cat = Category.new params[:category]
+    if cat.save
+      flash[:notice] = "Category #{cat.cat_name} added"
     else
-      cat = Category.new :cat_name => params['new_cat_name']
-      if cat.save
-        flash[:notice] = "Category #{cat.cat_name} added"
-      else
-        flash[:error] = cat.errors
-      end
+      flash[:error] = cat.errors
     end
   end
 
   def delete_category
-    if params['cat_to_delete'].nil?
-      flash[:error] = 'You must choose a category to delete'
+    if Category.delete(params['cat_to_delete'])
+      flash[:notice] = 'Category deleted'
     else
-      if Category.delete(params['cat_to_delete'])
-        flash[:notice] = 'Category deleted'
-      else
-        flash[:notice] = 'Unable to delete category'
-      end
+      flash[:notice] = 'Unable to delete category'
     end
   end
 
   def update_categories
     categories = Category.find(params[:cat].keys)
     for i  in 0...categories.length
-      # TODO: validation checks
-      categories[i].cat_name = params[:cat][categories[i].id.to_s][:cat_name]
-      categories[i].disp_position = params[:cat][categories[i].id.to_s][:disp_position]
+      categories[i].attributes= params[:cat][categories[i].id.to_s]
     end
     categories.each { |cat| cat.save or flash[:error] = cat.errors }
     flash[:notice] = 'Categories updated' if flash[:error].nil?
index f652e42..f87ea36 100644 (file)
@@ -2,7 +2,7 @@
        <div class="blockform">
                <h2><span>Add/remove/edit categories</span></h2>
                <div class="box">
-               <form method="post" action="<%=h url_for :action => 'manipulate_category' %>">
+               <form method="post" action="<%= url_for :action => 'categories' %>">
                        <div class="inform">
                                <fieldset>
                                        <legend>Add/delete categories</legend>
@@ -11,7 +11,7 @@
                                                        <tr>
                                                                <th scope="row">Add a new category<div><input type="submit" name="add_cat" value="Add New" tabindex="2" /></div></th>
                                                                <td>
-                                                                       <input type="text" name="new_cat_name" size="35" maxlength="80" tabindex="1" />
+                                                                       <input type="text" name="category[cat_name]" size="35" maxlength="80" tabindex="1" />
                                                                        <span>The name of the new category you want to add. You can edit the name of the category later (see below).Go to <a href="/admin/forums">Forums</a> to add forums to your new category.</span>
                                                                </td>
                                                        </tr>
index 30978c5..ab001d4 100644 (file)
@@ -1,14 +1,14 @@
-<%= render :partial => 'menu' -%>
+<%= render :partial => 'menu', :locals => {:action => 'categories'} -%>
        <div class="blockform">
                <h2><span>Category delete</span></h2>
                <div class="box">
-                       <form method="post" action="admin_categories.php">
+                       <form method="post" action="/admin/categories">
                                <div class="inform">
-                               <input type="hidden" name="cat_to_delete" value="<?php echo $cat_to_delete ?>" />
+                               <input type="hidden" name="cat_to_delete" value="<%= params[:cat_to_delete] %>" />
                                        <fieldset>
                                                <legend>Confirm delete category</legend>
                                                <div class="infldset">
-                                                       <p>Are you sure that you want to delete the category "<?php echo $cat_name ?>"?</p>
+                                                       <p>Are you sure that you want to delete the category "<%=h @category.cat_name %>"?</p>
                                                        <p>WARNING! Deleting a category will delete all forums and posts (if any) in that category!</p>
                                                </div>
                                        </fieldset>
diff --git a/test/functional/admin_controller_test.rb b/test/functional/admin_controller_test.rb
new file mode 100644 (file)
index 0000000..4bbd028
--- /dev/null
@@ -0,0 +1,40 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'admin_controller'
+
+# Re-raise errors caught by the controller.
+class AdminController; def rescue_action(e) raise e end; end
+
+class AdminCategoryTest < Test::Unit::TestCase
+  fixtures :settings, :groups, :users
+  def setup
+    @controller = AdminController.new
+    @request    = ActionController::TestRequest.new
+    @response   = ActionController::TestResponse.new
+    User.current_user = users(:admin)
+  end
+
+  def test_categories
+    get :categories, {:add_cat => true, :category => {:cat_name => 'testi'}}
+    assert_response :success
+    assert flash.key?(:error) == false
+    c = Category.find_by_cat_name('testi')
+    assert_not_nil c
+
+    get :categories, {:update => true, :cat => { "#{c.id}" => { :cat_name => 'testtest', :disp_position => 2}}}
+    assert_response :success
+    assert flash.key?(:error) == false
+    c.reload
+    assert_equal c.cat_name, 'testtest'
+    assert_equal c.disp_position, 2
+
+    get :categories, {:del_cat => true, :cat_to_delete => c.id}
+    assert_response :success
+    assert flash.key?(:error) == false
+    assert_template 'admin/categories_confirm'
+
+    get :categories, {:del_cat_comply => true, :cat_to_delete => c.id}
+    assert_response :success
+    assert flash.key?(:error) == false
+    assert_nil Category.find_by_id(c.id)
+  end
+end