Merge pull request #1429 from Retrospring/task/refactor-comment-controller

This commit is contained in:
Andreas Nedbal 2023-10-29 21:58:07 +01:00 committed by GitHub
commit 089bfc7350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 41 deletions

View File

@ -1,14 +1,7 @@
# frozen_string_literal: true
class CommentController < ApplicationController
class Comments::ReactionsController < ApplicationController
def index
answer = Answer.find(params[:id])
@comments = Comment.where(answer:).includes([{ user: :profile }, :smiles])
render "index", locals: { a: answer }
end
def show_reactions
comment = Comment.find(params[:id])
@reactions = Reaction.where(parent_type: "Comment", parent: comment.id).includes([{ user: :profile }])

View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
class CommentsController < ApplicationController
def index
answer = Answer.find(params[:id])
@comments = Comment.where(answer:).includes([{ user: :profile }, :smiles])
render "index", locals: { a: answer }
end
end

View File

@ -154,10 +154,10 @@ Rails.application.routes.draw do
get "/@:username/a/:id", to: "answer#show", as: :answer
post "/@:username/a/:id/pin", to: "answer#pin", as: :pin_answer
delete "/@:username/a/:id/pin", to: "answer#unpin", as: :unpin_answer
get "/@:username/a/:id/comments", to: "comment#index", as: :comments
get "/@:username/a/:id/comments", to: "comments#index", as: :comments
get "/@:username/a/:id/reactions", to: "reactions#index", as: :reactions
get "/@:username/q/:id", to: "question#show", as: :question
get "/@:username/c/:id/reactions", to: "comment#show_reactions", as: :comment_reactions
get "/@:username/c/:id/reactions", to: "comments/reactions#index", as: :comment_reactions
get "/@:username/followers", to: "user#followers", as: :show_user_followers
get "/@:username/followings", to: "user#followings", as: :show_user_followings
get "/@:username/friends", to: redirect("/@%{username}/followings")

View File

@ -0,0 +1,34 @@
# frozen_string_literal: true
require "rails_helper"
describe Comments::ReactionsController, type: :controller do
describe "#index" do
let(:answer_author) { FactoryBot.create(:user) }
let(:answer) { FactoryBot.create(:answer, user: answer_author) }
let(:commenter) { FactoryBot.create(:user) }
let(:comment) { FactoryBot.create(:comment, answer:, user: commenter) }
context "a regular web navigation request" do
subject { get :index, params: { username: commenter.screen_name, id: comment.id } }
it "should redirect to the answer page" do
subject
expect(response).to redirect_to answer_path(username: answer_author.screen_name, id: answer.id)
end
end
context "a Turbo Frame request" do
subject { get :index, params: { username: commenter.screen_name, id: comment.id } }
it "renders the index template" do
@request.headers["Turbo-Frame"] = "some_id"
subject
expect(response).to render_template(:index)
end
end
end
end

View File

@ -2,12 +2,12 @@
require "rails_helper"
describe CommentController, type: :controller do
describe CommentsController, type: :controller do
describe "#index" do
shared_examples_for "succeeds" do
it "returns the correct response" do
subject
expect(response).to have_rendered("comment/index")
expect(response).to have_rendered :index
expect(response).to have_http_status(200)
expect(assigns(:comments)).to eq(comments)
expect(assigns(:comments)).to_not include(unrelated_comment)
@ -34,33 +34,4 @@ describe CommentController, type: :controller do
end
end
end
describe "#show_reactions" do
let(:answer_author) { FactoryBot.create(:user) }
let(:answer) { FactoryBot.create(:answer, user: answer_author) }
let(:commenter) { FactoryBot.create(:user) }
let(:comment) { FactoryBot.create(:comment, answer:, user: commenter) }
context "a regular web navigation request" do
subject { get :show_reactions, params: { username: commenter.screen_name, id: comment.id } }
it "should redirect to the answer page" do
subject
expect(response).to redirect_to answer_path(username: answer_author.screen_name, id: answer.id)
end
end
context "a Turbo Frame request" do
subject { get :show_reactions, params: { username: commenter.screen_name, id: comment.id } }
it "renders the show_reaction template" do
@request.headers["Turbo-Frame"] = "some_id"
subject
expect(response).to render_template(:show_reactions)
end
end
end
end