def create
@report = current_user.reports.new(report_params)
- @report.issue = Issue.find_or_initialize_by(:reportable_id => params[:report][:issue][:reportable_id], :reportable_type => params[:report][:issue][:reportable_type])
+ @report.issue = Issue
+ .create_with(:assigned_role => default_assigned_role)
+ .find_or_initialize_by(issue_params)
if @report.save
- @report.issue.save
- @report.issue.reopen! unless @report.issue.open?
+ @report.issue.assigned_role = "administrator" if default_assigned_role == "administrator"
+ @report.issue.reopen unless @report.issue.open?
+ @report.issue.save!
+
redirect_to helpers.reportable_url(@report.issue.reportable), :notice => t(".successful_report")
else
redirect_to new_report_path(:reportable_type => @report.issue.reportable_type, :reportable_id => @report.issue.reportable_id), :notice => t(".provide_details")
end
def report_params
- params[:report].permit(:details, :category)
+ params.require(:report).permit(:details, :category)
+ end
+
+ def issue_params
+ params.require(:report).require(:issue).permit(:reportable_id, :reportable_type)
+ end
+
+ def default_assigned_role
+ case issue_params[:reportable_type]
+ when "Note" then "moderator"
+ when "User" then case report_params[:category]
+ when "vandal" then "moderator"
+ else "administrator"
+ end
+ else "administrator"
+ end
end
end
ASSIGNED_ROLES = %w[administrator moderator].freeze
validates :assigned_role, :presence => true, :inclusion => ASSIGNED_ROLES
- before_validation :set_default_assigned_role
before_validation :set_reported_user
scope :with_status, ->(issue_status) { where(:status => statuses[issue_status]) }
reportable.user
end
end
-
- def set_default_assigned_role
- if assigned_role.blank?
- self.assigned_role = case reportable
- when Note then "moderator"
- else "administrator"
- end
- end
- end
end
</div>
<div class='form-row'>
- <%= text_area :report, :details, :cols => 20, :rows => 5, placeholder: t('.details'), :required => true %>
+ <%= text_area :report, :details, :cols => 20, :rows => 5, placeholder: t('.details') %>
</div>
<div class='buttons'>
# Default to reporting users
association :reportable, :factory => :user
association :reported_user, :factory => :user
+
+ # Default to assigning to an administrator
+ assigned_role "administrator"
end
end
diary_entry = create(:diary_entry)
diary_comment = create(:diary_comment, :diary_entry => diary_entry)
- issue = Issue.new(:reportable => user)
+ issue = Issue.new(:reportable => user, :assigned_role => "administrator")
issue.save!
assert_equal issue.reported_user, user
- issue = Issue.new(:reportable => note)
+ issue = Issue.new(:reportable => note, :assigned_role => "administrator")
issue.save!
assert_equal issue.reported_user, note.author
- issue = Issue.new(:reportable => anonymous_note)
+ issue = Issue.new(:reportable => anonymous_note, :assigned_role => "administrator")
issue.save!
assert_nil issue.reported_user
- issue = Issue.new(:reportable => diary_entry)
+ issue = Issue.new(:reportable => diary_entry, :assigned_role => "administrator")
issue.save!
assert_equal issue.reported_user, diary_entry.user
- issue = Issue.new(:reportable => diary_comment)
+ issue = Issue.new(:reportable => diary_comment, :assigned_role => "administrator")
issue.save!
assert_equal issue.reported_user, diary_comment.user
end
-
- def test_default_assigned_role
- create(:language, :code => "en")
- user = create(:user)
- note = create(:note_with_comments)
- diary_entry = create(:diary_entry)
- diary_comment = create(:diary_comment, :diary_entry => diary_entry)
-
- issue = Issue.new(:reportable => user)
- issue.save!
- assert_equal "administrator", issue.assigned_role
-
- issue = Issue.new(:reportable => note)
- issue.save!
- assert_equal "moderator", issue.assigned_role
-
- issue = Issue.new(:reportable => diary_entry)
- issue.save!
- assert_equal "administrator", issue.assigned_role
-
- issue = Issue.new(:reportable => diary_comment)
- issue.save!
- assert_equal "administrator", issue.assigned_role
- end
-
- def test_no_default_explicit_role
- create(:language, :code => "en")
- user = create(:user)
- note = create(:note_with_comments)
- diary_entry = create(:diary_entry)
- diary_comment = create(:diary_comment, :diary_entry => diary_entry)
-
- issue = Issue.new(:reportable => user, :assigned_role => "moderator")
- issue.save!
- assert_equal "moderator", issue.reload.assigned_role
-
- issue = Issue.new(:reportable => note, :assigned_role => "administrator")
- issue.save!
- assert_equal "administrator", issue.reload.assigned_role
-
- issue = Issue.new(:reportable => diary_entry, :assigned_role => "moderator")
- issue.save!
- assert_equal "moderator", issue.reload.assigned_role
-
- issue = Issue.new(:reportable => diary_comment, :assigned_role => "moderator")
- issue.save!
- assert_equal "moderator", issue.reload.assigned_role
- end
end
def test_view_issues_with_no_reported_user
sign_in_as(create(:moderator_user))
anonymous_note = create(:note_with_comments)
- issue = create(:issue, :reportable => anonymous_note)
+ issue = create(:issue, :reportable => anonymous_note, :assigned_role => "moderator")
visit issues_path
assert page.has_content?(reportable_title(anonymous_note))
assert page.has_content? "Your report has been registered sucessfully"
assert_equal @comment, Issue.last.reportable
+ assert_equal "administrator", Issue.last.assigned_role
end
end
assert page.has_content? "Your report has been registered sucessfully"
assert_equal @diary_entry, Issue.last.reportable
+ assert_equal "administrator", Issue.last.assigned_role
end
def test_it_reopens_issue
assert page.has_content? "Your report has been registered sucessfully"
assert_equal note, Issue.last.reportable
+ assert_equal "moderator", Issue.last.assigned_role
end
def test_can_report_notes_with_author
assert page.has_content? "Your report has been registered sucessfully"
assert_equal note, Issue.last.reportable
+ assert_equal "moderator", Issue.last.assigned_role
end
end
assert page.has_content? "Your report has been registered sucessfully"
assert_equal user, Issue.last.reportable
+ assert_equal "moderator", Issue.last.assigned_role
+ end
+
+ def test_it_promotes_issues
+ user = create(:user)
+ sign_in_as(create(:user))
+ visit user_path(user.display_name)
+
+ click_on I18n.t("user.view.report")
+ assert page.has_content? "Report"
+ assert page.has_content? I18n.t("reports.new.disclaimer.intro")
+
+ choose I18n.t("reports.new.categories.user.vandal_label")
+ fill_in "report_details", :with => "This user is a vandal"
+ assert_difference "Issue.count", 1 do
+ click_on "Create Report"
+ end
+
+ assert page.has_content? "Your report has been registered sucessfully"
+
+ assert_equal user, Issue.last.reportable
+ assert_equal "moderator", Issue.last.assigned_role
+
+ visit user_path(user.display_name)
+
+ click_on I18n.t("user.view.report")
+ assert page.has_content? "Report"
+ assert page.has_content? I18n.t("reports.new.disclaimer.intro")
+
+ choose I18n.t("reports.new.categories.user.spam_label")
+ fill_in "report_details", :with => "This user is a spammer"
+ assert_no_difference "Issue.count" do
+ click_on "Create Report"
+ end
+
+ assert page.has_content? "Your report has been registered sucessfully"
+
+ assert_equal user, Issue.last.reportable
+ assert_equal "administrator", Issue.last.assigned_role
end
end