Permalink
Browse files

Merge branch 'master' of git://github.com/wlodars/jobberrails into wl…

…oards/master
  • Loading branch information...
2 parents 8df1d94 + 66cab3a commit 04221122097871b454bb0a423f6d57b114316389 Ivan Acosta-Rubio committed Jul 11, 2008
Showing with 991 additions and 49 deletions.
  1. +78 −0 app/controllers/admin/categories_controller.rb
  2. +39 −0 app/controllers/admin/jobs_controller.rb
  3. +12 −0 app/controllers/application.rb
  4. +27 −0 app/controllers/sessions_controller.rb
  5. +41 −0 app/helpers/admin/categories_helper.rb
  6. +33 −0 app/helpers/admin/jobs_helper.rb
  7. +3 −0 app/helpers/application_helper.rb
  8. +2 −2 app/helpers/categories_helper.rb
  9. +1 −1 app/helpers/jobs_helper.rb
  10. +11 −0 app/helpers/sessions_helper.rb
  11. +20 −0 app/models/admin.rb
  12. +4 −1 app/models/category.rb
  13. +14 −0 app/views/admin/categories/_category.html.haml
  14. +4 −0 app/views/admin/categories/_list.html.haml
  15. +6 −0 app/views/admin/categories/create.js.rjs
  16. +10 −0 app/views/admin/categories/destroy.js.rjs
  17. +11 −0 app/views/admin/categories/index.html.haml
  18. +2 −0 app/views/admin/categories/saveorder.js.rjs
  19. +3 −0 app/views/admin/categories/show.html.haml
  20. +5 −0 app/views/admin/categories/update.js.rjs
  21. +25 −0 app/views/admin/jobs/_jobs_table.html.haml
  22. +2 −0 app/views/admin/jobs/destroy.js.rjs
  23. +2 −0 app/views/admin/jobs/index.html.haml
  24. +51 −0 app/views/admin/jobs/show.html.haml
  25. +2 −0 app/views/admin/jobs/update.js.rjs
  26. +4 −0 app/views/layouts/_admin_flash_boxes.html.haml
  27. +14 −0 app/views/layouts/_admin_header.html.haml
  28. +23 −0 app/views/layouts/admin.html.haml
  29. +1 −1 app/views/layouts/application.html.haml
  30. 0 app/views/session/_login_status.html.haml
  31. +6 −0 app/views/sessions/_login_status.html.haml
  32. +12 −0 app/views/sessions/new.html.haml
  33. +11 −4 config/routes.rb
  34. +4 −0 db/fixtures/admins.rb
  35. +15 −0 db/migrate/20080703181307_create_admins.rb
  36. +8 −1 db/schema.rb
  37. BIN public/images/icon-delete.png
  38. BIN public/images/icon_accept.gif
  39. BIN public/images/icon_activate.gif
  40. +159 −0 public/stylesheets/styles.css
  41. +11 −0 test/fixtures/admins.yml
  42. +13 −6 test/fixtures/categories.yml
  43. +6 −6 test/fixtures/job_types.yml
  44. +22 −18 test/fixtures/jobs.yml
  45. +4 −6 test/fixtures/locations.yml
  46. +70 −0 test/functional/admin/categories_controller_test.rb
  47. +43 −0 test/functional/admin/jobs_controller_test.rb
  48. +5 −3 test/functional/jobs_controller_test.rb
  49. +30 −0 test/functional/sessions_controller_test.rb
  50. +99 −0 test/integration/administrative_panel_test.rb
  51. +3 −0 test/test_helper.rb
  52. +20 −0 test/unit/admin_test.rb
