Create an IssueCommentsController for managing IssueComments
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 13 Sep 2017 15:19:16 +0000 (16:19 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 13 Sep 2017 15:19:16 +0000 (16:19 +0100)
app/controllers/issue_comments_controller.rb [new file with mode: 0644]
app/controllers/issues_controller.rb
app/views/issues/_comments.html.erb
config/routes.rb
test/features/issues_test.rb [new file with mode: 0644]

diff --git a/app/controllers/issue_comments_controller.rb b/app/controllers/issue_comments_controller.rb
new file mode 100644 (file)
index 0000000..ba35b79
--- /dev/null
@@ -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
index 534cc2f..e156ea0 100644 (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
index a12e462..36a5ec4 100644 (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/>
index 4694789..277bd34 100644 (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
diff --git a/test/features/issues_test.rb b/test/features/issues_test.rb
new file mode 100644 (file)
index 0000000..04ff7f5
--- /dev/null
@@ -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