From 8ffe0f3de35f76116f1f81be59d746ee5a2f83c2 Mon Sep 17 00:00:00 2001 From: Edison Yap Date: Tue, 9 Jul 2019 05:28:27 +0800 Subject: [PATCH] Bust cache for articles in series (#3052) --- app/models/article.rb | 2 +- app/models/collection.rb | 6 ++++++ spec/factories/articles.rb | 2 ++ spec/factories/collections.rb | 6 ++++++ spec/models/collection_spec.rb | 13 +++++++++++++ spec/requests/comments_spec.rb | 4 +--- 6 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 spec/models/collection_spec.rb diff --git a/app/models/article.rb b/app/models/article.rb index 24d04d9b0..05511b9fd 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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 diff --git a/app/models/collection.rb b/app/models/collection.rb index 0400a223c..7a9e0dae3 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -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 diff --git a/spec/factories/articles.rb b/spec/factories/articles.rb index 8018d00bc..881038572 100644 --- a/spec/factories/articles.rb +++ b/spec/factories/articles.rb @@ -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} --- diff --git a/spec/factories/collections.rb b/spec/factories/collections.rb index 8a9eacfdd..db57ed7c6 100644 --- a/spec/factories/collections.rb +++ b/spec/factories/collections.rb @@ -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 diff --git a/spec/models/collection_spec.rb b/spec/models/collection_spec.rb new file mode 100644 index 000000000..2d5210fe1 --- /dev/null +++ b/spec/models/collection_spec.rb @@ -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 diff --git a/spec/requests/comments_spec.rb b/spec/requests/comments_spec.rb index c340c5e70..a1ff9d08d 100644 --- a/spec/requests/comments_spec.rb +++ b/spec/requests/comments_spec.rb @@ -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