From 88cde6d018bda19da690e2e735963deab12c86fc Mon Sep 17 00:00:00 2001 From: rgarcia Date: Thu, 10 May 2018 16:07:53 +0200 Subject: [PATCH] Deal gracefully with hidden followable in my activity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We were seeing an exception when a user was following a proposal and that proposal becomes hidden With this commit we are skipping this proposals and hopefully fixing this exception We are also adjusting the count of followed proposals, without including hidden proposals. It might be cleaner, to remove a `Follow` when its `Followable` becomes hidden. But then we should probably activate the `Follow` again if the `Followable` becomes non-hidden… For now this commit should suffice :relieved: --- app/controllers/users_controller.rb | 2 +- app/helpers/followables_helper.rb | 2 ++ spec/features/users_spec.rb | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6649c5283..5065f1aca 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -17,7 +17,7 @@ class UsersController < ApplicationController debates: (Setting['feature.debates'] ? Debate.where(author_id: @user.id).count : 0), budget_investments: (Setting['feature.budgets'] ? Budget::Investment.where(author_id: @user.id).count : 0), comments: only_active_commentables.count, - follows: @user.follows.count) + follows: @user.follows.map(&:followable).compact.count) end def load_filtered_activity diff --git a/app/helpers/followables_helper.rb b/app/helpers/followables_helper.rb index f185ab25a..f7dd4a7a0 100644 --- a/app/helpers/followables_helper.rb +++ b/app/helpers/followables_helper.rb @@ -12,6 +12,8 @@ module FollowablesHelper end def render_follow(follow) + return unless follow.followable.present? + followable = follow.followable partial = followable_class_name(followable) + "_follow" locals = {followable_class_name(followable).to_sym => followable} diff --git a/spec/features/users_spec.rb b/spec/features/users_spec.rb index f46d5850d..e188853cf 100644 --- a/spec/features/users_spec.rb +++ b/spec/features/users_spec.rb @@ -428,7 +428,7 @@ feature 'Users' do @user = create(:user) end - scenario 'Not display following tab when user is not following any followable' do + scenario "Do not display follows' tab when user is not following any followables" do visit user_path(@user) expect(page).not_to have_content('0 Following') @@ -443,6 +443,19 @@ feature 'Users' do expect(page).to have_selector(".activity li.is-active", text: "1 Following") end + scenario "Gracefully handle followables that have been hidden", :focus do + active_proposal = create(:proposal) + hidden_proposal = create(:proposal) + + create(:follow, followable: active_proposal, user: @user) + create(:follow, followable: hidden_proposal, user: @user) + + hidden_proposal.hide + visit user_path(@user) + + expect(page).to have_content('1 Following') + end + describe 'Proposals' do scenario 'Display following tab when user is following one proposal at least' do @@ -463,7 +476,7 @@ feature 'Users' do expect(page).to have_link('Citizen proposals', href: "#citizen_proposals") end - scenario 'Not display proposal tab when user is not following any proposal' do + scenario "Do not display proposals' tab when user is not following any proposal" do visit user_path(@user, filter: "follows") expect(page).not_to have_link('Citizen proposals', href: "#citizen_proposals")