From ea72da5259a1d113e3c69c72b822e97baf604f46 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Thu, 13 Feb 2014 22:38:42 +0100 Subject: [PATCH] rework post controller To handle errors correctly the post controller needed some rework. Now instead of single functions running all around the place, there is one function collecting the data needed and raising errors when needed. For that to work, the controller class can now return more classes to keep the chain going. Unless a class is returned, it will call the renderer. --- controller/post.rb | 32 +++++++++++++++----------------- lib/controller.rb | 3 ++- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/controller/post.rb b/controller/post.rb index 755f194..55c68a3 100644 --- a/controller/post.rb +++ b/controller/post.rb @@ -10,33 +10,31 @@ module Routes SQL def self.get(session) - define_posts(session) + posts = define_posts_query(session) + if session.options[:id] + posts = posts.filter(:posts__id => session.options[:id].to_i) + return RouteNotFound if posts.empty? + set_previous_and_next_post(session, posts) + else + if session.request.params['search'] + posts = load_fulltextsearch(session, posts) + end + set_page_information(session, posts) + end + session.options[:posts] = posts session.options[:render] = 'posts/index' end - def self.define_posts(session) + def self.define_posts_query(session) posts = DB[:posts]. filter(:released => true). select(:posts__id___post_id, :written, :title, :content, :username). join(:accounts, :id___account_id => :account_id). reverse_order(:written, :posts__id) - - if session.options[:id] - posts = posts.filter(:posts__id => session.options[:id].to_i) - load_previous_and_next_post(session, posts) unless posts.empty? - else - if session.request.params['search'] - posts = load_fulltextsearch(session, posts) - end - load_page_information(session, posts) - # return no_results if posts.empty? - end - - session.options[:posts] = posts end # load posts depending on the pagination - def self.load_page_information(session, posts) + def self.set_page_information(session, posts) # compute pages page = session.request.params['page'].to_i session.options[:page] = page if page @@ -47,7 +45,7 @@ SQL end # load a single posts and the ids of the next and previous posts - def self.load_previous_and_next_post(session, posts) + def self.set_previous_and_next_post(session, posts) written = posts.first[:written] session.options[:post_ids_pn] = DB[PREV_AND_NEXT_QUERY, written, written].first end diff --git a/lib/controller.rb b/lib/controller.rb index 9d742b0..8f9dc15 100644 --- a/lib/controller.rb +++ b/lib/controller.rb @@ -5,7 +5,8 @@ class Controller end def self.call_method(session) - send(session.request.method, session) + result = send(session.request.method, session) + return result if result.kind_of?(Class) session.options[:renderer] end end