Refactor:Remove Production Conditional for Page View Updates (#11164)

This commit is contained in:
Molly Struve 2020-10-30 14:32:54 -04:00 committed by GitHub
parent 6e9d7bf00f
commit ca3452f044
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View file

@ -32,7 +32,7 @@ class PageViewsController < ApplicationMetalController
private
def update_article_page_views
return if Rails.env.production? && rand(15) != 1 # We don't need to update the article page views every time.
return if skip_page_view_update?
@article = Article.find(page_view_params[:article_id])
new_page_views_count = @article.page_views.sum(:counts_for_number_of_views)
@ -46,7 +46,7 @@ class PageViewsController < ApplicationMetalController
end
def update_organic_page_views
return if Rails.env.production? && rand(100) != 1 # We need to do this operation only once in a while.
return if skip_organic_page_view_update?
page_views_from_google_com = @article.page_views.where(referrer: "https://www.google.com/")
@ -64,4 +64,14 @@ class PageViewsController < ApplicationMetalController
.where("created_at > ?", 1.month.ago).sum(:counts_for_number_of_views)
@article.update_column(:organic_page_views_past_month_count, organic_count_past_month_count)
end
def skip_page_view_update?
# We don't need to update the article page views every time.
rand(15) != 1
end
def skip_organic_page_view_update?
# We need to do this operation only once in a while.
rand(100) != 1
end
end

View file

@ -4,6 +4,13 @@ RSpec.describe "PageViews", type: :request do
let(:user) { create(:user, :trusted) }
let(:article) { create(:article) }
before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(PageViewsController).to receive(:skip_page_view_update?).and_return(false)
allow_any_instance_of(PageViewsController).to receive(:skip_organic_page_view_update?).and_return(false)
# rubocop:enable RSpec/AnyInstance
end
describe "POST /page_views" do
context "when user signed in" do
before do