require 'nitro/controller' require 'nitro/ui/pager' require 'nitro/localization' require 'mailer' require 'models/blog' # The controller of the Blog part. class BlogController < Nitro::Controller pre :get_errors post :cache_invalidate, :on => [ :new_entry, :new_comment, :del_entry, :del_comment ] wrap Nitro::LocalizationAspect, :pre => :localize # generate default BlogEntry related actions. scaffold BlogEntry, :name => 'entry', :index => true # , :nosuffix => true # generate default Comment related actions. scaffold Comment # as an example of full page caching. # cache_output :index # Override the template root for this controller. @template_root = 'src/views' def list_entry @pager = UI::Pager.new('entries', request, 3) @entries = BlogEntry.all(:order => 'oid DESC', :extra => @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 def mail_entry entry = BlogEntry[request['oid']] BlogMailer.deliver_blog_entry_email(request['to'], entry) end def new_comment comment = request.fill(Comment.new) unless comment.valid? session[:errors] = comment.errors redirect_referer '#new_comment' end comment.save! end def login if password = request['password'] if password == Blog.password session[:owner] = true session[:username] = Blog.username request.add_cookie 'nauth', 'just an example, not used :)' redirect '/' else @error = 'Invalid password' end end end def logout session.delete(:owner) session.delete(:username) end def toggle_locale cache_invalidate if session[:LOCALE] == :de session[:LOCALE] = :en else session[:LOCALE] = :de end 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 # Invalidates the cache. def cache_invalidate # gmosx: will be improved. expire_fragment('entriesadmin') expire_fragment('entriesadmintrue') end end # A special controller for that presents # the blog content using XML (RSS). This controller # reuses the BlogController actions but adds # XML templates. class XmlController < BlogController @base = 'xml' @content_type = 'text/xml' @template_root = 'src/views/xml' # This is the prefered way to add programmatically # generated output. The method list_entry_template is # called after list_entry. def list_entry_template o.build_rss(@entries, :description => 'Blog entries', :link => context.host_url) end # Another way to overload the method for RSS output. # Not that flexible though. def list_comment super o.build_rss(@comments, :description => 'Blog comments', :link => context.host_url) end end