From 7520caf0e2fa03acc51ee92b32d8c334c53b991d Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Mon, 14 Sep 2020 11:37:59 -0500 Subject: [PATCH] [deploy] Refactor:Remove ReadingList Labor Class, Replace with user Method (#10322) --- app/controllers/async_info_controller.rb | 4 +--- app/labor/reading_list.rb | 21 --------------------- app/models/user.rb | 8 ++++++++ spec/labor/reading_list_spec.rb | 24 ------------------------ spec/models/user_spec.rb | 9 +++++++++ 5 files changed, 18 insertions(+), 48 deletions(-) delete mode 100644 app/labor/reading_list.rb delete mode 100644 spec/labor/reading_list_spec.rb diff --git a/app/controllers/async_info_controller.rb b/app/controllers/async_info_controller.rb index 2e89bfd51..02cd4bab6 100644 --- a/app/controllers/async_info_controller.rb +++ b/app/controllers/async_info_controller.rb @@ -54,7 +54,7 @@ class AsyncInfoController < ApplicationController followed_tags: @user.cached_followed_tags.to_json(only: %i[id name bg_color_hex text_color_hex hotness_score], methods: [:points]), followed_podcast_ids: @user.cached_following_podcasts_ids, - reading_list_ids: ReadingList.new(@user).cached_ids_of_articles, + reading_list_ids: @user.cached_reading_list_article_ids, blocked_user_ids: @user.all_blocking.pluck(:blocked_id), saw_onboarding: @user.saw_onboarding, checked_code_of_conduct: @user.checked_code_of_conduct, @@ -77,8 +77,6 @@ class AsyncInfoController < ApplicationController #{current_user&.last_followed_at}__ #{current_user&.updated_at}__ #{current_user&.reactions_count}__ - #{current_user&.saw_onboarding}__ - #{current_user&.checked_code_of_conduct}__ #{current_user&.articles_count}__ #{current_user&.pro?}__ #{current_user&.blocking_others_count}__" diff --git a/app/labor/reading_list.rb b/app/labor/reading_list.rb deleted file mode 100644 index a0e3907c7..000000000 --- a/app/labor/reading_list.rb +++ /dev/null @@ -1,21 +0,0 @@ -class ReadingList - attr_accessor :user - - def initialize(user) - @user = user - end - - def cached_ids_of_articles - Rails.cache.fetch("reading_list_ids_of_articles_#{user.id}_#{user.public_reactions_count}") do - ids_of_articles - end - end - - def ids_of_articles - Reaction.where(reaction_criteria).where.not(status: "archived").order(created_at: :desc).pluck(:reactable_id) - end - - def reaction_criteria - { user_id: user.id, reactable_type: "Article", category: "readinglist" } - end -end diff --git a/app/models/user.rb b/app/models/user.rb index 2d0423ab6..5b4d2b424 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -295,6 +295,14 @@ class User < ApplicationRecord end end + def cached_reading_list_article_ids + Rails.cache.fetch("reading_list_ids_of_articles_#{id}_#{public_reactions_count}") do + Reaction.readinglist.where( + user_id: id, reactable_type: "Article", + ).where.not(status: "archived").order(created_at: :desc).pluck(:reactable_id) + end + end + def preferred_languages_array return @preferred_languages_array if defined?(@preferred_languages_array) diff --git a/spec/labor/reading_list_spec.rb b/spec/labor/reading_list_spec.rb deleted file mode 100644 index cbe5c5e39..000000000 --- a/spec/labor/reading_list_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require "rails_helper" - -RSpec.describe ReadingList, type: :labor do - let(:user) { create(:user) } - let(:article) { create(:article) } - let(:article2) { create(:article) } - let(:article3) { create(:article) } - - def create_reaction(user, article) - Reaction.create!( - user_id: user.id, - reactable_id: article.id, - reactable_type: "Article", - category: "readinglist", - ) - end - - it "returns cached ids of articles that have been reacted to" do - create_reaction(user, article) - create_reaction(user, article2) - - expect(described_class.new(user).cached_ids_of_articles).to eq([article2.id, article.id]) - end -end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3b0d92446..998fd2e23 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1057,6 +1057,15 @@ RSpec.describe User, type: :model do user.follow(org) expect(user.reload.following_orgs_count).to eq(1) end + + it "returns cached ids of articles that have been saved to their readinglist" do + article = create(:article) + article2 = create(:article) + create(:reading_reaction, user: user, reactable: article) + create(:reading_reaction, user: user, reactable: article2) + + expect(user.cached_reading_list_article_ids).to eq([article2.id, article.id]) + end end describe "#org_admin?" do