Skip to content

Commit

Permalink
cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
tpinto committed Jul 6, 2008
1 parent bbb3483 commit 2a9e64f
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 63 deletions.
2 changes: 2 additions & 0 deletions app/controllers/account_controller.rb
@@ -1,5 +1,7 @@
class AccountController < ApplicationController
before_filter :login_required, :except => [:login, :logout]

cache_sweeper :user_sweeper, :only => [:details,:profile]

def profile
@user = self.current_user if logged_in?
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/admin_controller.rb
Expand Up @@ -3,6 +3,8 @@ class AdminController < ApplicationController
before_filter :signup_first_user, :except => [:signup]
before_filter :no_more_than_one_signup, :only => [:signup]

cache_sweeper :post_sweeper, :only => [:new_post]

def signup
@admin = Admin.new

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/comments_controller.rb
@@ -1,6 +1,8 @@
class CommentsController < ApplicationController
before_filter :login_required

cache_sweeper :comment_sweeper, :only => [:create]

def create
@comment = Comment.new(params[:comment])
@comment.talk_id = params[:id]
Expand Down
21 changes: 15 additions & 6 deletions app/controllers/events_controller.rb
@@ -1,18 +1,27 @@
class EventsController < ApplicationController
caches_page :feed

def index
@users = User.find :all, :order => "id DESC", :limit => 10
@user_count = User.count
@talks = Talk.find :all, :order => "id DESC", :limit => 5
@talk_count = Talk.count
@posts = Post.find :all, :order => "id DESC"
unless fragment_exist?("home_users")
@users = User.find :all, :order => "id DESC", :limit => 10
@user_count = User.count
end

unless fragment_exist?("home_talks")
@talks = Talk.find :all, :order => "id DESC", :limit => 5
@talk_count = Talk.count
end

unless fragment_exist?("home_intro")
@posts = Post.find :all, :order => "id DESC"
end
end

def feed
it = []
it << Post.find(:all, :limit => 20)
it << Talk.find(:all, :limit => 20)
it << Comment.find(:all, :limit => 20)
it << Comment.find(:all, :limit => 20, :include => [:talk])

it.flatten!
it.sort!{|a,b| a.created_at <=> b.created_at}
Expand Down
11 changes: 3 additions & 8 deletions app/controllers/talks_controller.rb
@@ -1,5 +1,7 @@
class TalksController < ApplicationController
before_filter :login_required, :except => [:show, :index]

cache_sweeper :talk_sweeper, :only => [:create]

def create
@talk = Talk.new(params[:talk])
Expand All @@ -11,6 +13,7 @@ def create
@talk.save!

flash[:message] = "Talk adicionada :)"

redirect_to root_path

rescue ActiveRecord::RecordInvalid
Expand All @@ -33,12 +36,4 @@ def show
def new
render :layout => "textile_help"
end

def destroy
@talk = Talk.find_by_id_and_user_id(params[:id],current_user.id)
@talk.destroy

flash[:message] = "Talk apagada :)"
redirect_to root_path
end
end
7 changes: 6 additions & 1 deletion app/controllers/users_controller.rb
@@ -1,6 +1,8 @@
class UsersController < ApplicationController
before_filter :login_required, :only => [:show]

cache_sweeper :user_sweeper, :only => [:create]

# shows the signup form
def new
end
Expand All @@ -10,7 +12,10 @@ def show
end

def index
@users = User.find :all, :order => "id DESC"
unless fragment_exist?("users_page")
@users = User.find :all, :order => "id DESC"
@openid_count = User.count :conditions => "identity_url is not null"
end
end

# receives the new user form post
Expand Down
15 changes: 15 additions & 0 deletions app/sweepers/comment_sweeper.rb
@@ -0,0 +1,15 @@
class CommentSweeper < ActionController::Caching::Sweeper
observe Comment

def after_create(comment)
expire_page("/feed.xml")
end

def after_destroy(comment)
expire_page("/feed.xml")
end

def after_update(comment)
#expire_page("/feed.xml")
end
end
18 changes: 18 additions & 0 deletions app/sweepers/post_sweeper.rb
@@ -0,0 +1,18 @@
class PostSweeper < ActionController::Caching::Sweeper
observe Post

def after_create(post)
expire_page("/feed.xml")
expire_fragment("home_intro")
end

def after_destroy(post)
expire_page("/feed.xml")
expire_fragment("home_intro")
end

def after_update(post)
expire_page("/feed.xml")
expire_fragment("home_intro")
end
end
18 changes: 18 additions & 0 deletions app/sweepers/talk_sweeper.rb
@@ -0,0 +1,18 @@
class TalkSweeper < ActionController::Caching::Sweeper
observe Talk

def after_create(talk)
expire_page("/feed.xml")
expire_fragment("home_talks")
end

def after_destroy(talk)
expire_page("/feed.xml")
expire_fragment("home_talks")
end

