environment_notification_plugin_admin_controller.rb
2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class EnvironmentNotificationPluginAdminController < AdminController
before_filter :admin_required, :except => [:hide_notification]
def index
@notifications = environment.environment_notifications.order('updated_at DESC')
end
def new
@notification = EnvironmentNotificationPlugin::EnvironmentNotification.new
if request.post?
@notification = EnvironmentNotificationPlugin::EnvironmentNotification.new(params[:notifications])
@notification.message = @notification.message.html_safe
@notification.environment_id = environment.id
if @notification.save
session[:notice] = _("Notification successfully created")
redirect_to :action => :index
else
session[:notice] = _("Notification couldn't be created")
end
end
end
def destroy
if request.delete?
notification = environment.environment_notifications.find_by_id(params[:id])
if notification && notification.destroy
session[:notice] = _('The notification was deleted.')
else
session[:notice] = _('Could not remove the notification')
end
end
redirect_to :action => :index
end
def edit
@notification = environment.environment_notifications.find_by_id(params[:id])
if request.post?
if @notification.update_attributes(params[:notifications])
session[:notice] = _('The notification was edited.')
else
session[:notice] = _('Could not edit the notification.')
end
redirect_to :action => :index
end
end
def change_status
@notification = environment.environment_notifications.find_by_id(params[:id])
@notification.active = !@notification.active
if @notification.save!
session[:notice] = _('The status of the notification was changed.')
else
session[:notice] = _('Could not change the status of the notification.')
end
redirect_to :action => :index
end
def hide_notification
result = false
if logged_in?
@notification = environment.environment_notifications.find_by_id(params[:notification_id])
if @notification
@notification.users << current_user
result = @notification.users.include?(current_user)
end
end
render json: result
end
protected
def admin_required
redirect_to :root unless current_user.person.is_admin?
end
end