Create an IssueCommentsController for managing IssueComments

This commit is contained in:
Andy Allan 2017-09-13 16:19:16 +01:00
parent f0e3a41726
commit 8cf16fe78b
5 changed files with 76 additions and 22 deletions

View file

@ -0,0 +1,33 @@
class IssueCommentsController < ApplicationController
layout "site"
before_action :authorize_web
before_action :require_user
before_action :check_permission
def create
@issue = Issue.find(params[:issue_id])
comment = @issue.comments.build(issue_comment_params)
comment.user = current_user
# if params[:reassign]
# reassign_issue
# @issue_comment.reassign = true
# end
comment.save!
notice = t("issues.comment.comment_created")
redirect_to @issue, :notice => notice
end
private
def issue_comment_params
params.require(:issue_comment).permit(:body)
end
def check_permission
unless current_user.administrator? || current_user.moderator?
flash[:error] = t("application.require_admin.not_an_admin")
redirect_to root_path
end
end
end

View file

@ -58,6 +58,7 @@ class IssuesController < ApplicationController
@unread_reports = @issue.unread_reports
@comments = @issue.comments
@related_issues = @issue.reported_user.issues.where(:issue_type => @user_role)
@new_comment = IssueComment.new(:issue => @issue)
end
def update
@ -94,25 +95,6 @@ class IssuesController < ApplicationController
end
end
def comment
@issue = Issue.find(params[:id])
if issue_comment_params.blank?
notice = t("issues.comment.provide_details")
else
@issue_comment = @issue.comments.build(issue_comment_params)
@issue_comment.commenter_user_id = current_user.id
if params[:reassign]
reassign_issue
@issue_comment.reassign = true
end
@issue_comment.save!
@issue.updated_by = current_user.id
@issue.save!
notice = t("issues.comment.comment_created")
end
redirect_to @issue, :notice => notice
end
# Status Transistions
def resolve
if @issue.resolve

View file

@ -20,7 +20,7 @@
</div>
<br/>
<div class="comment">
<%= form_for :issue_comment, :url => { :action => 'comment', :id => @issue.id } do |f| %>
<%= form_for @new_comment, url: issue_comments_path(@issue) do |f| %>
<%= richtext_area :issue_comment, :body, :cols => 10, :rows => 8, :required => true %>
<%= label_tag t('issues.show.comments.reassign_param') %> <%= check_box_tag :reassign, true %>
<br/>

View file

@ -292,6 +292,7 @@ OpenStreetMap::Application.routes.draw do
# issues and reports
resources :issues do
resources :comments, :controller => :issue_comments
member do
post "resolve"
post "assign"
@ -302,8 +303,6 @@ OpenStreetMap::Application.routes.draw do
resources :reports
post "/comment" => "issues#comment"
# redactions
resources :redactions
end

View file

@ -0,0 +1,40 @@
require "test_helper"
class IssuesTest < Capybara::Rails::TestCase
def test_view_issues_normal_user
sign_in_as(create(:user))
visit issues_path
assert page.has_content?(I18n.t("application.require_admin.not_an_admin"))
end
def test_view_no_issues
sign_in_as(create(:moderator_user))
visit issues_path
assert page.has_content?(I18n.t(".issues.index.search.issues_not_found"))
end
def test_view_issues
sign_in_as(create(:moderator_user))
issues = create_list(:issue, 3, :issue_type => "moderator")
visit issues_path
assert page.has_content?(issues.first.reported_user.display_name)
end
def test_commenting
issue = create(:issue)
sign_in_as(create(:moderator_user))
visit issue_path(issue)
fill_in :issue_comment_body, :with => "test comment"
click_on "Submit"
assert page.has_content?(I18n.t(".issues.comment.comment_created"))
assert page.has_content?("test comment")
issue.reload
assert_equal issue.comments.first.body, "test comment"
end
end