Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add category validation, made some fixes and add integration tests

  • Loading branch information...
commit 2eb39d7be5b263f081e4cf0f8b43314dbd8679a9 1 parent fbcb87c
@wlodars wlodars authored
View
21 app/controllers/admin/categories_controller.rb
@@ -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 }
View
2  app/controllers/admin/jobs_controller.rb
@@ -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 }
View
5 app/controllers/application.rb
@@ -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?
View
2  app/helpers/admin/categories_helper.rb
@@ -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
View
4 app/helpers/application_helper.rb
@@ -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
5 app/models/category.rb
@@ -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
View
6 app/views/admin/categories/create.js.rjs
@@ -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
View
12 app/views/admin/categories/destroy.js.rjs
@@ -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
View
6 app/views/admin/categories/saveorder.js.rjs
@@ -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
View
6 app/views/admin/categories/update.js.rjs
@@ -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
View
6 app/views/admin/jobs/destroy.js.rjs
@@ -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
View
6 app/views/admin/jobs/update.js.rjs
@@ -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
View
4 app/views/layouts/_admin_flash_boxes.html.haml
@@ -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);"
View
6 app/views/layouts/_admin_header.html.haml
@@ -1,7 +1,7 @@
#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
@@ -9,8 +9,6 @@
%li= link_to "Change your password", "#"
= bull
%li= link_to "Logout", logout_path
-
- = partial "layouts/flash_boxes"
-
+
- unless logged_in?
#categs-nav
View
4 app/views/layouts/admin.html.haml
@@ -19,5 +19,5 @@
#content
= yield
- .footer
- #messagesContainer{:style => 'display:none'}
+ #footer
+ = render :partial => 'layouts/admin_flash_boxes'
View
1  app/views/sessions/_login_status.html.haml
@@ -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
View
16 public/stylesheets/styles.css
@@ -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
2  test/fixtures/admins.yml
@@ -4,7 +4,7 @@ bob:
mark:
login: mark
- password: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 # longtest
+ password: bf5a0cb861642b94cb8635dce9d197d375569e1f # longtest
admin:
login: admin
View
4 test/fixtures/job_types.yml
@@ -1,7 +1,7 @@
-full-time:
+fulltime:
name: Full-time
value: fulltime
-part-time:
+parttime:
name: Part-time
value: parttime
View
4 test/fixtures/jobs.yml
@@ -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
View
14 test/functional/admin/categories_controller_test.rb
@@ -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
View
99 test/integration/administrative_panel_test.rb
@@ -0,0 +1,99 @@
+require 'test_helper'
+
+class AdministrativePanelTest < ActionController::IntegrationTest
+
+ def test_login_logout
+ get '/admin/jobs'
+ assert_response :redirect
+
+ assert_equal 'Please log in.', flash[:notice]
+
+ post '/session', {:login => 'admin', :password => 'admin'}
+ assert_response :redirect
+
+ assert session[:admin]
+
+ post '/logout'
+ assert_response :redirect
+ assert_nil session[:admin]
+ end
+
+ def test_activate_and_deactivate_jobs
+ login_user
+
+ # activate deactive job
+ put "/admin/jobs/#{jobs(:two).id}"
+ jobs(:two).reload
+ assert jobs(:two).is_active
+
+ # deactivate active job
+ get "/admin/categories/#{jobs(:two).category}"
+ put "/admin/jobs/#{jobs(:two).id}"
+ jobs(:two).reload
+ assert_equal(false, jobs(:two).is_active)
+ assert_equal "Job has been activated/deactivated", flash[:notice]
+
+ logout_user
+ end
+
+ def test_delete_job
+ login_user
+
+ # inactive job
+ delete "/admin/jobs/#{jobs(:two).id}"
+ assert_raise(ActiveRecord::RecordNotFound) { jobs(:two).reload }
+
+ # active job
+ delete "/admin/jobs/#{jobs(:one).id}"
+ assert_raise(ActiveRecord::RecordNotFound) { jobs(:one).reload }
+
+ logout_user
+ end
+
+ def test_manage_categories
+ login_user
+
+ get '/admin/categories'
+ assert_response :success
+
+ count = Category.count
+
+ # add category
+ post '/admin/categories'
+ assert_response :redirect
+ assert_not_equal(count, Category.count)
+
+ # update category name
+ put "/admin/categories/#{categories(:designer).id}", :name => 'New name', :url => categories(:designer).value
+ categories(:designer).reload
+ assert_equal 'New name', categories(:designer).name
+
+ # sort categories
+ put '/admin/categories/saveorder', :categoriesContainer => [categories(:designer).id,categories(:administrator).id,categories(:programmer).id]
+ assert_equal [0,1,2], [categories(:designer).reload.position, categories(:administrator).reload.position,categories(:programmer).reload.position]
+
+ count = Category.count
+ # should not delete category with jobs
+ delete "/admin/categories/#{categories(:designer).id}"
+ assert_response :redirect
+ assert_equal count, Category.count
+
+ # should delete category without jobs
+ delete "/admin/categories/#{categories(:administrator).id}"
+ assert_response :redirect
+ assert_equal count - 1, Category.count
+
+ logout_user
+ end
+
+ private
+ def login_user
+ post '/session', {:login => 'admin', :password => 'admin'}
+ assert_response :redirect
+ end
+
+ def logout_user
+ post '/logout'
+ assert_response :redirect
+ end
+end
View
6 test/unit/admin_test.rb
@@ -1,10 +1,12 @@
require 'test_helper'
class AdminTest < ActiveSupport::TestCase
- self.use_instantiated_fixtures = true
def test_authentication
- assert_equal @mark, Admin.authenticate("mark", "longtest")
+
+ assert_not_nil Admin.authenticate("admin", "admin")
+
+ assert_equal admins(:mark), Admin.authenticate("mark", "longtest")
# wrong username
assert_nil Admin.authenticate("adminnn", "admin")
Please sign in to comment.
Something went wrong with that request. Please try again.