Merge pull request #4463 from consul/fix_follow_cache
Expire cache when users follow/unfollow
This commit is contained in:
@@ -3,13 +3,12 @@ class FollowsController < ApplicationController
|
|||||||
load_and_authorize_resource
|
load_and_authorize_resource
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@follow = Follow.create!(user: current_user, followable: find_followable)
|
@follow.save!
|
||||||
flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.create.notice")
|
flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.create.notice")
|
||||||
render :refresh_follow_button
|
render :refresh_follow_button
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@follow = Follow.find(params[:id])
|
|
||||||
@follow.destroy!
|
@follow.destroy!
|
||||||
flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.destroy.notice")
|
flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.destroy.notice")
|
||||||
render :refresh_follow_button
|
render :refresh_follow_button
|
||||||
@@ -17,8 +16,8 @@ class FollowsController < ApplicationController
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def find_followable
|
def follow_params
|
||||||
params[:followable_type].constantize.find(params[:followable_id])
|
params.permit(:followable_type, :followable_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def followable_translation_key(followable)
|
def followable_translation_key(followable)
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ module Abilities
|
|||||||
can [:flag, :unflag], Budget::Investment
|
can [:flag, :unflag], Budget::Investment
|
||||||
cannot [:flag, :unflag], Budget::Investment, author_id: user.id
|
cannot [:flag, :unflag], Budget::Investment, author_id: user.id
|
||||||
|
|
||||||
can [:create, :destroy], Follow
|
can [:create, :destroy], Follow, user_id: user.id
|
||||||
|
|
||||||
can [:destroy], Document do |document|
|
can [:destroy], Document do |document|
|
||||||
document.documentable&.author_id == user.id
|
document.documentable&.author_id == user.id
|
||||||
|
|||||||
@@ -3,6 +3,5 @@ class Follow < ApplicationRecord
|
|||||||
belongs_to :followable, polymorphic: true
|
belongs_to :followable, polymorphic: true
|
||||||
|
|
||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
validates :followable_id, presence: true
|
validates :followable, presence: true
|
||||||
validates :followable_type, presence: true
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
investment.image,
|
investment.image,
|
||||||
investment.author,
|
investment.author,
|
||||||
Flag.flagged?(current_user, investment),
|
Flag.flagged?(current_user, investment),
|
||||||
|
investment.followed_by?(current_user),
|
||||||
@investment_votes] do %>
|
@investment_votes] do %>
|
||||||
<section class="budget-investment-show" id="<%= dom_id(investment) %>">
|
<section class="budget-investment-show" id="<%= dom_id(investment) %>">
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
@proposal,
|
@proposal,
|
||||||
@proposal.author,
|
@proposal.author,
|
||||||
Flag.flagged?(current_user, @proposal),
|
Flag.flagged?(current_user, @proposal),
|
||||||
|
@proposal.followed_by?(current_user),
|
||||||
@proposal_votes] do %>
|
@proposal_votes] do %>
|
||||||
<div class="proposal-show">
|
<div class="proposal-show">
|
||||||
<div id="<%= dom_id(@proposal) %>" class="row">
|
<div id="<%= dom_id(@proposal) %>" class="row">
|
||||||
|
|||||||
@@ -119,6 +119,16 @@ describe Abilities::Common do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "follows" do
|
||||||
|
let(:other_user) { create(:user) }
|
||||||
|
|
||||||
|
it { should be_able_to(:create, build(:follow, :followed_proposal, user: user)) }
|
||||||
|
it { should_not be_able_to(:create, build(:follow, :followed_proposal, user: other_user)) }
|
||||||
|
|
||||||
|
it { should be_able_to(:destroy, create(:follow, :followed_proposal, user: user)) }
|
||||||
|
it { should_not be_able_to(:destroy, create(:follow, :followed_proposal, user: other_user)) }
|
||||||
|
end
|
||||||
|
|
||||||
describe "other users" do
|
describe "other users" do
|
||||||
let(:other_user) { create(:user) }
|
let(:other_user) { create(:user) }
|
||||||
|
|
||||||
|
|||||||
@@ -21,4 +21,16 @@ describe Follow do
|
|||||||
follow.followable_type = nil
|
follow.followable_type = nil
|
||||||
expect(follow).not_to be_valid
|
expect(follow).not_to be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "is not valid with an invalid followable_type" do
|
||||||
|
follow.followable_type = "NotARealModel"
|
||||||
|
|
||||||
|
expect { follow.valid? }.to raise_exception "uninitialized constant NotARealModel"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is not valid with the ID of a non-existent record" do
|
||||||
|
follow.followable_id = Proposal.last.id + 1
|
||||||
|
|
||||||
|
expect(follow).not_to be_valid
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user