diff options
author | re4k <re4k@re4k.info> | 2013-04-28 00:38:21 +0900 |
---|---|---|
committer | re4k <re4k@re4k.info> | 2013-04-28 00:38:21 +0900 |
commit | 4af8acda5e4664d41079e3ff2ea853e1fb74a2a8 (patch) | |
tree | 51a583486af42c349d1d0ff8cedfb40b8265fb5b /app/controllers/application_controller.rb | |
parent | 4189cf84b0c337568133c6dafadc07669daaa868 (diff) | |
download | aclog-4af8acda5e4664d41079e3ff2ea853e1fb74a2a8.tar.gz |
refactor controller
Diffstat (limited to 'app/controllers/application_controller.rb')
-rw-r--r-- | app/controllers/application_controller.rb | 54 |
1 files changed, 20 insertions, 34 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3a727b5..249b4ea 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,32 +1,32 @@ # -*- coding: utf-8 -*- class ApplicationController < ActionController::Base protect_from_forgery - before_filter :set_format after_filter :xhtml - helper_method :logged_in?, :page - helper_method :get_bool, :get_int - - def render_timeline(a = nil, &blk) - @items = a || blk.call - - @items = @items.where("tweets.id <= ?", max_id) if max_id - @items = @items.where("tweets.id > ?", since_id) if since_id - - @items = @items.limit(count) - @items = @items.offset(((page || 1) - 1) * count) if page + def render_tweets(options = {}, &blk) + if params[:count] + count = params[:count].to_i + else + count = 10 + end + p options + if options[:force_page] + params[:page] ||= "1" + end + + @items = blk.call.limit(count) + + if params[:page] + @items = @items.page(params[:page].to_i, count) + else + @items = @items.max_id(params[:max_id].to_i) if params[:max_id] + @items = @items.since_id(params[:since_id].to_i) if params[:since_id] + end + render "shared/tweets" end - def logged_in?; session[:user_id] != nil end - - # params - def page; get_int(params[:page], nil){|i| i > 0} end - def count; get_int(params[:count], 10){|i| (1..100) === i} end - def max_id; get_int(params[:max_id], nil){|i| i >= 0} end - def since_id; get_int(params[:since_id], nil){|i| i >= 0} end - private def set_format unless [:json, :html].include?(request.format.to_sym) @@ -42,18 +42,4 @@ class ApplicationController < ActionController::Base response.body = response.body.gsub(/[\x0-\x8\xb\xc\xe-\x1f]/, "") end end - - def get_bool(str) - /^(t|true|1)$/ =~ str - end - - def get_int(str, default = 0, &blk) - if str =~ /^[1-9]\d*$/ - i = str.to_i - if !block_given? || blk.call(i) - return i - end - end - default - end end |