]> git.openstreetmap.org Git - rails.git/commitdiff
Rename 'issue_type' to 'assigned_role', set defaults in the model and test.
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 6 Dec 2017 16:28:13 +0000 (16:28 +0000)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 6 Dec 2017 17:15:11 +0000 (17:15 +0000)
app/controllers/issues_controller.rb
app/models/issue.rb
db/migrate/20160822153055_create_issues_and_reports.rb
db/structure.sql
test/models/issue_test.rb
test/system/issues_test.rb

index edf8f1121cbf9b297eaf7ab10d9997961153bec6..25358796595306a8640469c33d8dc5a74507151b 100644 (file)
@@ -17,7 +17,7 @@ class IssuesController < ApplicationController
       @users = User.joins(:roles).where(:user_roles => { :role => "administrator" })
     end
 
-    @issues = Issue.where(:issue_type => @user_role)
+    @issues = Issue.where(:assigned_role => @user_role)
 
     # If search
     if params[:search_by_user] && params[:search_by_user].present?
@@ -55,7 +55,7 @@ class IssuesController < ApplicationController
     @read_reports = @issue.read_reports
     @unread_reports = @issue.unread_reports
     @comments = @issue.comments
-    @related_issues = @issue.reported_user.issues.where(:issue_type => @user_role)
+    @related_issues = @issue.reported_user.issues.where(:assigned_role => @user_role)
     @new_comment = IssueComment.new(:issue => @issue)
   end
 
@@ -125,7 +125,7 @@ class IssuesController < ApplicationController
 
   # Reassign Issues between Administrators and Moderators
   def reassign_issue
-    @issue.issue_type = upgrade_issue(@issue.issue_type)
+    @issue.assigned_role = upgrade_issue(@issue.assigned_role)
     @issue.save!
   end
 
index a08bfd6a4ca642d45130ab4ab35b0adf83e6914a..7294b07f50c2ad8da02084873d68631ff511dc76 100644 (file)
@@ -7,7 +7,7 @@
 #  reportable_id    :integer          not null
 #  reported_user_id :integer          not null
 #  status           :integer
-#  issue_type       :string
+#  assigned_role    :enum             not null
 #  resolved_at      :datetime
 #  resolved_by      :integer
 #  updated_by       :integer
@@ -38,6 +38,7 @@ class Issue < ActiveRecord::Base
   validates :reportable_id, :uniqueness => { :scope => [:reportable_type] }
   validates :reported_user_id, :presence => true
 
+  before_validation :set_default_assigned_role
   before_validation :set_reported_user
 
   # Check if more statuses are needed
@@ -89,4 +90,9 @@ class Issue < ActiveRecord::Base
                            reportable.user
                          end
   end
+
+  def set_default_assigned_role
+    role = %w[Changeset Note].include?(reportable.class.name) ? "moderator" : "administrator"
+    self.assigned_role = role if assigned_role.blank?
+  end
 end
index cdd3ea7c7b09bfa611b50b87c17aeea5bf3dd792..a9ba965544021d3eee70831c4ef5d06402231da4 100644 (file)
@@ -5,7 +5,7 @@ class CreateIssuesAndReports < ActiveRecord::Migration[5.0]
       t.integer :reportable_id, :null => false
       t.integer :reported_user_id, :null => false
       t.integer :status
-      t.string :issue_type
+      t.column :assigned_role, :user_role_enum, :null => false
       t.datetime :resolved_at
       t.integer :resolved_by
       t.integer :updated_by
index 742791df544f70baafa75f6aeb4fcfe3318a98da..f01cbfbbc7e6bf65f865931d8fce149d0076cb71 100644 (file)
@@ -728,7 +728,7 @@ CREATE TABLE issues (
     reportable_id integer NOT NULL,
     reported_user_id integer NOT NULL,
     status integer,
-    issue_type character varying,
+    assigned_role user_role_enum NOT NULL,
     resolved_at timestamp without time zone,
     resolved_by integer,
     updated_by integer,
@@ -2877,3 +2877,5 @@ INSERT INTO "schema_migrations" (version) VALUES
 ('7'),
 ('8'),
 ('9');
+
+
index 7ee700124c7448a84a6bc02fa08a59f66069a6cd..49649eede79c5279f9dad1e1d0afdf39d7d1d920 100644 (file)
@@ -21,4 +21,26 @@ class IssueTest < ActiveSupport::TestCase
     issue.save!
     assert_equal issue.reported_user, diary_entry.user
   end
+
+  def test_default_assigned_role
+    create(:language, :code => "en")
+    diary_entry = create(:diary_entry)
+    changeset = create(:changeset)
+
+    issue = Issue.new
+    issue.reportable = diary_entry
+    issue.save!
+    assert_equal "administrator", issue.assigned_role
+
+    issue = Issue.new
+    issue.reportable = changeset
+    issue.save!
+    assert_equal "moderator", issue.assigned_role
+
+    # check the callback doesn't override an explicitly set role
+    issue.assigned_role = "administrator"
+    issue.save!
+    issue.reload
+    assert_equal "administrator", issue.assigned_role
+  end
 end
index 9eb2fbbcb27925d047674fe55395dba573474b2f..ab3e619c908c4c2a3346783d1b61d71fd5c9bddb 100644 (file)
@@ -17,7 +17,7 @@ class IssuesTest < ApplicationSystemTestCase
 
   def test_view_issues
     sign_in_as(create(:moderator_user))
-    issues = create_list(:issue, 3, :issue_type => "moderator")
+    issues = create_list(:issue, 3, :assigned_role => "moderator")
 
     visit issues_path
     assert page.has_content?(issues.first.reported_user.display_name)
@@ -26,7 +26,7 @@ class IssuesTest < ApplicationSystemTestCase
   def test_search_issues_by_user
     good_user = create(:user)
     bad_user = create(:user)
-    create(:issue, :reportable => bad_user, :reported_user => bad_user, :issue_type => "administrator")
+    create(:issue, :reportable => bad_user, :reported_user => bad_user, :assigned_role => "administrator")
 
     sign_in_as(create(:administrator_user))