SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 376 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"

The file app/
controllers/application.rb should look like this:
class ApplicationController < ActionController::Base
session :session_key => '_liblib_session_id'
before_filter :try_borrower
protected
def authenticate_librarian
unless session[:librarian_id] &&
(@a_librarian = Librarian.find_by_id(session[:librarian_id]))
redirect_to :controller=>'auth',
:action=>'llogin', :into=>url_for(params)
end
end
def try_librarian
@a_librarian = Librarian.find_by_id(
session[:librarian_id]) if session[:librarian_id]
end
def try_borrower
@a_borrower = Borrower.find_by_id(
session[:borrower_id]) if session[:borrower_id]
end
end
You have three different filters and one before_filter declaration. The before_filter
invokes try_borrower for all pages. You don??™t do the same thing for try_librarian, because
there are a few cases where you don??™t want to do try_librarian (specifically those cases where
a Librarian needs to be logged in). The method authenticate_librarian will be used from
several controllers, so it makes sense to share the implementation. The actual implementation
is the same thing you??™ve seen before; it uses the session value librarian_id??”if it exists??”to
find the Librarian. Otherwise it redirects to the llogin method (standing for librarian login,
as compared to blogin) on the Auth controller.
CHAPTER 14 ?–  THE LIBLIB RAILS APPLICATION 261
The two try filters check if the session contains a librarian_id and a borrower_id, and try
to find them from the database in that case.


Pages:
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388