Permalink
Browse files

Added jobs management in admin panel and simple authentication system

  • Loading branch information...
1 parent df546a2 commit e8a40d474957cdb87eea95a2cffff0c1c07ce05d @wlodars wlodars committed Jul 4, 2008
Showing with 532 additions and 50 deletions.
  1. +10 −0 app/controllers/admin/categories_controller.rb
  2. +37 −0 app/controllers/admin/jobs_controller.rb
  3. +8 −1 app/controllers/application.rb
  4. +27 −0 app/controllers/sessions_controller.rb
  5. +5 −0 app/helpers/admin/categories_helper.rb
  6. +9 −0 app/helpers/admin/jobs_helper.rb
  7. +0 −1 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. +5 −0 app/views/admin/categories/_list.html.haml
  13. +3 −0 app/views/admin/categories/show.html.haml
  14. +24 −0 app/views/admin/jobs/_jobs_table.html.haml
  15. +1 −0 app/views/admin/jobs/destroy.js.rjs
  16. +2 −0 app/views/admin/jobs/index.html.haml
  17. +51 −0 app/views/admin/jobs/show.html.haml
  18. +1 −0 app/views/admin/jobs/update.js.rjs
  19. +16 −0 app/views/layouts/_admin_header.html.haml
  20. +19 −0 app/views/layouts/admin.html.haml
  21. +1 −1 app/views/layouts/application.html.haml
  22. 0 app/views/session/_login_status.html.haml
  23. +5 −0 app/views/sessions/_login_status.html.haml
  24. +12 −0 app/views/sessions/new.html.haml
  25. +11 −4 config/routes.rb
  26. +4 −0 db/fixtures/admins.rb
  27. +15 −0 db/migrate/20080703181307_create_admins.rb
  28. +8 −1 db/schema.rb
  29. BIN public/images/icon-delete.png
  30. BIN public/images/icon_accept.gif
  31. BIN public/images/icon_activate.gif
  32. +60 −0 public/stylesheets/styles.css
  33. +11 −0 test/fixtures/admins.yml
  34. +8 −6 test/fixtures/categories.yml
  35. +6 −6 test/fixtures/job_types.yml
  36. +22 −18 test/fixtures/jobs.yml
  37. +4 −6 test/fixtures/locations.yml
  38. +14 −0 test/functional/admin/categories_controller_test.rb
  39. +43 −0 test/functional/admin/jobs_controller_test.rb
  40. +5 −3 test/functional/jobs_controller_test.rb
  41. +30 −0 test/functional/sessions_controller_test.rb
  42. +3 −0 test/test_helper.rb
  43. +18 −0 test/unit/admin_test.rb
