From: Andy Allan Date: Wed, 13 Sep 2017 15:19:16 +0000 (+0100) Subject: Create an IssueCommentsController for managing IssueComments X-Git-Tag: live~2981^2~93 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/8cf16fe78b85743f284413fd8542f890bd6865fa?ds=inline Create an IssueCommentsController for managing IssueComments --- diff --git a/app/controllers/issue_comments_controller.rb b/app/controllers/issue_comments_controller.rb new file mode 100644 index 000000000..ba35b7978 --- /dev/null +++ b/app/controllers/issue_comments_controller.rb @@ -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 diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 534cc2fde..e156ea004 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -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 diff --git a/app/views/issues/_comments.html.erb b/app/views/issues/_comments.html.erb index a12e46237..36a5ec43d 100644 --- a/app/views/issues/_comments.html.erb +++ b/app/views/issues/_comments.html.erb @@ -20,7 +20,7 @@
- <%= 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 %>
diff --git a/config/routes.rb b/config/routes.rb index 469478969..277bd34ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/test/features/issues_test.rb b/test/features/issues_test.rb new file mode 100644 index 000000000..04ff7f5a6 --- /dev/null +++ b/test/features/issues_test.rb @@ -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