Bust cache for articles in series (#3052)

This commit is contained in:
Edison Yap 2019-07-09 05:28:27 +08:00 committed by Mac Siri
parent d997ffee4f
commit 8ffe0f3de3
6 changed files with 29 additions and 4 deletions

View file

@ -16,7 +16,7 @@ class Article < ApplicationRecord
belongs_to :user
belongs_to :job_opportunity, optional: true
belongs_to :organization, optional: true
belongs_to :collection, optional: true
belongs_to :collection, optional: true, touch: true
counter_culture :user
counter_culture :organization

View file

@ -6,7 +6,13 @@ class Collection < ApplicationRecord
validates :user_id, presence: true
validates :slug, uniqueness: { scope: :user_id }
after_touch :touch_articles
def self.find_series(slug, user)
Collection.find_or_create_by(slug: slug, user: user)
end
def touch_articles
articles.update_all(updated_at: Time.zone.now)
end
end

View file

@ -12,6 +12,7 @@ FactoryBot.define do
with_hr_issue { false }
with_tweet_tag { false }
with_title { true }
with_collection { nil }
end
association :user, factory: :user, strategy: :create
description { Faker::Hipster.paragraph(1)[0..100] }
@ -25,6 +26,7 @@ FactoryBot.define do
published: #{published}
tags: #{tags if with_tags}
date: #{date if with_date}
series: #{with_collection&.slug if with_collection}
canonical_url: #{canonical_url if with_canonical_url}
---

View file

@ -2,4 +2,10 @@ FactoryBot.define do
factory :collection do
slug { "word-#{rand(10_000)}" }
end
trait :with_articles do
after(:create) do |collection|
create_list(:article, 3, with_collection: collection, user: collection.user)
end
end
end

View file

@ -0,0 +1,13 @@
require "rails_helper"
RSpec.describe Collection, type: :model do
let(:user) { create(:user) }
let(:collection) { create(:collection, :with_articles, user: user) }
describe "when a single article in collection is updated" do
it "touches all articles in the collection" do
random_article = collection.articles.sample
expect { random_article.touch }.to change { collection.articles.map(&:updated_at) }
end
end
end

View file

@ -49,9 +49,7 @@ RSpec.describe "Comments", type: :request do
commentable_type: "Article",
user_id: user.id)
get child.path
expect(response.body).to include("TOP OF THREAD")
expect(response.body).to include(comment.title(150))
expect(response.body).to include(child.processed_html)
expect(CGI.unescapeHTML(response.body)).to include("TOP OF THREAD", child.processed_html, comment.title(150))
end
end