@@ -0,0 +1,10 @@
+class Admin::CategoriesController < ApplicationController
+ before_filter :login_required
+ layout 'admin'
+
+ # GET /admin/jobs/1
+ def show
+ @category = Category.find_by_value(params[:id])
+ @jobs = @category.jobs.find_all_by_is_active(true, :order => "created_at DESC")
+ end
+end
@@ -0,0 +1,37 @@
+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])
+
+ 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
+
+ respond_to do |format|
+ format.html { redirect_to admin_jobs_url }
+ format.js # admin/jobs/destroy.js.rjs
+ end
+ end
+end
@@ -13,7 +13,14 @@ 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
+
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!"
+ render :action => :new
+ end
+ end
+
+ def destroy
+ reset_session
+ flash[:notice] = "You've been logged out."
+ redirect_to login_url
+ end
+end
@@ -0,0 +1,5 @@
+module Admin::CategoriesHelper
+ def admin_category_nav_item(category)
+ category_nav_item(category, true)
+ end
+end
@@ -0,0 +1,9 @@
+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
+end
@@ -22,5 +22,4 @@ def error_message_for(record, attribute, error_msg = nil)
return content_tag(:div, error_msg || error_list.join(", "), :class => "error-message")
end
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.pluralize}</span><span class='cnr'>&nbsp;</span>", (admin ? admin_category_url(category) : category_url(category)), :title => category.name.pluralize
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
@@ -0,0 +1,5 @@
+#categories
+ %ul
+ - Category.list.each do |category|
+ = admin_category_nav_item(category)
+ = clear
@@ -0,0 +1,3 @@
+.job-listings
+ %h2= "Jobs for " + @category.name.pluralize
+ = partial "admin/jobs/jobs_table", :jobs => @jobs
@@ -0,0 +1,24 @@
+- unless jobs.empty?
+ -# Display Job Posts
+ %table.job-posts{:cellspacing => 0}
+ - jobs.each do |job|
+ %tr.job_item{:id => "job_" + job.id.to_s}
+ %td
+ = job_type_image(job.job_type)
+ = link_to job.title, admin_job_url(job), :title => job.title
+ %span.la at
+ = job_location_text(job)
+
+ -# display time
+ %td.time-posted
+ = image_tag "clock.gif", :class => "vert-middle"
+ = job.created_at.to_s(:job_listing)
+
+ %td{:style => 'text-align:left;width:15%'}
+ -# activate/deactivate link
+ = link_to_remote(activation_image_tag(job), :url => admin_job_url(job.id), :method => :put)
+ -# delete link
+ = 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)
+
+- else
+ #no-ads No jobs, yet.
@@ -0,0 +1 @@
+page["job_#{@job.id}"].visual_effect :fade, "job_#{@job.id}"
@@ -0,0 +1,2 @@
+%h2 Inactive jobs
+= partial "admin/jobs/jobs_table", :jobs => @jobs
@@ -0,0 +1,51 @@
+- if @job
+ = br
+ = link_to '« go back', admin_category_url(@job.category)
+ #job_details
+ -# display job outdated warning if applicable
+ - if @job.created_at < 40.days.ago
+ .old-ad
+ This job ad has been posted over 40 days ago...
+
+ -# show applicants count
+ .applied-to-job
+ = @job.job_applicants.size
+ %p
+ - if @job.job_applicants.size != 1
+ applicants
+ - else
+ applicant
+
+ -# show job title
+ %h2
+ = job_type_image(@job.job_type)
+ = @job.title
+
+ -# show job location
+ %p
+ %span.fading at
+ - unless @job.url.blank? or @job.url == "http://"
+ = link_to @job.company, @job.url
+ - else
+ %strong= @job.company
+
+ - if @job.located_at == "Anywhere"
+ %strong= "(" + @job.located_at + ")"
+ - else
+ %span.fading in
+ %strong= @job.located_at
+
+ -# show job description
+ .job-description
+ = @job.description_html
+ = br
+
+ #job_bottom
+ #number_views
+ Published at:
+ %strong= @job.created_at.to_s(:post_listing_date)
+ = br
+ Viewed:
+ %strong= pluralize(@job.view_count, "time")
+ = clear
+
@@ -0,0 +1 @@
+page.replace "activate_#{@job.id}", activation_image_tag(@job)
@@ -0,0 +1,16 @@
+#header
+ %h1#logo= link_to AppConfig.site_name, root_path
+ - if logged_in?
+ %ul#top
+ %li= link_to "Pages", "#"
+ = bull
+ %li= link_to "Categories", "#"
+ = bull
+ %li= link_to "Change your password", "#"
+ = bull
+ %li= link_to "Logout", logout_path
+
+ = partial "layouts/flash_boxes"
+
+- unless logged_in?
+ #categs-nav
@@ -0,0 +1,19 @@
+!!! Strict
+%html{html_attrs}
+ %head
+ %meta{"http-equiv" => "Content-type", "content" => "text/html; charset=utf-8"}
+
+ = title :site => AppConfig.site_name
+ = stylesheets
+ = javascripts
+
+ = feed_links
+
+ %body
+ #container
+ = partial "layouts/admin_header"
+ - if logged_in?
+ = partial "admin/categories/list"
+
+ #content
+ = yield
@@ -11,7 +11,7 @@
%body
#container
- = partial "session/login_status"
+ = partial "sessions/login_status"
= partial "layouts/header"
= partial "search/searchbox"
= partial "categories/list"
@@ -0,0 +1,5 @@
+.login_status
+ - if logged_in?
+ = "You are logged as #{current_user.login} " + link_to('Logout', logout_path)
+ - else
+ = link_to 'Login', login_path
@@ -0,0 +1,12 @@
+- form_tag session_path, :id => 'publish_form' do
+ %fieldset
+ %legend Login
+ %table{:border => 0, :cellspacing => 2, :cellpadding => 2}
+ %tr
+ %td.publish-label Username:
+ %td= text_field_tag :login, params[:login], :size => 40
+ %tr
+ %td.publish-label Password:
+ %td= password_field_tag :password, params[:password], :size => 40
+ %fieldset
+ = submit_tag 'Login', :id => 'submit'
View
@@ -1,5 +1,9 @@
ActionController::Routing::Routes.draw do |map|
+ map.login '/login', :controller => 'sessions', :action => 'new'
+ map.logout '/logout', :controller => 'sessions', :action => 'destroy'
+ map.resource :session
+
map.resources :jobs, :member => {
:verify => :any,
:apply => :post,
@@ -11,10 +15,13 @@
map.resources :categories
map.resource :search, :controller => "Search"
- # map.namespace :admin do |admin|
- # # Directs /admin/jobs/* to Admin::JobsController (app/controllers/admin/jobs_controller.rb)
- # admin.resources :jobs
- # end
+ map.admin '/admin', :controller => 'admin/jobs', :action => 'index'
+ map.namespace :admin do |admin|
+ # Directs /admin/jobs/* to Admin::JobsController (app/controllers/admin/jobs_controller.rb)
+ admin.resources :jobs
+ # Directs /admin/categories/* to Admin::CategoriesController (app/controllers/admin/categories_controller.rb)
+ admin.resources :categories
+ end
map.root :controller => "jobs"
@@ -0,0 +1,4 @@
+Admin.seed(:login, :password) do |s|
+ s.login = 'admin'
+ s.password = 'd033e22ae348aeb5660fc2140aec35850c4da997' # admin
+end
@@ -0,0 +1,15 @@
+class CreateAdmins < ActiveRecord::Migration
+ def self.up
+ create_table :admins do |t|
+ t.string :login
+ t.string :password
+
+ t.timestamps
+ end
+
+ end
+
+ def self.down
+ drop_table :admins
+ end
+end
Oops, something went wrong.

0 comments on commit e8a40d4

Please sign in to comment.