Commit fc719411e6a291f5f01f307d4b0a7d8b03218b3f
1 parent
413e4545
Exists in
community_notifications
Refactor environment notification controllers
Showing
6 changed files
with
45 additions
and
199 deletions
Show diff stats
plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb
1 | class EnvironmentNotificationPluginAdminController < AdminController | 1 | class EnvironmentNotificationPluginAdminController < AdminController |
2 | 2 | ||
3 | - helper EnvironmentNotificationHelper | ||
4 | - include EnvironmentNotificationHelper | 3 | + include NotificationManager |
5 | 4 | ||
6 | - before_filter :admin_required, :except => [:close_notification, :hide_notification] | ||
7 | - | ||
8 | - def index | ||
9 | - @notifications = environment.environment_notifications.order('updated_at DESC') | ||
10 | - end | ||
11 | - | ||
12 | - def new | ||
13 | - @notification = EnvironmentNotificationPlugin::EnvironmentNotification.new | ||
14 | - if request.post? | ||
15 | - @notification = EnvironmentNotificationPlugin::EnvironmentNotification.new(params[:notifications]) | ||
16 | - @notification.message = @notification.message.html_safe | ||
17 | - @notification.target = environment | ||
18 | - if @notification.save | ||
19 | - session[:notice] = _("Notification successfully created") | ||
20 | - redirect_to :action => :index | ||
21 | - else | ||
22 | - session[:notice] = _("Notification couldn't be created") | ||
23 | - end | ||
24 | - end | ||
25 | - end | ||
26 | - | ||
27 | - def destroy | ||
28 | - if request.delete? | ||
29 | - notification = environment.environment_notifications.find_by_id(params[:id]) | ||
30 | - if notification && notification.destroy | ||
31 | - session[:notice] = _('The notification was deleted.') | ||
32 | - else | ||
33 | - session[:notice] = _('Could not remove the notification') | ||
34 | - end | ||
35 | - end | ||
36 | - redirect_to :action => :index | ||
37 | - end | ||
38 | - | ||
39 | - def edit | ||
40 | - @notification = environment.environment_notifications.find_by_id(params[:id]) | ||
41 | - if request.post? | ||
42 | - if @notification.update_attributes(params[:notifications]) | ||
43 | - session[:notice] = _('The notification was edited.') | ||
44 | - else | ||
45 | - session[:notice] = _('Could not edit the notification.') | ||
46 | - end | ||
47 | - redirect_to :action => :index | ||
48 | - end | ||
49 | - end | ||
50 | - | ||
51 | - def change_status | ||
52 | - @notification = environment.environment_notifications.find_by_id(params[:id]) | ||
53 | - | ||
54 | - @notification.active = !@notification.active | ||
55 | - | ||
56 | - if @notification.save! | ||
57 | - session[:notice] = _('The status of the notification was changed.') | ||
58 | - else | ||
59 | - session[:notice] = _('Could not change the status of the notification.') | ||
60 | - end | ||
61 | - | ||
62 | - redirect_to :action => :index | ||
63 | - end | ||
64 | - | ||
65 | - def close_notification | ||
66 | - result = false | ||
67 | - | ||
68 | - if logged_in? | ||
69 | - @notification = environment.environment_notifications.find_by_id(params[:notification_id]) | ||
70 | - | ||
71 | - if @notification | ||
72 | - @notification.users << current_user | ||
73 | - result = @notification.users.include?(current_user) | ||
74 | - end | ||
75 | - end | ||
76 | - | ||
77 | - render json: result | ||
78 | - end | ||
79 | - | ||
80 | - def hide_notification | ||
81 | - result = false | ||
82 | - | ||
83 | - if logged_in? | ||
84 | - @notification = environment.environment_notifications.find_by_id(params[:notification_id]) | ||
85 | - | ||
86 | - if @notification | ||
87 | - current_notificaions = [] | ||
88 | - current_notificaions = JSON.parse(cookies[:hide_notifications]) unless cookies[:hide_notifications].blank? | ||
89 | - current_notificaions << @notification.id unless current_notificaions.include? @notification.id | ||
90 | - cookies[:hide_notifications] = JSON.generate(current_notificaions) | ||
91 | - result = current_notificaions.include? @notification.id | ||
92 | - end | ||
93 | - end | ||
94 | - | ||
95 | - render json: result | ||
96 | - end | 5 | + before_filter :admin_required |
97 | 6 | ||
98 | protected | 7 | protected |
99 | - def admin_required | ||
100 | - redirect_to :root unless current_user.person.is_admin? | 8 | + def target |
9 | + environment | ||
101 | end | 10 | end |
102 | 11 | ||
103 | end | 12 | end |
plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb
1 | class EnvironmentNotificationPluginMyprofileController < MyProfileController | 1 | class EnvironmentNotificationPluginMyprofileController < MyProfileController |
2 | 2 | ||
3 | - helper EnvironmentNotificationHelper | ||
4 | - include EnvironmentNotificationHelper | 3 | + include NotificationManager |
5 | 4 | ||
6 | - before_filter :admin_required, :except => [:close_notification, :hide_notification] | ||
7 | - #TODO test new notification when profile is not an organization | ||
8 | - | ||
9 | - def index | ||
10 | - @notifications = profile.environment_notifications.order('updated_at DESC') | ||
11 | - end | ||
12 | - | ||
13 | - def new | ||
14 | - @notification = EnvironmentNotificationPlugin::EnvironmentNotification.new | ||
15 | - if request.post? && profile && profile.organization? | ||
16 | - @notification = EnvironmentNotificationPlugin::EnvironmentNotification.new(params[:notifications]) | ||
17 | - @notification.message = @notification.message.html_safe | ||
18 | - @notification.target = profile | ||
19 | - if @notification.save | ||
20 | - session[:notice] = _("Notification successfully created for profile %s" % profile.name) | ||
21 | - redirect_to :action => :index | ||
22 | - else | ||
23 | - session[:notice] = _("Notification couldn't be created") | ||
24 | - end | ||
25 | - end | ||
26 | - end | ||
27 | - | ||
28 | - def destroy | ||
29 | - if request.delete? | ||
30 | - notification = profile.environment_notifications.find_by_id(params[:id]) | ||
31 | - if notification && notification.destroy | ||
32 | - session[:notice] = _('The notification was deleted.') | ||
33 | - else | ||
34 | - session[:notice] = _('Could not remove the notification') | ||
35 | - end | ||
36 | - end | ||
37 | - redirect_to :action => :index | ||
38 | - end | ||
39 | - | ||
40 | - def edit | ||
41 | - @notification = profile.environment_notifications.find_by_id(params[:id]) | ||
42 | - if request.post? | ||
43 | - if @notification.update_attributes(params[:notifications]) | ||
44 | - session[:notice] = _('The notification was edited.') | ||
45 | - else | ||
46 | - session[:notice] = _('Could not edit the notification.') | ||
47 | - end | ||
48 | - redirect_to :action => :index | ||
49 | - end | ||
50 | - end | ||
51 | - | ||
52 | - def change_status | ||
53 | - @notification = profile.environment_notifications.find_by_id(params[:id]) | ||
54 | - | ||
55 | - @notification.active = !@notification.active | ||
56 | - | ||
57 | - if @notification.save! | ||
58 | - session[:notice] = _('The status of the notification was changed.') | ||
59 | - else | ||
60 | - session[:notice] = _('Could not change the status of the notification.') | ||
61 | - end | ||
62 | - | ||
63 | - redirect_to :action => :index | ||
64 | - end | ||
65 | - | ||
66 | - def close_notification | ||
67 | - result = false | ||
68 | - | ||
69 | - if logged_in? | ||
70 | - @notification = profile.environment_notifications.find_by_id(params[:notification_id]) | ||
71 | - | ||
72 | - if @notification | ||
73 | - @notification.users << current_user | ||
74 | - result = @notification.users.include?(current_user) | ||
75 | - end | ||
76 | - end | ||
77 | - | ||
78 | - render json: result | ||
79 | - end | ||
80 | - | ||
81 | - def hide_notification | ||
82 | - result = false | ||
83 | - | ||
84 | - if logged_in? | ||
85 | - @notification = profile.environment_notifications.find_by_id(params[:notification_id]) | ||
86 | - | ||
87 | - if @notification | ||
88 | - current_notificaions = [] | ||
89 | - current_notificaions = JSON.parse(cookies[:hide_notifications]) unless cookies[:hide_notifications].blank? | ||
90 | - current_notificaions << @notification.id unless current_notificaions.include? @notification.id | ||
91 | - cookies[:hide_notifications] = JSON.generate(current_notificaions) | ||
92 | - result = current_notificaions.include? @notification.id | ||
93 | - end | ||
94 | - end | ||
95 | - | ||
96 | - render json: result | ||
97 | - end | 5 | + before_filter :admin_required |
98 | 6 | ||
99 | protected | 7 | protected |
100 | - def admin_required | ||
101 | - redirect_to :root unless (current_person.is_admin? || profile.is_admin?(current_person)) | 8 | + def target |
9 | + profile | ||
102 | end | 10 | end |
103 | 11 | ||
104 | end | 12 | end |
plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb
@@ -12,4 +12,37 @@ class EnvironmentNotificationPluginPublicController < PublicController | @@ -12,4 +12,37 @@ class EnvironmentNotificationPluginPublicController < PublicController | ||
12 | end | 12 | end |
13 | end | 13 | end |
14 | 14 | ||
15 | + def close_notification | ||
16 | + result = false | ||
17 | + | ||
18 | + if logged_in? | ||
19 | + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.find(params[:notification_id]) | ||
20 | + | ||
21 | + if @notification | ||
22 | + @notification.users << current_user | ||
23 | + result = @notification.users.include?(current_user) | ||
24 | + end | ||
25 | + end | ||
26 | + | ||
27 | + render json: result | ||
28 | + end | ||
29 | + | ||
30 | + def hide_notification | ||
31 | + result = false | ||
32 | + | ||
33 | + if logged_in? | ||
34 | + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.find(params[:notification_id]) | ||
35 | + | ||
36 | + if @notification | ||
37 | + current_notificaions = [] | ||
38 | + current_notificaions = JSON.parse(cookies[:hide_notifications]) unless cookies[:hide_notifications].blank? | ||
39 | + current_notificaions << @notification.id unless current_notificaions.include? @notification.id | ||
40 | + cookies[:hide_notifications] = JSON.generate(current_notificaions) | ||
41 | + result = current_notificaions.include? @notification.id | ||
42 | + end | ||
43 | + end | ||
44 | + | ||
45 | + render json: result | ||
46 | + end | ||
47 | + | ||
15 | end | 48 | end |
plugins/environment_notification/lib/environment_notification_plugin.rb
@@ -14,7 +14,7 @@ class EnvironmentNotificationPlugin < Noosfero::Plugin | @@ -14,7 +14,7 @@ class EnvironmentNotificationPlugin < Noosfero::Plugin | ||
14 | 14 | ||
15 | def js_files | 15 | def js_files |
16 | %w( | 16 | %w( |
17 | - public/environment_notification_plugin.js | 17 | + environment_notification_plugin.js |
18 | ) | 18 | ) |
19 | end | 19 | end |
20 | 20 |
plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb
@@ -26,16 +26,12 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas | @@ -26,16 +26,12 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas | ||
26 | end | 26 | end |
27 | end | 27 | end |
28 | 28 | ||
29 | - scope :active, lambda{|target| { :conditions => { :target_id => target.id, :active => true } } } | 29 | + scope :active, lambda{|target| { :conditions => { :target_id => (target.kind_of?(Organization) ? [target.id, target.environment.id] : target.id), :active => true } } } |
30 | 30 | ||
31 | # TODO: Change method body to scopes | 31 | # TODO: Change method body to scopes |
32 | def self.visibles(target, user, controller_path) | 32 | def self.visibles(target, user, controller_path) |
33 | notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(target).order('updated_at DESC') | 33 | notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(target).order('updated_at DESC') |
34 | 34 | ||
35 | - if target.kind_of?(Organization) && target.environment.present? | ||
36 | - env_notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(target.environment).order('updated_at DESC') | ||
37 | - end | ||
38 | - | ||
39 | if user | 35 | if user |
40 | active_notifications_ids = notifications.pluck(:id) - user.environment_notifications.pluck(:id) | 36 | active_notifications_ids = notifications.pluck(:id) - user.environment_notifications.pluck(:id) |
41 | 37 |
plugins/environment_notification/public/environment_notification_plugin.js
@@ -14,7 +14,7 @@ | @@ -14,7 +14,7 @@ | ||
14 | var id = notification.attr("data-notification"); | 14 | var id = notification.attr("data-notification"); |
15 | 15 | ||
16 | $.ajax({ | 16 | $.ajax({ |
17 | - url: noosfero_root()+"/admin/plugin/environment_notification/close_notification", | 17 | + url: noosfero_root()+'/plugin/environment_notification/public/close_notification', |
18 | type: "POST", | 18 | type: "POST", |
19 | data: {notification_id: id}, | 19 | data: {notification_id: id}, |
20 | success: function(response) { | 20 | success: function(response) { |
@@ -28,7 +28,7 @@ | @@ -28,7 +28,7 @@ | ||
28 | var id = notification.attr("data-notification"); | 28 | var id = notification.attr("data-notification"); |
29 | 29 | ||
30 | $.ajax({ | 30 | $.ajax({ |
31 | - url: noosfero_root()+"/admin/plugin/environment_notification/hide_notification", | 31 | + url: noosfero_root()+'/plugin/environment_notification/public/hide_notification', |
32 | type: "POST", | 32 | type: "POST", |
33 | data: {notification_id: id}, | 33 | data: {notification_id: id}, |
34 | success: function(response) { | 34 | success: function(response) { |