From a05b141366007197abc18a31fcbbbbebcc892a1e Mon Sep 17 00:00:00 2001 From: Raphael Sofaer <raphael@joindiaspora.com> Date: Thu, 7 Apr 2011 16:36:14 -0700 Subject: [PATCH] Add tagged posts to db seed, make people#show and tags#show infinite scroll --- app/controllers/application_controller.rb | 3 +++ app/controllers/aspects_controller.rb | 3 +-- app/controllers/people_controller.rb | 15 ++++++++++----- app/controllers/tags_controller.rb | 17 +++++++++++------ app/helpers/application_helper.rb | 7 +++++-- app/views/aspects/_aspect_stream.haml | 2 +- app/views/people/show.html.haml | 9 ++++----- app/views/tags/show.haml | 9 ++++----- db/seeds.rb | 2 +- spec/controllers/people_controller_spec.rb | 8 ++++---- spec/controllers/tags_controller_spec.rb | 8 ++++---- 11 files changed, 48 insertions(+), 35 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 76f513fdcb..595d963a84 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -38,6 +38,9 @@ class ApplicationController < ActionController::Base @request_count = Request.where(:recipient_id => current_user.person.id).count if current_user end + def ensure_page + params[:page] = params[:page] ? params[:page].to_i : 1 + end def set_invites if user_signed_in? @invites = current_user.invites diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index 4d59643c34..9a66779aad 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -5,6 +5,7 @@ class AspectsController < ApplicationController before_filter :authenticate_user! before_filter :save_sort_order, :only => :index + before_filter :ensure_page, :only => :index respond_to :html respond_to :json, :only => [:show, :create] @@ -25,8 +26,6 @@ class AspectsController < ApplicationController redirect_to getting_started_path return end - params[:page] = params[:page] ? params[:page].to_i : 1 - @selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq unless params[:only_posts] diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 4bd76a6b5f..b334b4af31 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -4,6 +4,7 @@ class PeopleController < ApplicationController before_filter :authenticate_user!, :except => [:show] + before_filter :ensure_page, :only => :show respond_to :html respond_to :json, :only => [:index, :show] @@ -76,7 +77,7 @@ class PeopleController < ApplicationController @incoming_request = current_user.request_from(@person) @contact = current_user.contact_for(@person) @aspects_with_person = [] - if @contact + if @contact && !params[:only_posts] @aspects_with_person = @contact.aspects @aspect_ids = @aspects_with_person.map(&:id) @contacts_of_contact = @contact.contacts @@ -91,14 +92,18 @@ class PeopleController < ApplicationController else @commenting_disabled = false end - @posts = current_user.posts_from(@person).where(:type => "StatusMessage").includes(:comments).paginate(:per_page => 15, :page => params[:page]) + @posts = current_user.posts_from(@person).where(:type => "StatusMessage").includes(:comments).limit(15).offset(15*(params[:page]-1)) else @commenting_disabled = true - @posts = @person.posts.where(:type => "StatusMessage", :public => true).includes(:comments).paginate(:per_page => 15, :page => params[:page], :order => 'created_at DESC') + @posts = @person.posts.where(:type => "StatusMessage", :public => true).includes(:comments).limit(15).offset(15*(params[:page]-1)) end - @fakes = PostsFake.new(@posts) - respond_with @person, :locals => {:post_type => :all} + @posts = PostsFake.new(@posts) + if params[:only_posts] + render :partial => 'shared/stream', :locals => {:posts => @posts} + else + respond_with @person, :locals => {:post_type => :all} + end else flash[:error] = I18n.t 'people.show.does_not_exist' diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 46d5172ab5..05f2cc886f 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -7,6 +7,7 @@ class TagsController < ApplicationController skip_before_filter :set_invites skip_before_filter :which_action_and_user skip_before_filter :set_grammatical_gender + before_filter :ensure_page, :only => :show respond_to :html, :only => [:show] respond_to :json, :only => [:index] @@ -51,13 +52,17 @@ class TagsController < ApplicationController end @posts = @posts.tagged_with(params[:name]) - @posts = @posts.includes(:comments, :photos).paginate(:page => params[:page], :per_page => 15, :order => 'created_at DESC') + @posts = @posts.includes(:comments, :photos).order('created_at DESC').limit(15).offset(15*(params[:page]-1)) - profiles = Profile.tagged_with(params[:name]).where(:searchable => true).select('profiles.id, profiles.person_id') - @people = Person.where(:id => profiles.map{|p| p.person_id}).limit(15) - @people_count = Person.where(:id => profiles.map{|p| p.person_id}).count - - @fakes = PostsFake.new(@posts) + @posts = PostsFake.new(@posts) @commenting_disabled = true + + if params[:only_posts] + render :partial => 'shared/stream', :locals => {:posts => @posts} + else + profiles = Profile.tagged_with(params[:name]).where(:searchable => true).select('profiles.id, profiles.person_id') + @people = Person.where(:id => profiles.map{|p| p.person_id}).limit(15) + @people_count = Person.where(:id => profiles.map{|p| p.person_id}).count + end end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 944831697c..95991435ab 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -5,6 +5,9 @@ module ApplicationHelper @@youtube_title_cache = Hash.new("no-title") + def next_page + params[:page] ? (params[:page].to_i + 1) : 2 + end def timeago(time, options = {}) options[:class] ||= "timeago" content_tag(:abbr, time.to_s, options.merge(:title => time.iso8601)) if time @@ -48,7 +51,7 @@ module ApplicationHelper link = opts.delete(:link) if !link str << link_to(aspect.name, "#", 'data-guid' => aspect.id, :class => 'hard_aspect_link').html_safe - else + else str << link_for_aspect(aspect).html_safe end str << "</span>" @@ -167,7 +170,7 @@ module ApplicationHelper message = process_emphasis(message) message = process_youtube(message, options[:youtube_maps]) message = process_vimeo(message, options[:vimeo_maps]) - + message.gsub!(/<3/, "♥") if options[:newlines] diff --git a/app/views/aspects/_aspect_stream.haml b/app/views/aspects/_aspect_stream.haml index 382d17cae5..78f540716c 100644 --- a/app/views/aspects/_aspect_stream.haml +++ b/app/views/aspects/_aspect_stream.haml @@ -13,6 +13,6 @@ - if posts.length > 0 = render 'shared/stream', :posts => posts #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 => next_page, :a_ids => params[:a_ids]), :class => 'paginate') - else = render 'aspects/no_posts_message', :post_count => posts.length diff --git a/app/views/people/show.html.haml b/app/views/people/show.html.haml index 7ff399398a..f10b9f9031 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -74,15 +74,14 @@ %hr - - if @posts.count > 0 + - if @posts.length > 0 -if @post_type == :photos = render 'photos/index', :photos => @posts - else #main_stream.stream - = render 'shared/stream', :posts => @fakes, :commenting_disabled => @commenting_disabled - %a.paginate - = t("more") - = will_paginate @posts + = render 'shared/stream', :posts => @posts, :commenting_disabled => @commenting_disabled + #pagination + =link_to(t('more'), person_path(@person, :page => next_page), :class => 'paginate') - else #stream diff --git a/app/views/tags/show.haml b/app/views/tags/show.haml index 52af3add6b..4b16d4476a 100644 --- a/app/views/tags/show.haml +++ b/app/views/tags/show.haml @@ -20,13 +20,12 @@ .span-15 #main_stream.stream - - if @fakes.length > 0 - = render 'shared/stream', :posts => @fakes - %a.paginate - = t("more") + - if @posts.length > 0 + = render 'shared/stream', :posts => @posts + #pagination + =link_to(t('more'), tag_path(params[:name], :page => next_page), :class => 'paginate') - else = t('.nobody_talking', :tag => "##{params[:name]}") - = will_paginate @posts .prepend-2.span-7.last %h3 diff --git a/db/seeds.rb b/db/seeds.rb index 21fad3f733..ef0d810830 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -33,7 +33,7 @@ require 'spec/support/user_methods' time_interval = 1000 (1..25).each do |n| [alice, bob, eve].each do |u| - post = u.post :status_message, :text => "#{u.username} - #{n}", :to => u.aspects.first.id + post = u.post :status_message, :text => "#{u.username} - #{n} - #seeded", :to => u.aspects.first.id post.created_at = post.created_at + time_interval post.updated_at = post.updated_at + time_interval post.save diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index 9193a11845..fb23121fcd 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -123,7 +123,7 @@ describe PeopleController do @user.post(:status_message, :text => "public", :to => 'all', :public => true) @user.reload.posts.length.should == 3 get :show, :id => @user.person.to_param - assigns(:posts).should =~ @user.posts + assigns(:posts).models.should =~ @user.posts end it "renders the comments on the user's posts" do @@ -152,7 +152,7 @@ describe PeopleController do get :show, :id => @person.id - assigns[:posts].should =~ public_posts + assigns[:posts].models.should =~ public_posts end it 'throws 404 if the person is remote' do @@ -182,7 +182,7 @@ describe PeopleController do bob.reload.posts.length.should == 4 get :show, :id => @person.id - assigns(:posts).should =~ posts_user_can_see + assigns(:posts).models.should =~ posts_user_can_see end end @@ -204,7 +204,7 @@ describe PeopleController do eve.reload.posts.length.should == 3 get :show, :id => @person.id - assigns[:posts].should =~ [public_post] + assigns[:posts].models.should =~ [public_post] end end end diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index ff0f944315..8af83537b2 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -46,19 +46,19 @@ describe TagsController do it 'displays your own post' do my_post = alice.post(:status_message, :text => "#what", :to => 'all') get :show, :name => 'what' - assigns(:posts).should == [my_post] + assigns(:posts).models.should == [my_post] response.status.should == 200 end it "displays a friend's post" do other_post = bob.post(:status_message, :text => "#hello", :to => 'all') get :show, :name => 'hello' - assigns(:posts).should == [other_post] + assigns(:posts).models.should == [other_post] response.status.should == 200 end it 'displays a public post' do other_post = eve.post(:status_message, :text => "#hello", :public => true, :to => 'all') get :show, :name => 'hello' - assigns(:posts).should == [other_post] + assigns(:posts).models.should == [other_post] response.status.should == 200 end end @@ -89,7 +89,7 @@ describe TagsController do end it "assigns the right set of posts" do get :show, :name => 'what' - assigns[:posts].should == [@post] + assigns[:posts].models.should == [@post] end it 'succeeds with comments' do alice.comment('what WHAT!', :on => @post) -- GitLab