Render less stuff, set fewer things, make AspectsController#index more horrifying. Maybe Vanna could help with this? Mustache definitely would.

This commit is contained in:
Raphael Sofaer 2011-04-06 12:11:49 -07:00
parent fd0fa61d53
commit bbf1bf00cb
9 changed files with 54 additions and 30 deletions

View file

@ -24,7 +24,7 @@ class ApplicationController < ActionController::Base
def set_header_data def set_header_data
if user_signed_in? if user_signed_in?
if request.format.html? if request.format.html? && !params[:only_posts]
@aspect = nil @aspect = nil
@object_aspect_ids = [] @object_aspect_ids = []
@notification_count = Notification.for(current_user, :unread =>true).count @notification_count = Notification.for(current_user, :unread =>true).count

View file

@ -16,7 +16,9 @@ class AspectsController < ApplicationController
else else
@aspects = current_user.aspects @aspects = current_user.aspects
end end
@aspects = @aspects.includes(:contacts => {:person => :profile})
#No aspect_listings on infinite scroll
@aspects = @aspects.includes(:contacts => {:person => :profile}) unless params[:only_posts]
# redirect to signup # redirect to signup
if (current_user.getting_started == true || @aspects.blank?) && !request.format.mobile? && !request.format.js? if (current_user.getting_started == true || @aspects.blank?) && !request.format.mobile? && !request.format.js?
@ -25,18 +27,26 @@ class AspectsController < ApplicationController
end end
params[:page] = params[:page] ? params[:page].to_i : 1 params[:page] = params[:page] ? params[:page].to_i : 1
@selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq
@selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq unless params[:only_posts]
@aspect_ids = @aspects.map { |a| a.id } @aspect_ids = @aspects.map { |a| a.id }
@posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids, :type => 'StatusMessage', :order => session[:sort_order] + ' DESC', :page => params[:page]).includes( posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids,
:comments, :mentions, :likes, :dislikes) :type => 'StatusMessage',
:order => session[:sort_order] + ' DESC',
@fakes = PostsFake.new(@posts) :page => params[:page]
).includes(:comments, :mentions, :likes, :dislikes)
@posts = PostsFake.new(posts)
if params[:only_posts]
render :partial => 'shared/stream', :locals => {:posts => @posts}
else
@contact_count = current_user.contacts.count @contact_count = current_user.contacts.count
@aspect = :all unless params[:a_ids] @aspect = :all unless params[:a_ids]
@aspect ||= @aspects.first # used in mobile @aspect ||= @aspects.first # used in mobile
end end
end
def create def create
@aspect = current_user.aspects.create(params[:aspect]) @aspect = current_user.aspects.create(params[:aspect])

View file

@ -11,7 +11,7 @@
= link_to_if(session[:sort_order] == 'updated_at', t('.post_time'), aspects_path(:a_ids => params[:a_ids], :sort_order => 'created_at' )) = link_to_if(session[:sort_order] == 'updated_at', t('.post_time'), aspects_path(:a_ids => params[:a_ids], :sort_order => 'created_at' ))
#main_stream.stream{:data => {:guids => aspect_ids.join(',')}} #main_stream.stream{:data => {:guids => aspect_ids.join(',')}}
- if posts.length > 0 - if posts.length > 0
= render 'shared/stream', :posts => fakes = render 'shared/stream', :posts => posts
#pagination #pagination
=link_to(t('more'), aspects_path(:page => (params[:page] + 1), :a_ids => params[:a_ids]), :class => 'paginate') =link_to(t('more'), aspects_path(:page => (params[:page] + 1), :a_ids => params[:a_ids]), :class => 'paginate')
- else - else

View file

@ -10,8 +10,7 @@
= render 'aspect_stream', = render 'aspect_stream',
:aspect => @aspect, :aspect => @aspect,
:aspect_ids => @aspect_ids, :aspect_ids => @aspect_ids,
:posts => @posts, :posts => @posts
:fakes => @fakes
.span-7.last .span-7.last
#home_user_badge #home_user_badge

View file

@ -1,3 +1,3 @@
$('#aspect_stream_container').html("<%= escape_javascript(render('aspects/aspect_stream', :aspect => @aspect, :aspect_ids => @aspect_ids, :posts => @posts, :fakes => @fakes)) %>"); $('#aspect_stream_container').html("<%= escape_javascript(render('aspects/aspect_stream', :aspect => @aspect, :aspect_ids => @aspect_ids, :posts => @posts)) %>");
$('#aspect_listings').html("<%= escape_javascript(render('aspects/aspect_listings', :aspects => @aspects)) %>"); $('#aspect_listings').html("<%= escape_javascript(render('aspects/aspect_listings', :aspects => @aspects)) %>");
$('a[rel*=facebox]').facebox(); $('a[rel*=facebox]').facebox();

View file

