]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
Fixed tests + Altered migration file + Added reporting strings + Added update method
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :require_user
6   before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore,:comment]
7   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
8
9   def index
10     @issues = Issue.all
11   end
12
13   def show
14     @read_reports = @issue.read_reports
15     @unread_reports = @issue.unread_reports
16     @comments = @issue.comments
17     @related_issues = @issue.user.issues
18   end
19
20   def new
21     unless create_new_issue_params.blank?
22       @issue = Issue.find_or_initialize_by(create_new_issue_params)
23     end
24   end
25
26   def create
27     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
28     if !@issue 
29       @issue = Issue.find_or_initialize_by(issue_params)
30       @admins = UserRole.where(role: "administrator")
31       @admins.each do |admin|
32         Notifier.new_issue_notification(User.find(admin.user_id)).deliver_now
33       end
34     end
35     @report = @issue.reports.build(report_params)
36     details =  params[:report][:details].to_s + "||" + params[:spam].to_s + "||" + params[:offensive].to_s + "||" + params[:threat].to_s + "||" + params[:vandal].to_s + "||" + params[:other].to_s
37     @report.reporter_user_id = @user.id
38     @report.details = details
39     if @issue.save!
40       redirect_to root_path, notice: 'Your report has been registered sucessfully.'
41     else
42       render :new
43     end
44   end
45
46   def update
47     @issue = Issue.find_by(issue_params)
48     @report = @issue.reports.where(reporter_user_id: @user.id).first
49     details =  params[:report][:details].to_s + "||" + params[:spam].to_s + "||" + params[:offensive].to_s + "||" + params[:threat].to_s + "||" + params[:vandal].to_s + "||" + params[:other].to_s
50     @report.details = details    
51     if @report.save!
52       redirect_to root_path, notice: 'Your report was successfully updated.'
53     else
54       render :edit
55     end  
56   end
57
58   def comment
59     @issue = Issue.find(params[:id])
60     @issue_comment = @issue.comments.build(issue_comment_params)
61     @issue_comment.commenter_user_id = @user.id
62     @issue_comment.save!
63     redirect_to @issue
64   end
65
66   # Status Transistions
67   def resolve
68     if @issue.resolve
69       @issue.save!
70       redirect_to @issue, notice: "Issue status has been set to: 'Resolved'"
71     else
72       render :show
73     end
74   end
75
76   def ignore
77     if @issue.ignore
78       @issue.save!
79       redirect_to @issue, notice: "Issue status has been set to: 'Ignored'"
80     else
81       render :show
82     end
83   end
84
85   def reopen
86     if @issue.reopen
87       @issue.save!
88       redirect_to @issue, notice: "Issue status has been set to: 'Open'"
89     else
90       render :show
91     end
92   end
93
94   private
95
96     def find_issue
97       @issue = Issue.find(params[:id])
98     end
99
100     def check_permission
101       unless @user.administrator?
102         flash[:error] = t("application.require_admin.not_an_admin")
103         redirect_to root_path
104       end
105     end
106
107     def create_new_issue_params
108       params.permit(:reportable_id, :reportable_type, :reported_user_id)
109     end
110
111     def issue_params
112       params[:issue].permit(:reportable_id, :reportable_type,:reported_user_id)
113     end
114
115     def report_params
116       params[:report].permit(:details)
117     end
118
119     def issue_comment_params
120       params.require(:issue_comment).permit(:body)
121     end
122 end