# * George Moschovitis # (c) 2004-2005 Navel, all rights reserved. # $Id$ require 'nitro/controller' require 'nitro/ui/pager' require 'lib/blog/model' # The controller of the Blog part. class BlogController < N::Controller before_filter :get_errors after_filter :cache scaffold BlogEntry, :name => 'entry', :index => true # , :nosuffix => true scaffold Comment def list_entry @pager = UI::Pager.new('entries', request, 3) @entries = BlogEntry.all("ORDER BY oid DESC #{@pager.sql_limit}") @pager.set(BlogEntry.count) end def new_entry entry = request.fill(BlogEntry.new) entry.author = session[:username] unless entry.valid? session[:errors] = entry.errors redirect_referer '#new_entry' end entry.save! end # An example route. # Example of action with parameter. The parameter # request['oid'] is referenced by the oid method # parameter. This also demonstrates implicit routing. def myview entry = BlogEntry[@oid] @out << entry.title end action :myview, :route => /view\/(.*)/, 'oid' => 1 # example of generated view def list_entry__xml o.build_rss(@entries, :description => 'Blog entries', :link => context.host_url) end def new_comment comment = request.fill(Comment.new) unless comment.valid? session[:errors] = comment.errors redirect_referer '#new_comment' end comment.save! end # example of generated view def list_comment__xml @out.build_rss(@comments, :description => 'Blog comments', :link => context.host_url) end def login if password = request['password'] if password == Blog.password session[:owner] = true session[:username] = Blog.username redirect '/' else @error = 'Invalid password' end end end def logout session.delete(:owner) session.delete(:username) end private # A helper prefilter, autocleans session errors. # Used as an example. def get_errors # gmosx: should better implement top-level filters! if errors = session.delete(:errors) @errors = errors end end # Just an example. def cache Logger.info 'cache (after filter example)' end end