diff --git a/app/controllers/comment_controller.rb b/app/controllers/comments/reactions_controller.rb similarity index 57% rename from app/controllers/comment_controller.rb rename to app/controllers/comments/reactions_controller.rb index b3e12514..0efe2c2a 100644 --- a/app/controllers/comment_controller.rb +++ b/app/controllers/comments/reactions_controller.rb @@ -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 }]) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 00000000..25f4b435 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/views/comment/index.html.haml b/app/views/comments/index.html.haml similarity index 100% rename from app/views/comment/index.html.haml rename to app/views/comments/index.html.haml diff --git a/app/views/comment/show_reactions.html.haml b/app/views/comments/reactions/index.html.haml similarity index 100% rename from app/views/comment/show_reactions.html.haml rename to app/views/comments/reactions/index.html.haml diff --git a/config/routes.rb b/config/routes.rb index 3161f9f8..fabc4263 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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") diff --git a/spec/controllers/comments/reactions_controller_spec.rb b/spec/controllers/comments/reactions_controller_spec.rb new file mode 100644 index 00000000..083b89b5 --- /dev/null +++ b/spec/controllers/comments/reactions_controller_spec.rb @@ -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 diff --git a/spec/controllers/comment_controller_spec.rb b/spec/controllers/comments_controller_spec.rb similarity index 51% rename from spec/controllers/comment_controller_spec.rb rename to spec/controllers/comments_controller_spec.rb index 63812485..209c1886 100644 --- a/spec/controllers/comment_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -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