Skip to content

Commit

Permalink
✨ create feed list
Browse files Browse the repository at this point in the history
  • Loading branch information
karinevieira committed Jul 14, 2024
1 parent 8fee0e9 commit c72b605
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
22 changes: 22 additions & 0 deletions app/operations/posts/feed_list.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion app/operations/posts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions spec/operations/posts/feed_list_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/operations/posts/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c72b605

Please sign in to comment.