def after_update(talk)
expire_page("/feed.xml")
expire_fragment("home_talks")
end
end
20 changes: 20 additions & 0 deletions app/sweepers/user_sweeper.rb
@@ -0,0 +1,20 @@
class UserSweeper < ActionController::Caching::Sweeper
observe User

def after_create(user)
#expire_page("/index.xml")
expire_fragment("home_users")
expire_fragment("users_page")
end

def after_destroy(user)
#expire_page("/index.xml")
expire_fragment("home_users")
expire_fragment("users_page")
end

def after_update(talk)
#expire_page("/index.xml")
expire_fragment("home_users")
end
end
8 changes: 4 additions & 4 deletions app/views/events/feed.xml.builder
Expand Up @@ -12,18 +12,18 @@ xml.rss "version" => "2.0" do
when "Comment"
xml.title "Comentário à sessão '#{item.talk.title}'"
xml.link "http://barcamp.webreakstuff.com/talks/#{item.talk_id}"
xml.description item.body
xml.description item.body_html
xml.guid "#{item.class.to_s.downcase}-#{item.id}-#{item.created_at.strftime("%d%m%Y%H%M%S")}"
when "Talk"
xml.title "Nova sessão proposta '#{item.title}'"
xml.link "http://barcamp.webreakstuff.com/talks/#{item.id}"
xml.description item.description
xml.description item.description_html
xml.guid "#{item.class.to_s.downcase}-#{item.id}-#{item.created_at.strftime("%d%m%Y%H%M%S")}"
when "Post"
xml.title "Novidades! - #{item.title}"
xml.link "http://barcamp.webreakstuff.com/"
xml.description item.body
xml.guid "#{item.class.to_s.downcase}-#{item.id}-#{item.updated_at.strftime("%d%m%Y%H%M%S")}"
xml.description item.body_html
xml.guid "#{item.class.to_s.downcase}-#{item.id}-#{item.created_at.strftime("%d%m%Y%H%M%S")}"
end
xml.pubDate(item.updated_at)
end
Expand Down
76 changes: 41 additions & 35 deletions app/views/events/index.html.erb
@@ -1,41 +1,47 @@
<% content_for :intro do %>
<div class="yui-b first" id="intro">
<h1><img src="/images/mail-24.png" class="icon"> O que é o Barcamp Portugal?</h1>
<p>O Barcamp Portugal nasceu em 2006 e pretende ser um ponto de encontro de gente que se junta para falar nos mais variados temas que, normalmente, gravitam em torno de Inovação, Empreendorismo e Web.</p>
<p>Este ano, optámos por usar este site em vez da tradicional wiki por permitir uma gestão mais rigorosa dos inscritos (no evento e nas refeições, mais tarde) e esperemos que facilite a comunicação entre as pessoas.</p>

<h1><img src="/images/mail-24.png" class="icon"> Novidades <a href="/feed.xml"><img src="/images/rss-16.png" class="icon" style="margin-right:0px"></a></h1>

<% for post in @posts %>
<div class="leftbar">
<h3><%= post.title %>, <%= post.created_at.strftime("%d/%m/%Y - %H:%M")%></h3>
<p><%= white_list post.body_html %><p>
<% cache "home_intro" do %>
<div class="yui-b first" id="intro">
<h1><img src="/images/mail-24.png" class="icon"> O que é o Barcamp Portugal?</h1>
<p>O Barcamp Portugal nasceu em 2006 e pretende ser um ponto de encontro de gente que se junta para falar nos mais variados temas que, normalmente, gravitam em torno de Inovação, Empreendorismo e Web.</p>
<p>Este ano, optámos por usar este site em vez da tradicional wiki por permitir uma gestão mais rigorosa dos inscritos (no evento e nas refeições, mais tarde) e esperemos que facilite a comunicação entre as pessoas.</p>

<h1><img src="/images/mail-24.png" class="icon"> Novidades <a href="/feed.xml"><img src="/images/rss-16.png" class="icon" style="margin-right:0px"></a></h1>

