0479e66418f805d3f69d4a4e2e3673729bb58e89
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
4   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
5
6   def index
7     @issues = Issue.all
8   end
9
10   def show
11     @read_reports = @issue.read_reports
12     @unread_reports = @issue.unread_reports
13   end
14
15   def new
16     unless create_new_issue_params.blank?
17       @issue = Issue.find_or_initialize_by(create_new_issue_params)
18     end
19   end
20
21   def create
22     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
23     if !@issue 
24       @issue = Issue.find_or_initialize_by(issue_params)
25       @admins = UserRole.where(role: "administrator")
26       @admins.each do |user|
27         Notifier.new_issue_notification(User.find(user.user_id)).deliver_now
28       end
29     end
30
31     @report = @issue.reports.build(report_params)
32
33     if @issue.save
34       redirect_to @issue, notice: 'Issue was successfully created.'
35     else
36       render :new
37     end
38   end
39
40   # Status Transistions
41   def resolve
42     if @issue.resolve
43       @issue.save!
44       redirect_to @issue, notice: "Issue status has been set to: 'Resolved'"
45     else
46       render :show
47     end
48   end
49
50   def ignore
51     if @issue.ignore
52       @issue.save!
53       redirect_to @issue, notice: "Issue status has been set to: 'Ignored'"
54     else
55       render :show
56     end
57   end
58
59   def reopen
60     if @issue.reopen
61       @issue.save!
62       redirect_to @issue, notice: "Issue status has been set to: 'Open'"
63     else
64       render :show
65     end
66   end
67
68   private
69
70     def find_issue
71       @issue = Issue.find(params[:id])
72     end
73
74     def create_new_issue_params
75       params.permit(:reportable_id, :reportable_type, :user_id)
76     end
77
78     def issue_params
79       params[:issue].permit(:reportable_id, :reportable_type,:user_id)
80     end
81
82     def report_params
83       params[:report].permit(:details)
84     end
85 end