-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ROAD-546: Add comments to stories (#304)
* Adds comment model and controller * Adds routes * Adds view * Adds empty comment and comments to stories controller * Adds has_many comment association * ROAD-547 adjusting edit and delete actions for comments * cleanup of controller due to test failures * Adds specs for comments * ROAD-548 add support to export comments optionally * [wip] adding controller specs * Add user to comment * Adds specs to test that a user cannot edit, delete, or update another user's comments * Address review's feedback * Add flash for delete - add redirect projects when not found * Removes respond_to blocks * adds comments_headers to avoid appending headers to constant * ROAD-546 comment_headers * Fix partial usage with local variables, and fix csv export - add spec with more than one comment * Adjust partials * Fixes typo * fix issue of comments containing comments of previous story * improve csv export test case * ROAD-548 adjust csv header comment loop * Remove unused comment_id * Undisables the submit button --------- Co-authored-by: Henrique Medeiros <[email protected]>
- Loading branch information
Showing
18 changed files
with
347 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class CommentsController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :load_story_and_project | ||
before_action :find_comment, only: [:edit, :update, :destroy] | ||
|
||
def edit | ||
end | ||
|
||
def create | ||
@comment = current_user.comments.build(story: @story) | ||
@comment.attributes = comment_params | ||
saved = @comment.save | ||
if saved | ||
flash[:success] = "Comment created!" | ||
else | ||
flash[:error] = @comment.errors.full_messages | ||
end | ||
|
||
redirect_to project_story_path(@comment.story.project_id, @comment.story_id) | ||
end | ||
|
||
def update | ||
updated = @comment.update(comment_params) | ||
if updated | ||
flash[:success] = "Comment updated!" | ||
redirect_to project_story_path(@comment.story.project_id, @comment.story_id) | ||
else | ||
flash[:error] = @comment.errors.full_messages | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
@comment.destroy | ||
flash[:success] = "Comment deleted!" | ||
redirect_to project_story_path(@comment.story.project_id, @comment.story_id) | ||
end | ||
|
||
private | ||
|
||
def find_comment | ||
@comment = current_user.comments.where(story_id: params[:story_id]).find(params[:id]) | ||
rescue ActiveRecord::RecordNotFound | ||
flash[:error] = "Comment not found" | ||
redirect_to project_story_path(params[:project_id], params[:story_id]) | ||
end | ||
|
||
def load_story_and_project | ||
@project = Project.find(params[:project_id]) | ||
@story = Story.find(params[:story_id]) | ||
rescue ActiveRecord::RecordNotFound | ||
flash[:error] = "Project or Story not found" | ||
redirect_to projects_path | ||
end | ||
|
||
def comment_params | ||
params.require(:comment).permit(:body) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Comment < ApplicationRecord | ||
belongs_to :story | ||
belongs_to :user | ||
validates :body, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ class User < ApplicationRecord | |
include OmbuLabsAuthenticable | ||
|
||
has_many :estimates | ||
has_many :comments | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="comment-card"> | ||
<p class="bold"><%= comment.user.name %>: <%= markdown(comment.body) %> <%= comment.created_at %><p> | ||
<% if current_user == comment.user %> | ||
<%= link_to "Edit Comment", edit_project_story_comment_path(project, story, comment), class: "link-blue" %> | | ||
<%= link_to "Delete", project_story_comment_path(project, story, comment), method: :delete, data: { confirm: "Are you sure?" }, title: "Delete" %> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= form_with model: [project, story, comment] do |form| %> | ||
<%= form.text_area :body, rows: 6 %> | ||
<%= form.submit class: "button green"%> | ||
<%= link_to "Back", project_story_path(project, story), id: "back", class: "button" if ["edit", "update"].include?(action_name) %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div class="container"> | ||
<h2 class="new-edit-title">Edit Comment</h2> | ||
<%= render partial: "form", locals: {story: @story, comment: @comment, project: @project} %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateComments < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :comments do |t| | ||
t.text :body | ||
t.integer :story_id | ||
t.integer :user_id | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe CommentsController, type: :controller do | ||
render_views | ||
|
||
let!(:user) { FactoryBot.create(:user) } | ||
let!(:project) { FactoryBot.create(:project) } | ||
let!(:story) { FactoryBot.create(:story, project: project) } | ||
let!(:comment) { FactoryBot.create(:comment, story: story, user: user) } | ||
|
||
before do | ||
@request.env["devise.mapping"] = Devise.mappings[:user] | ||
sign_in user | ||
end | ||
|
||
describe "#create" do | ||
context "with valid attributes" do | ||
let(:valid_params) { FactoryBot.attributes_for(:comment) } | ||
|
||
it "creates a new comment" do | ||
expect { | ||
post :create, params: {project_id: project.id, story_id: story.id, comment: valid_params} | ||
}.to change(Comment, :count).by(1) | ||
end | ||
|
||
it "redirects to the story path" do | ||
post :create, params: {project_id: project.id, story_id: story.id, comment: valid_params} | ||
|
||
expect(response).to redirect_to project_story_path(project.id, story.id) | ||
end | ||
end | ||
|
||
context "with invalid attributes" do | ||
let(:invalid_params) { {body: ""} } | ||
|
||
it "redirects back to the story page" do | ||
post :create, params: {project_id: project.id, story_id: story.id, comment: invalid_params} | ||
expect(response).to redirect_to project_story_path(project.id, story.id) | ||
end | ||
end | ||
end | ||
|
||
describe "#destroy" do | ||
it "deletes the comment" do | ||
delete :destroy, params: {project_id: project.id, story_id: story.id, id: comment.id} | ||
expect(Comment.exists?(comment.id)).to be_falsey | ||
expect(response).to redirect_to project_story_path(project.id, story.id) | ||
end | ||
|
||
it "disallows destroying another users' comment" do | ||
user2 = FactoryBot.create(:user) | ||
comment2 = FactoryBot.create(:comment, story: story, user: user2) | ||
|
||
delete :destroy, params: {id: comment2.id, story_id: story.id, project_id: project.id} | ||
|
||
expect(response).to redirect_to project_story_path(project.id, story.id) | ||
expect(flash[:error]).to eq "Comment not found" | ||
end | ||
end | ||
|
||
describe "#edit" do | ||
before do | ||
get :edit, params: {id: comment.id, story_id: story.id, project_id: project.id} | ||
end | ||
|
||
it "redirects to the edit page" do | ||
expect(response).to render_template :edit | ||
end | ||
|
||
it "shows the fields for the comment" do | ||
expect(assigns(:comment)).to eq comment | ||
end | ||
end | ||
|
||
describe "#edit as other user" do | ||
it "disallows editing another users' comment" do | ||
user2 = FactoryBot.create(:user) | ||
comment2 = FactoryBot.create(:comment, story: story, user: user2) | ||
|
||
get :edit, params: {id: comment2.id, story_id: story.id, project_id: project.id} | ||
|
||
expect(response).to redirect_to project_story_path(project.id, story.id) | ||
expect(flash[:error]).to eq "Comment not found" | ||
end | ||
end | ||
|
||
describe "#update" do | ||
it "updates the body for the comment" do | ||
put :update, params: { | ||
id: comment.id, | ||
story_id: story.id, | ||
project_id: project.id, | ||
comment: { | ||
body: "test123" | ||
} | ||
} | ||
expect(comment.reload.body).to eq "test123" | ||
end | ||
|
||
it "disallows updating another users' comment" do | ||
user2 = FactoryBot.create(:user) | ||
comment2 = FactoryBot.create(:comment, story: story, user: user2) | ||
|
||
put :update, params: {id: comment2.id, | ||
story_id: story.id, | ||
project_id: project.id, | ||
comment: {body: "test123"}} | ||
|
||
expect(response).to redirect_to project_story_path(project.id, story.id) | ||
expect(flash[:error]).to eq "Comment not found" | ||
end | ||
end | ||
end |
Oops, something went wrong.