Hopefully cut down the stream rendering time by memoizing the new comment form

This commit is contained in:
Raphael 2010-12-07 09:27:43 -08:00
parent 2ab4d1664d
commit 18319f8e42
4 changed files with 28 additions and 3 deletions

View file

@ -3,7 +3,7 @@
# the COPYRIGHT file.
module StreamHelper
GSUB_THIS = "FIUSDHVIUSHDVIUBAIUHAPOIUXJM"
def comment_toggle(count)
if count == 0
link_to "#{t('comments.new_comment.comment').downcase}", '#', :class => "show_post_comments"
@ -14,4 +14,9 @@ module StreamHelper
end
end
def new_comment_form(post_id)
@form ||= controller.render_to_string(
:partial => 'comments/new_comment', :locals => {:post_id => GSUB_THIS})
@form.gsub(GSUB_THIS, post_id.to_s)
end
end

View file

@ -10,5 +10,5 @@
-else
= render :partial => 'comments/comment', :collection => comment_hashes, :as => :hash
%li.comment.show
= render 'comments/new_comment', :post_id => post_id
= new_comment_form(post_id)

View file

@ -53,7 +53,7 @@ describe AspectsController do
it 'takes time' do
Benchmark.realtime{
get :index
}.should < 3
}.should < 2
end
end
end

View file

@ -0,0 +1,20 @@
require 'spec_helper'
describe StreamHelper do
before do
@user = make_user
@aspect = @user.aspects.create(:name => 'aspect')
@post = @user.post(:status_message, :message => "hi", :to => @aspect.id)
end
it 'renders a new comment form' do
new_comment_form(@post.id).should ==
@controller.render_to_string(:partial => 'comments/new_comment', :locals => {:post_id => @post.id})
end
it 'renders it fast the second time' do
new_comment_form(@post.id)
time = Benchmark.realtime{
new_comment_form(@post.id)
}
(time*1000).should < 1
end
end