@ -11,7 +11,7 @@
%h1 %h1
= link_to t('.post_a_message'), '#publisher_page', :id => 'publisher_button' = link_to t('.post_a_message'), '#publisher_page', :id => 'publisher_button'
#main_stream.stream #main_stream.stream
= render 'shared/stream', :posts => @fakes = render 'shared/stream', :posts => @posts
%a.more-link.paginate{:href => aspects_path(:a_ids => params[:a_ids], :page => params[:page] + 1)} %a.more-link.paginate{:href => aspects_path(:a_ids => params[:a_ids], :page => params[:page] + 1)}
%h2= t("more") %h2= t("more")
- content_for :subpages do - content_for :subpages do

View file

@ -24,8 +24,13 @@ class PostsFake
end end
end end
def models
self.post_fakes.map{|a| a.model }
end
class Fake class Fake
attr_accessor :comments attr_accessor :comments
attr_reader :model
def initialize(model, fakes_collection) def initialize(model, fakes_collection)
@fakes_collection = fakes_collection @fakes_collection = fakes_collection
@model = model @model = model

View file

@ -4,10 +4,11 @@ var InfiniteScroll = {
// selector for the paged navigation (it will be hidden) // selector for the paged navigation (it will be hidden)
nextSelector : ".paginate", nextSelector : ".paginate",
// selector for the NEXT link (to page 2) // selector for the NEXT link (to page 2)
itemSelector : "#main_stream .stream_element", itemSelector : ".stream_element",
// selector for all items you'll retrieve // selector for all items you'll retrieve
pathParse : function( pathStr, nextPage ){ pathParse : function( pathStr, nextPage ){
return pathStr.replace( "page=2", "page=" + nextPage) var newPath = pathStr.replace("?", "?only_posts=true&");
return newPath.replace( "page=2", "page=" + nextPage);
}, },
bufferPx: 300, bufferPx: 300,
debug: false, debug: false,

View file

@ -116,57 +116,66 @@ describe AspectsController do
it "pulls back non hidden posts" do it "pulls back non hidden posts" do
get :index get :index
assigns[:posts].include?(@status).should be_true assigns[:posts].models.include?(@status).should be_true
end end
it "does not pull back hidden posts" do it "does not pull back hidden posts" do
@vis.update_attributes( :hidden => true ) @vis.update_attributes( :hidden => true )
get :index get :index
assigns[:posts].include?(@status).should be_false assigns[:posts].models.include?(@status).should be_false
end end
end end
describe 'infinite scroll' do
it 'renders with the infinite scroll param' do
get :index, :only_posts => true
assigns[:posts].models.include?(@posts.first).should be_true
response.should be_success
end
end
describe "ordering" do describe "ordering" do
it "orders posts by updated_at by default" do it "orders posts by updated_at by default" do
get :index get :index
assigns(:posts).should == @posts assigns(:posts).models.should == @posts
end end
it "orders posts by created_at on request" do it "orders posts by created_at on request" do
get :index, :sort_order => 'created_at' get :index, :sort_order => 'created_at'
assigns(:posts).should == @posts.reverse assigns(:posts).models.should == @posts.reverse
end end
it "remembers your sort order and lets you override the memory" do it "remembers your sort order and lets you override the memory" do
get :index, :sort_order => "created_at" get :index, :sort_order => "created_at"
assigns(:posts).should == @posts.reverse assigns(:posts).models.should == @posts.reverse
get :index get :index
assigns(:posts).should == @posts.reverse assigns(:posts).models.should == @posts.reverse
get :index, :sort_order => "updated_at" get :index, :sort_order => "updated_at"
assigns(:posts).should == @posts assigns(:posts).models.should == @posts
end end
it "doesn't allow SQL injection" do it "doesn't allow SQL injection" do
get :index, :sort_order => "\"; DROP TABLE users;" get :index, :sort_order => "\"; DROP TABLE users;"
assigns(:posts).should == @posts assigns(:posts).models.should == @posts
get :index, :sort_order => "created_at" get :index, :sort_order => "created_at"
assigns(:posts).should == @posts.reverse assigns(:posts).models.should == @posts.reverse
end end
end end
it "returns all posts by default" do it "returns all posts by default" do
@alice.aspects.reload @alice.aspects.reload
get :index get :index
assigns(:posts).length.should == 2 assigns(:posts).models.length.should == 2
end end
it "can filter to a single aspect" do it "can filter to a single aspect" do
get :index, :a_ids => [@alices_aspect_2.id.to_s] get :index, :a_ids => [@alices_aspect_2.id.to_s]
assigns(:posts).length.should == 1 assigns(:posts).models.length.should == 1
end end
it "can filter to multiple aspects" do it "can filter to multiple aspects" do
get :index, :a_ids => [@alices_aspect_1.id.to_s, @alices_aspect_2.id.to_s] get :index, :a_ids => [@alices_aspect_1.id.to_s, @alices_aspect_2.id.to_s]
assigns(:posts).length.should == 2 assigns(:posts).models.length.should == 2
end end
end end