<% for post in @posts %>
<div class="leftbar">
<h3><%= post.title %>, <%= post.created_at.strftime("%d/%m/%Y - %H:%M")%></h3>
<p><%= white_list post.body_html %><p>
</div>
<% end %>
</div>
<% end %>
</div>
<% end %>
<% end %>
<div class="yui-u first">
<h2><img src="/images/phone.png" class="icon"> Users inscritos</h2>
<ul>
<%= render :partial => "users/user", :collection => @users %>
<% if @users.empty? %>
<li>nenhum. ainda :)</li>
<% cache "home_users" do %>
<div class="yui-u first">
<h2><img src="/images/phone.png" class="icon"> Users inscritos (<%=@user_count%>)</h2>
<ul>
<%= render :partial => "users/user", :collection => @users %>
<% if @users.empty? %>
<li>nenhum. ainda :)</li>
<% end %>
</ul>
<% if @user_count > 10 %>
<a href="/users">Ver a lista completa</a>
<% end %>
</ul>
<% if @user_count > 10 %>
<a href="/users">Ver a lista completa</a>
<% end %>
</div>
<div class="yui-u">
<h2><img src="/images/text.png" class="icon"> Sessões propostas</h2>
<ul>
<%= render :partial => "talks/talk", :collection => @talks %>
<% if @talks.empty? %>
<li>nenhuma. ainda :)</li>
</div>
<% end %>
<% cache "home_talks" do %>
<div class="yui-u">
<h2><img src="/images/text.png" class="icon"> Sessões propostas</h2>
<ul>
<%= render :partial => "talks/talk", :collection => @talks %>
<% if @talks.empty? %>
<li>nenhuma. ainda :)</li>
<% end %>
</ul>
<% if @talk_count > 4 %>
<a href="/talks">Ver a lista completa</a>
<% end %>
</ul>
<% if @talk_count > 4 %>
<a href="/talks">Ver a lista completa</a>
<% end %>
</div>
</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/includes/_textile_help.html.erb
Expand Up @@ -10,6 +10,7 @@ A descrição permite o uso de Textile, uma linguagem de markup que pretende ser
<tr><td>Texto +itálico+</td> <td>&rarr;</td> <td>Text <em>itálico</em></td></tr>
<tr><td>Uma lista<br/><br/>* Um item<br/>* Outro item</td> <td>&rarr;</td> <td>Uma lista<ul><li>Um item</li><li>Outro item</li></ul></td></tr>
<tr><td>"Link":http://google.com</td> <td>&rarr;</td> <td><a href="http://google.com">Link</a></td></tr>
<tr><td>Código:<br/>@&lt;script&gt;alert('test');&lt;/script&gt;@</td> <td>&rarr;</td> <td>Código:<br/><code><pre>&lt;script&gt;alert('test');&lt;/script&gt;</pre></code></td></tr>
</table>

Para uma referência mais exaustiva, <%= textile_link "clicar aqui" %>.
1 change: 1 addition & 0 deletions app/views/layouts/twocols.html.erb
Expand Up @@ -6,6 +6,7 @@
<%= stylesheet_link_tag "reset-fonts-grids", "base", "screen", :cache => true %>
<%= javascript_include_tag :defaults, :cache => true %>
<link rel="alternate" type="application/rss+xml" title="Novidades" href="/feed.xml" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<div id="doc2" class="yui-t6">
Expand Down
18 changes: 11 additions & 7 deletions app/views/users/index.html.erb
@@ -1,7 +1,11 @@
<h2><img src="/images/phone.png" class="icon"> Users inscritos</h2>
<ul>
<%= render :partial => "users/user", :collection => @users %>
<% if @users.empty? %>
<li>nenhum. ainda :)</li>
<% end %>
</ul>
<% cache "users_page" do %>
Até este momento, estão inscritas <%= @users.size %> pessoas, <%= @openid_count %> das quais usaram OpenID.

<h2><img src="/images/phone.png" class="icon"> Users inscritos (<%= @users.size %>)</h2>
<ul>
<%= render :partial => "users/user", :collection => @users %>
<% if @users.empty? %>
<li>nenhum. ainda :)</li>
<% end %>
</ul>
<% end %>
3 changes: 2 additions & 1 deletion config/environment.rb
Expand Up @@ -33,6 +33,7 @@

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
config.load_paths += %W( #{RAILS_ROOT}/app/sweepers )

# Force all environments to use the same logger level
# (by default production uses :info, the others :debug)
Expand Down Expand Up @@ -63,5 +64,5 @@
# config.active_record.schema_format = :sql

# Activate observers that should always be running
config.active_record.observers = :user_observer
config.active_record.observers = [:user_observer]
end
18 changes: 18 additions & 0 deletions db/migrate/20080705213159_populate_body_html.rb
@@ -0,0 +1,18 @@
class PopulateBodyHtml < ActiveRecord::Migration
def self.up
for c in Comment.find(:all)
c.save!
end

for p in Post.find(:all)
p.save!
end

for t in Talk.find(:all)
t.save!
end
end

def self.down
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20080705180543) do
ActiveRecord::Schema.define(:version => 20080705213159) do

create_table "admins", :force => true do |t|
t.string "username"
Expand Down
8 changes: 8 additions & 0 deletions test/unit/post_observer_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class PostObserverTest < Test::Unit::TestCase
# Replace this with your real tests.
def test_truth
assert true
end
end
8 changes: 8 additions & 0 deletions test/unit/talk_observer_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class TalkObserverTest < Test::Unit::TestCase
# Replace this with your real tests.
def test_truth
assert true
end
end

0 comments on commit 2a9e64f

Please sign in to comment.