@@ -0,0 +1,78 @@
+class Admin::CategoriesController < ApplicationController
+ before_filter :login_required
+ layout 'admin'
+
+ # GET /admin/categories
+ def index
+ @categories = Category.list
+ end
+
+ # GET /admin/categories/1
+ def show
+ @category = Category.find_by_value(params[:id])
+ @jobs = @category.jobs.find_all_by_is_active(true, :order => "created_at DESC")
+ end
+
+ # POST /admin/categories
+ def create
+ 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 }
+ format.js # admin/categories/create.js.rjs
+ end
+ end
+
+ # 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
+
+ # PUT /admin/categories/saveorder
+ def saveorder
+ params[:categoriesContainer].each_with_index do |id, position|
+ 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 }
+ format.js # admin/categories/saveorder.js.rjs
+ end
+ end
+
+ # DELETE /admin/categories/1
+ 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 }
+ format.js # admin/categories/destroy.js.rjs
+ end
+ end
+end
@@ -0,0 +1,39 @@
+class Admin::JobsController < ApplicationController
+ before_filter :login_required
+ layout 'admin'
+
+ # GET /admin/jobs
+ def index
+ @jobs = Job.find_all_by_is_active(false, :order => 'updated_at DESC')
+ end
+
+ # GET /admin/jobs/1
+ def show
+ @job = Job.find(params[:id])
+ end
+
+ # 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)
+ format.html { redirect_to admin_jobs_url }
+ format.js # admin/jobs/update.js.rjs
+ end
+ end
+ end
+
+ # DELETE /admin/jobs/1
+ 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 }
+ format.js # admin/jobs/destroy.js.rjs
+ end
+ end
+end
@@ -13,7 +13,19 @@ class ApplicationController < ActionController::Base
# Uncomment this to filter the contents of submitted sensitive data parameters
# from your application log (in this case, all fields with names like "password").
filter_parameter_logging :password
+
+ def login_required
+ unless session[:admin]
+ flash[:notice] = "Please log in."
+ redirect_to login_url
+ end
+ end
+ def flash_notice(string)
+ flash[:notice] = string
+ flash.discard(:notice)
+ end
+
protected
def production?
ENV["RAILS_ENV"] == "production"
@@ -0,0 +1,27 @@
+class SessionsController < ApplicationController
+ layout 'admin'
+
+ def new
+ if session[:admin]
+ redirect_to admin_url
+ end
+ end
+
+ def create
+ admin = Admin.authenticate(params[:login], params[:password])
+ if admin
+ session[:admin] = admin
+ flash[:notice] = "You are logged as #{admin.login}"
+ redirect_to admin_url
+ else
+ flash[:error] = "Invalid login or password!"
+ redirect_to login_url
+ end
+ end
+
+ def destroy
+ reset_session
+ flash[:notice] = "You've been logged out."
+ redirect_to login_url
+ end
+end
@@ -0,0 +1,41 @@
+module Admin::CategoriesHelper
+ def admin_category_nav_item(category)
+ category_nav_item(category, true)
+ end
+
+ def show_save_category(category_id)
+ "Element.show('saveCategory#{category_id}')"
+ end
+
+ def link_to_delete_category(category_id)
+ link_to_remote(image_tag("icon-delete.png", :alt => 'Delete') + " Delete", {
+ :url => admin_category_url(category_id),
+ :confirm => 'Are you sure you want to delete this category?',
+ :method => :delete },
+ { :class => 'deleteCategory'})
+ end
+
+ def link_to_save_category(category_id)
+ link_to_remote(image_tag("disk.png", :alt => 'Save') + " Save", {
+ :url => admin_category_url(category_id),
+ :method => :put,
+ :with => "'name='+$F('name[#{category_id}]')+'&url='+$F('url[#{category_id}]')" },
+ { :id => "saveCategory#{category_id}",
+ :class => 'saveCategory', :style => 'display:none;' })
+ end
+
+ def sortable_element_categories_container
+ sortable_element "categoriesContainer",
+ :url => saveorder_admin_categories_path,
+ :tag => "div",
+ :complete => visual_effect(:highlight, 'categoriesContainer')
+ end
+
+ def link_to_add_category
+ link_to_remote image_tag("add.png", :alt => 'Add') + " Add new category",
+ :url => admin_categories_path,
+ :method => :post,
+ :loading => "Element.show('overlay')",
+ :complete => "Element.hide('overlay');"
+ end
+end
@@ -0,0 +1,33 @@
+module Admin::JobsHelper
+ def activation_image_tag(job)
+ activate = "activate"
+ unless job.is_active
+ activate = "de" + activate
+ end
+ image_tag("icon_#{activate}.gif",
+ :alt => activate.capitalize,
+ :id => "activate_#{job.id}")
+ end
+
+ def link_to_activate_deactivate_job(job)
+ link_to_remote(activation_image_tag(job), {
+ :url => admin_job_url(job.id),
+ :method => :put,
+ :loading => "Element.hide('activ_butt_#{job.id}');Element.show('loading_#{job.id}')",
+ :complete => "Element.hide('loading_#{job.id}');Element.show('activ_butt_#{job.id}')"},
+ { :id => "activ_butt_#{job.id}"} )
+ end
+
+ def link_to_delete_job(job_id)
+ link_to_remote(image_tag("icon-delete.png", :alt => 'Delete'),
+ :url => admin_job_url(job_id),
+ :confirm => 'Are you sure you want to delete this post?',
+ :method => :delete)
+ end
+
+ def saving_job_image_tag(job_id)
+ image_tag("ajax-loader.gif", :alt => "Saving...",
+ :style => 'display:none',
+ :id => "loading_#{job_id}")
+ end
+end
@@ -23,4 +23,7 @@ def error_message_for(record, attribute, error_msg = nil)
end
end
+ def display_notice
+ page.insert_html :after, 'footer', :partial => 'layouts/admin_flash_boxes'
+ end
end
@@ -1,5 +1,5 @@
module CategoriesHelper
- def category_nav_item(category)
+ def category_nav_item(category, admin = false)
li_options = {}
@@ -8,7 +8,7 @@ def category_nav_item(category)
li_options[:class] = "selected"
end
- link = link_to "<span>#{category.name}</span><span class='cnr'>&nbsp;</span>", category_url(category), :title => category.name
+ link = link_to "<span>#{category.name}</span><span class='cnr'>&nbsp;</span>", (admin ? admin_category_url(category) : category_url(category)), :title => category.name
return content_tag(:li, link, li_options)
end
@@ -17,7 +17,7 @@ def job_location_text(job)
puts ", #{job.location}"
elsif !job.location.blank?
haml_tag :span, "in", :class => "la"
- puts " #{job.location}"
+ puts " #{job.location.name}"
end
end
end
@@ -0,0 +1,11 @@
+module SessionsHelper
+ # checks if user is logged in
+ def logged_in?
+ session[:admin] ? true : false
+ end
+
+ # returns currently logged admin
+ def current_user
+ session[:admin]
+ end
+end
View
@@ -0,0 +1,20 @@
+require 'digest/sha1'
+
+class Admin < ActiveRecord::Base
+ validates_length_of :login, :within => 3..40
+ validates_length_of :password, :within => 5..40
+ validates_presence_of :login, :password
+ validates_uniqueness_of :login
+
+ attr_protected :id
+
+ def self.authenticate(login, pass)
+ a = find_by_login(login)
+ a && Admin.encrypt(pass) == a.password ? a : nil
+ end
+
+ protected
+ def self.encrypt(pass)
+ Digest::SHA1.hexdigest(pass)
+ 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
@@ -0,0 +1,14 @@
+.categoryItem{:id => "category_#{category.id}"}
+ .categoryHandle
+ .categoryWrapper
+ .categoryManagement{:style => 'float:right;width:10%;'}
+ = link_to_delete_category(category.id)
+ = link_to_save_category(category.id)
+ %label
+ %span Name:
+ = text_field_tag "name[#{category.id}]", category.name, :size => 60
+ = observe_field "name[#{category.id}]", :frequency => 1, :function => show_save_category(category.id)
+ %label
+ %span URL:
+ = text_field_tag "url[#{category.id}]", category.value, :size => 60
+ = observe_field "url[#{category.id}]", :frequency => 1, :function => show_save_category(category.id)
@@ -0,0 +1,4 @@
+%ul
+ - Category.list.each do |category|
+ = admin_category_nav_item(category)
+= clear
@@ -0,0 +1,6 @@
+page.insert_html :bottom, 'categoriesContainer', :partial => 'admin/categories/category'
+page.sortable 'categoriesContainer', :tag => 'div', :url => saveorder_admin_categories_path,
+ :complete => visual_effect(:highlight, 'categoriesContainer')
+@category = nil
+page.replace_html "categories", :partial => 'admin/categories/list'
+page.display_notice
@@ -0,0 +1,10 @@
+if @category.jobs.empty?
+ 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
@@ -0,0 +1,11 @@
+%h2 Categories
+
+#categoriesContainer
+ = partial @categories
+
+= sortable_element_categories_container
+
+#overlay{:style => "display: none; top: 145px; left: 157px;height: 72px;"}
+ = image_tag("ajax-loader.gif", :alt => "Loading...")
+%p
+ = link_to_add_category
@@ -0,0 +1,2 @@
+page.replace_html "categories", :partial => 'admin/categories/list'
+page.display_notice
@@ -0,0 +1,3 @@
+.job-listings
+ %h2= "Jobs for " + @category.name.pluralize
+ = partial "admin/jobs/jobs_table", :jobs => @jobs
@@ -0,0 +1,5 @@
+page["category_#{@category.id}"].visual_effect :pulsate, "category_#{@category.id}"
+page.hide "saveCategory#{@category.id}"
+@category = nil
+page.replace_html "categories", :partial => 'admin/categories/list'
+page.display_notice
Oops, something went wrong.

0 comments on commit 0422112

Please sign in to comment.