Skip to content

Instantly share code, notes, and snippets.

@goganchic
Created August 9, 2011 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goganchic/1134385 to your computer and use it in GitHub Desktop.
Save goganchic/1134385 to your computer and use it in GitHub Desktop.
Caching
def require_user
return true if logged_in?
reauthenticate
return false
end
def reauthenticate
cookies[:return_to] = request.fullpath
@user = User.new
cache_path = ActionController::Caching::Actions::ActionCachePath.new(self, :controller => 'users', :action => 'new')
body = read_fragment(cache_path.path, @store_options)
if body
render :text => body, :layout => false
else
render "users/new", :layout => 'application'
_save_fragment(cache_path.path, {})
end
end
class HomeController < ApplicationController
before_filter :require_user, :only => [:index, :blank]
before_filter :init_unread_counts, :only => [:index]
before_filter :init_stats, :only => [:index]
layout 'authorized'
caches_page :blank
caches_page :winners
caches_page :error_404
def self.cache_and_render_html_without_layout(*actions)
Array(actions).each do |action|
caches_page action
define_method action do
render :action => "#{action}.html", :layout => false
end
end
end
cache_and_render_html_without_layout :rules, :agreement, :game_info
def index
@comment = Comment.new
@comments = Comment.root.recent.all
@more_comments = @comments.last != Comment.root.last
@posts = Post.recent
@should_cache_posts = !@posts.first.try(:unread?, @last_post_shown)
current_user.last_comment_shown = @comments.first.id if @comments.present?
current_user.last_post_shown = @posts.first.id if @posts.present?
current_user.save if current_user.changed?
end
def blank
render :layout => 'application'
end
def error_404
render_404
end
private
def init_unread_counts
@unread_posts_count = Post.unread(current_user.last_post_shown).count
@unread_comments_count = Comment.root.unread(current_user.last_comment_shown).count
end
def init_stats
@best_time = format_time(LevelUser.best_results(current_user).first.try(:level_time))
@total_games = Game.active_and_finished.count
@user_games = GameUser.where(:user_id => current_user.id).count
@levels_stats = LevelUser.levels_stats_for(current_user)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment