From d2222a97e53129ad7466319c13dc776bcb7e2c77 Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Tue, 10 Mar 2015 03:27:28 +0100 Subject: [PATCH] Fix photo count in the profile view --- app/controllers/people_controller.rb | 8 ++++---- app/controllers/photos_controller.rb | 2 +- app/models/user/querying.rb | 3 +++ spec/controllers/people_controller_spec.rb | 24 ++++++++++++++++++++++ spec/controllers/photos_controller_spec.rb | 12 +++++++++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 93f214460..74a02a240 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -86,7 +86,7 @@ class PeopleController < ApplicationController end gon.preloads[:person] = @person_json gon.preloads[:photos] = { - count: photos_from(@person).count(:all), + count: photos_from(@person, :all).count(:all) } gon.preloads[:contacts] = { count: Contact.contact_contacts_for(current_user, @person).count(:all), @@ -144,7 +144,7 @@ class PeopleController < ApplicationController @contacts_of_contact = Contact.contact_contacts_for(current_user, @person) gon.preloads[:person] = PersonPresenter.new(@person, current_user).full_hash_with_profile gon.preloads[:photos] = { - count: photos_from(@person).count(:all), + count: photos_from(@person, :all).count(:all) } gon.preloads[:contacts] = { count: @contacts_of_contact.count(:all), @@ -220,9 +220,9 @@ class PeopleController < ApplicationController @person.try(:remote?) && !user_signed_in? end - def photos_from(person) + def photos_from(person, limit) @photos ||= if user_signed_in? - current_user.photos_from(person) + current_user.photos_from(person, limit: limit) else Photo.where(author_id: person.id, public: true) end.order('created_at desc') diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 4b1599f25..2993b5dbf 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -29,7 +29,7 @@ class PhotosController < ApplicationController format.all do gon.preloads[:person] = PersonPresenter.new(@person, current_user).full_hash_with_profile gon.preloads[:photos] = { - count: @posts.count(:all), + count: current_user.photos_from(@person, limit: :all).count(:all) } gon.preloads[:contacts] = { count: Contact.contact_contacts_for(current_user, @person).count(:all), diff --git a/app/models/user/querying.rb b/app/models/user/querying.rb index e10fbe036..a307e8a42 100644 --- a/app/models/user/querying.rb +++ b/app/models/user/querying.rb @@ -165,6 +165,9 @@ module User::Querying } defaults[:type] = Stream::Base::TYPES_OF_POST_IN_STREAM if klass == Post opts = defaults.merge(opts) + if opts[:limit] == :all + opts.delete(:limit) + end opts[:order_field] = opts[:order].split.first.to_sym opts[:order_with_table] = klass.table_name + '.' + opts[:order] diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index f19222f8d..74610562e 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -218,6 +218,18 @@ describe PeopleController, :type => :controller do expect(assigns(:photos)).to include public_photo end + it "displays the correct number of photos" do + 16.times do |i| + eve.post(:photo, :user_file => uploaded_photo, :to => eve.aspects.first.id, :public => true) + end + get :show, :id => eve.person.to_param + expect(response.body).to include '"photos":{"count":16}' + + eve.post(:photo, :user_file => uploaded_photo, :to => eve.aspects.first.id, :public => false) + get :show, :id => eve.person.to_param + expect(response.body).to include '"photos":{"count":16}' # eve is not sharing with alice + end + context "when the person is the current user" do it "succeeds" do get :show, :id => @user.person.to_param @@ -473,6 +485,18 @@ describe PeopleController, :type => :controller do expect(flash[:error]).to be_present expect(response).to redirect_to people_path end + + it "displays the correct number of photos" do + 16.times do |i| + eve.post(:photo, :user_file => uploaded_photo, :to => eve.aspects.first.id, :public => true) + end + get :contacts, :person_id => eve.person.to_param + expect(response.body).to include '"photos":{"count":16}' + + eve.post(:photo, :user_file => uploaded_photo, :to => eve.aspects.first.id, :public => false) + get :contacts, :person_id => eve.person.to_param + expect(response.body).to include '"photos":{"count":16}' # eve is not sharing with alice + end end describe '#diaspora_id?' do diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index 24bdf5354..8efdfe769 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -94,6 +94,18 @@ describe PhotosController, :type => :controller do expect(assigns[:posts]).to eq([@bobs_photo]) end + it "displays the correct number of photos" do + 16.times do |i| + eve.post(:photo, :user_file => uploaded_photo, :to => eve.aspects.first.id, :public => true) + end + get :index, :person_id => eve.person.to_param + expect(response.body).to include '"photos":{"count":16}' + + eve.post(:photo, :user_file => uploaded_photo, :to => eve.aspects.first.id, :public => false) + get :index, :person_id => eve.person.to_param + expect(response.body).to include '"photos":{"count":16}' # eve is not sharing with alice + end + it "returns json when requested" do request.env['HTTP_ACCEPT'] = 'application/json' get :index, :person_id => alice.person.guid.to_s