From c72b60527785f8130fddbed99a4f873dfaace7a0 Mon Sep 17 00:00:00 2001 From: Karine Vieira Date: Sun, 14 Jul 2024 15:59:37 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20create=20feed=20list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/posts_controller.rb | 2 +- app/operations/posts/feed_list.rb | 22 ++++++++++++++++++++++ app/operations/posts/list.rb | 4 +++- spec/operations/posts/feed_list_spec.rb | 23 +++++++++++++++++++++++ spec/operations/posts/list_spec.rb | 4 ++-- 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 app/operations/posts/feed_list.rb create mode 100644 spec/operations/posts/feed_list_spec.rb diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index f028f26..e7bcec4 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -4,7 +4,7 @@ class PostsController < ApplicationController before_action :authorize_action!, only: %i[edit update destroy] def index - result = Posts::List.result + result = Posts::FeedList.result(current_user: current_user) respond_to do |format| format.html { render Posts::IndexPage.new(posts: result.posts, user: current_user) } diff --git a/app/operations/posts/feed_list.rb b/app/operations/posts/feed_list.rb new file mode 100644 index 0000000..f97dfdc --- /dev/null +++ b/app/operations/posts/feed_list.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Posts + class FeedList < Actor + input :current_user, type: User + + output :posts, type: Enumerable + + def call + feed_posts = Post.where(user_id: feed_user_ids) + + self.posts = Posts::List.result(scope: feed_posts).posts + end + + private + + def feed_user_ids + following_users = current_user.following.pluck(:id) + following_users.push(current_user.id) + end + end +end diff --git a/app/operations/posts/list.rb b/app/operations/posts/list.rb index dc806fa..fbbc5a5 100644 --- a/app/operations/posts/list.rb +++ b/app/operations/posts/list.rb @@ -2,10 +2,12 @@ module Posts class List < Actor + input :scope, type: ActiveRecord::Relation + output :posts, type: Enumerable def call - self.posts = Post.order(created_at: :desc).all + self.posts = scope.order(created_at: :desc) end end end diff --git a/spec/operations/posts/feed_list_spec.rb b/spec/operations/posts/feed_list_spec.rb new file mode 100644 index 0000000..d39876c --- /dev/null +++ b/spec/operations/posts/feed_list_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Posts::FeedList, type: :operation do + describe ".result" do + it "returns posts from the user and their followed users" do + current_user = create(:user) + followed_user = create(:user) + not_followed_user = create(:user) + + create(:follow, follower: current_user, followed: followed_user) + + post_a = create(:post, user: followed_user) + _post_b = create(:post, user: not_followed_user) + post_c = create(:post, user: current_user) + + result = described_class.result(current_user: current_user) + + expect(result.posts).to contain_exactly(post_a, post_c) + end + end +end diff --git a/spec/operations/posts/list_spec.rb b/spec/operations/posts/list_spec.rb index 279cdf2..ea3a765 100644 --- a/spec/operations/posts/list_spec.rb +++ b/spec/operations/posts/list_spec.rb @@ -5,7 +5,7 @@ RSpec.describe Posts::List, type: :operation do describe ".result" do it "is successful" do - result = described_class.result + result = described_class.result(scope: Post.all) expect(result.success?).to be true end @@ -15,7 +15,7 @@ second_post = create(:post, subtitle: "Second post") third_post = create(:post, subtitle: "Third post") - result = described_class.result + result = described_class.result(scope: Post.all) expect(result.posts.to_a).to eq [third_post, second_post, first_post] end