basic UI for reporting diary entries,diary entry comments and user profiles
[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   end
18
19   def new
20     unless create_new_issue_params.blank?
21       @issue = Issue.find_or_initialize_by(create_new_issue_params)
22       puts params[:user_id].to_s + "--------------"
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     @report.user_id = @user.id
37     if @issue.save!
38       redirect_to root_path, notice: 'Issue was successfully created.'
39     else
40       render :new
41     end
42   end
43
44   def comment
45     @issue = Issue.find(params[:id])
46     @issue_comment = @issue.comments.build(issue_comment_params)
47     @issue_comment.user_id = @user.id
48     @issue_comment.save!
49     redirect_to @issue
50   end
51
52   # Status Transistions
53   def resolve
54     if @issue.resolve
55       @issue.save!
56       redirect_to @issue, notice: "Issue status has been set to: 'Resolved'"
57     else
58       render :show
59     end
60   end
61
62   def ignore
63     if @issue.ignore
64       @issue.save!
65       redirect_to @issue, notice: "Issue status has been set to: 'Ignored'"
66     else
67       render :show
68     end
69   end
70
71   def reopen
72     if @issue.reopen
73       @issue.save!
74       redirect_to @issue, notice: "Issue status has been set to: 'Open'"
75     else
76       render :show
77     end
78   end
79
80   private
81
82     def find_issue
83       @issue = Issue.find(params[:id])
84     end
85
86     def check_permission
87       unless @user.administrator?
88         flash[:error] = t("application.require_admin.not_an_admin")
89         redirect_to root_path
90       end
91     end
92
93     def create_new_issue_params
94       params.permit(:reportable_id, :reportable_type, :user_id)
95     end
96
97     def issue_params
98       params[:issue].permit(:reportable_id, :reportable_type,:user_id)
99     end
100
101     def report_params
102       params[:report].permit(:details)
103     end
104
105     def issue_comment_params
106       params.require(:issue_comment).permit(:body, :user_id)
107     end
108 end