Permalink
Browse files

Add category validation, made some fixes and add integration tests

  • Loading branch information...
wlodars committed Jul 8, 2008
1 parent fbcb87c commit 2eb39d7be5b263f081e4cf0f8b43314dbd8679a9
@@ -4,7 +4,7 @@ class Admin::CategoriesController < ApplicationController
# GET /admin/categories
def index
- @categories = Category.all(:order => 'position ASC')
+ @categories = Category.list
end
# GET /admin/categories/1
@@ -15,9 +15,11 @@ def show
# POST /admin/categories
def create
- temp = Category.find_by_sql "SELECT MAX(position) as position FROM categories"
- @category = Category.new(:name => 'New category', :value => 'newcategory', :position => temp[0].position + 1)
+ max_position = Category.find_by_sql "SELECT MAX(position) as position FROM categories"
+ max_id = Category.find_by_sql "SELECT MAX(id) as id FROM categories"
+ @category = Category.new(:name => 'New category', :value => "newcategory#{max_id[0].id + 1}", :position => max_position[0].position + 1)
@category.save
+ flash_notice("Category has been added")
respond_to do |format|
format.html { redirect_to admin_categories_url }
@@ -28,11 +30,22 @@ def create
# PUT /admin/categories/1
def update
@category = Category.find(params[:id])
+ flash_notice("Category has been updated")
respond_to do |format|
if @category.update_attributes(:name => params[:name], :value => params[:url])
format.html { redirect_to admin_categories_url }
format.js # admin/categories/update.js.rjs
+ else
+ format.html { redirect_to admin_categories_url }
+ format.js {
+ render :update do |page|
+ @category.reload
+ page.alert @category.errors.full_messages.join("\n")
+ page.replace("category_#{@category.id}", :partial => 'admin/categories/category', :category => @category)
+ page.sortable 'categoriesContainer', :tag => 'div', :url => saveorder_admin_categories_path
+ end
+ }
end
end
end
@@ -43,6 +56,7 @@ def saveorder
category = Category.find(id)
category.update_attribute('position', position)
end
+ flash_notice("Categories order changed. Saving ...")
respond_to do |format|
format.html { redirect_to admin_categories_url }
@@ -54,6 +68,7 @@ def saveorder
def destroy
@category = Category.find(params[:id])
@category.destroy if @category.jobs.empty?
+ flash_notice("Category has been deleted")
respond_to do |format|
format.html { redirect_to admin_categories_url }
@@ -15,6 +15,7 @@ def show
# PUT /admin/jobs/1
def update
@job = Job.find(params[:id])
+ flash_notice("Job has been activated/deactivated")
respond_to do |format|
if @job.update_attributes(:is_active => @job.is_active ? false : true)
@@ -28,6 +29,7 @@ def update
def destroy
@job = Job.find(params[:id])
@job.destroy
+ flash_notice("Job has been deleted")
respond_to do |format|
format.html { redirect_to admin_jobs_url }
@@ -20,6 +20,11 @@ def login_required
redirect_to login_url
end
end
+
+ def flash_notice(string)
+ flash[:notice] = string
+ flash.discard(:notice)
+ end
protected
def production?
@@ -4,6 +4,6 @@ def admin_category_nav_item(category)
end
def show_save_category(category_id)
- "Element.show('saveCategory#{category_id}');Element.replace('messagesContainer', '<div id=\"messagesContainer\" style=\"display:none\">Value changed. You must save the change!</div>');Element.show('messagesContainer')"
+ "Element.show('saveCategory#{category_id}')"
end
end
@@ -22,4 +22,8 @@ def error_message_for(record, attribute, error_msg = nil)
return content_tag(:div, error_msg || error_list.join(", "), :class => "error-message")
end
end
+
+ def display_notice
+ page.insert_html :after, 'footer', :partial => 'layouts/admin_flash_boxes'
+ end
end
View
@@ -1,6 +1,9 @@
class Category < ActiveRecord::Base
has_many :jobs
-
+
+ validates_presence_of :name, :value
+ validates_uniqueness_of :value
+
def to_param
self.value
end
@@ -2,8 +2,4 @@ page.insert_html :bottom, 'categoriesContainer', :partial => 'admin/categories/c
page.sortable 'categoriesContainer', :tag => 'div', :url => saveorder_admin_categories_path
@category = nil
page.replace_html "categories", :partial => 'admin/categories/list'
-page.replace_html "messagesContainer", "Category has been added"
-page.show "messagesContainer"
-page.delay(3) do
- page.visual_effect :fade, "messagesContainer"
-end
+page.display_notice
@@ -1,12 +1,10 @@
if @category.jobs.empty?
- page["category_#{@category.id}"].visual_effect :switch_off, "category_#{@category.id}"
- page.remove "category_#{@category.id}"
- page.replace_html "categories", :partial => 'admin/categories/list'
- page.replace_html "messagesContainer", "Category has been deleted"
- page.show "messagesContainer"
- page.delay(3) do
- page.visual_effect :fade, "messagesContainer"
+ page["category_#{@category.id}"].visual_effect :fade, "category_#{@category.id}"
+ page.delay(1) do
+ page.remove "category_#{@category.id}"
end
+ page.replace_html "categories", :partial => 'admin/categories/list'
+ page.display_notice
else
page.alert("You cannot delete this category because there are jobs in this category!")
end
@@ -1,6 +1,2 @@
page.replace_html "categories", :partial => 'admin/categories/list'
-page.replace_html "messagesContainer", "Categories order changed. Saving ..."
-page.show "messagesContainer"
-page.delay(3) do
- page.visual_effect :fade, "messagesContainer"
-end
+page.display_notice
@@ -2,8 +2,4 @@ page["category_#{@category.id}"].visual_effect :pulsate, "category_#{@category.i
page.hide "saveCategory#{@category.id}"
@category = nil
page.replace_html "categories", :partial => 'admin/categories/list'
-page.replace_html "messagesContainer", "Category has been updated"
-page.show "messagesContainer"
-page.delay(3) do
- page.visual_effect :fade, "messagesContainer"
-end
+page.display_notice
@@ -1,6 +1,2 @@
page["job_#{@job.id}"].visual_effect :fade, "job_#{@job.id}"
-page.replace_html "messagesContainer", "Job has been deleted"
-page.show "messagesContainer"
-page.delay(3) do
- page.visual_effect :fade, "messagesContainer"
-end
+page.display_notice
@@ -1,6 +1,2 @@
page.replace "activate_#{@job.id}", activation_image_tag(@job)
-page.replace_html "messagesContainer", "Job has been activated/deactivated"
-page.show "messagesContainer"
-page.delay(3) do
- page.visual_effect :fade, "messagesContainer"
-end
+page.display_notice
@@ -0,0 +1,4 @@
+- flash.each do |key, msg|
+ = content_tag :div, msg, :class => [key], :id => "#{key}_message"
+ - content_tag :script, :type => "text/javascript" do
+ = "setTimeout(\"new Effect.Fade('#{key}_message');\", 2000);"
@@ -1,16 +1,14 @@
#header
%h1#logo= link_to AppConfig.site_name, root_path
- if logged_in?
- %ul#top{:style => 'padding-top:50px'}
+ %ul#top{:style => 'padding-top:40px'}
%li= link_to "Pages", "#"
= bull
%li= link_to "Categories", admin_categories_path
= bull
%li= link_to "Change your password", "#"
= bull
%li= link_to "Logout", logout_path
-
- = partial "layouts/flash_boxes"
-
+
- unless logged_in?
#categs-nav
@@ -19,5 +19,5 @@
#content
= yield
- .footer
- #messagesContainer{:style => 'display:none'}
+ #footer
+ = render :partial => 'layouts/admin_flash_boxes'
@@ -1,5 +1,6 @@
.login_status
- if logged_in?
= "You are logged as #{current_user.login} " + link_to('Logout', logout_path)
+ = link_to '/ Admin tools', admin_path
- else
= link_to 'Login', login_path
@@ -1024,15 +1024,27 @@ div.footer {
z-index:3000;
}
-#messagesContainer {
+#notice_message {
left:50%;
margin-left:-150px;
position:absolute;
top:0pt;
width:300px;
background-color:#fff;
- height:30px;
border:8px solid #eeeeee;
font-size:9px;
padding-top:10px;
+ text-align:center;
+}
+
+#error_message {
+ left:50%;
+ margin-left:-150px;
+ position:absolute;
+ top:0pt;
+ width:300px;
+ background-color:#fff;
+ border:8px solid #ec0f0f;
+ font-size:9px;
+ padding-top:10px;
}
View
@@ -4,7 +4,7 @@ bob:
mark:
login: mark
- password: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 # longtest
+ password: bf5a0cb861642b94cb8635dce9d197d375569e1f # longtest
admin:
login: admin
@@ -1,7 +1,7 @@
-full-time:
+fulltime:
name: Full-time
value: fulltime
-part-time:
+parttime:
name: Part-time
value: parttime
View
@@ -9,7 +9,7 @@ one:
outside_location:
poster_email: temp@temp.us
apply_online: false
- job_type: full-time
+ job_type: fulltime
category: programmer
location: seatle
@@ -24,6 +24,6 @@ two:
outside_location:
poster_email: admin@admin.sd
apply_online: false
- job_type: part-time
+ job_type: parttime
category: designer
location: tampa
@@ -23,21 +23,33 @@ def test_should_create_category
assert_difference('Category.count') do
xhr :post, :create
end
+ assert_select_rjs :insert_html, :bottom, 'categoriesContainer'
end
def test_should_update_category
login_as(:admin)
- xhr :put, :update, {:id => categories(:programmer).id, :name => "New name"}
+ get :index
+ xhr :put, :update, {:id => categories(:programmer).id, :name => "New name", :url => 'new_value23'}
+ assert_response :success
categories(:programmer).reload
assert_equal('New name', categories(:programmer).name)
end
+ def test_should_not_update_category
+ login_as(:mark)
+ category = categories(:programmer)
+ xhr :put, :update, {:id => categories(:programmer).id, :name => "Programmers", :url => 'new_value23'}
+ categories(:programmer).reload
+ assert_equal category.name, categories(:programmer).name
+ end
+
def test_should_delete_category_without_jobs
login_as(:bob)
assert_difference('Category.count', -1) do
xhr :delete, :destroy, :id => categories(:administrator).id
end
assert_response :success
+ assert_select_rjs :remove, "category_#{categories(:administrator).id}"
end
def test_should_not_delete_category_with_jobs
Oops, something went wrong.

0 comments on commit 2eb39d7

Please sign in to comment.