From fc719411e6a291f5f01f307d4b0a7d8b03218b3f Mon Sep 17 00:00:00 2001 From: Gabriel Silva Date: Wed, 23 Mar 2016 18:21:04 +0000 Subject: [PATCH] Refactor environment notification controllers --- plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb | 99 ++++----------------------------------------------------------------------------------------------- plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb | 100 ++++------------------------------------------------------------------------------------------------ plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb | 33 +++++++++++++++++++++++++++++++++ plugins/environment_notification/lib/environment_notification_plugin.rb | 2 +- plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb | 6 +----- plugins/environment_notification/public/environment_notification_plugin.js | 4 ++-- 6 files changed, 45 insertions(+), 199 deletions(-) diff --git a/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb b/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb index 75f3be2..e6b20a7 100644 --- a/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb +++ b/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb @@ -1,103 +1,12 @@ class EnvironmentNotificationPluginAdminController < AdminController - helper EnvironmentNotificationHelper - include EnvironmentNotificationHelper + include NotificationManager - before_filter :admin_required, :except => [:close_notification, :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.target = environment - 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 close_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 - - def hide_notification - result = false - - if logged_in? - @notification = environment.environment_notifications.find_by_id(params[:notification_id]) - - if @notification - current_notificaions = [] - current_notificaions = JSON.parse(cookies[:hide_notifications]) unless cookies[:hide_notifications].blank? - current_notificaions << @notification.id unless current_notificaions.include? @notification.id - cookies[:hide_notifications] = JSON.generate(current_notificaions) - result = current_notificaions.include? @notification.id - end - end - - render json: result - end + before_filter :admin_required protected - def admin_required - redirect_to :root unless current_user.person.is_admin? + def target + environment end end diff --git a/plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb b/plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb index 7c1df2c..f8ffb6f 100644 --- a/plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb +++ b/plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb @@ -1,104 +1,12 @@ class EnvironmentNotificationPluginMyprofileController < MyProfileController - helper EnvironmentNotificationHelper - include EnvironmentNotificationHelper + include NotificationManager - before_filter :admin_required, :except => [:close_notification, :hide_notification] - #TODO test new notification when profile is not an organization - - def index - @notifications = profile.environment_notifications.order('updated_at DESC') - end - - def new - @notification = EnvironmentNotificationPlugin::EnvironmentNotification.new - if request.post? && profile && profile.organization? - @notification = EnvironmentNotificationPlugin::EnvironmentNotification.new(params[:notifications]) - @notification.message = @notification.message.html_safe - @notification.target = profile - if @notification.save - session[:notice] = _("Notification successfully created for profile %s" % profile.name) - redirect_to :action => :index - else - session[:notice] = _("Notification couldn't be created") - end - end - end - - def destroy - if request.delete? - notification = profile.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 = profile.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 = profile.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 close_notification - result = false - - if logged_in? - @notification = profile.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 - - def hide_notification - result = false - - if logged_in? - @notification = profile.environment_notifications.find_by_id(params[:notification_id]) - - if @notification - current_notificaions = [] - current_notificaions = JSON.parse(cookies[:hide_notifications]) unless cookies[:hide_notifications].blank? - current_notificaions << @notification.id unless current_notificaions.include? @notification.id - cookies[:hide_notifications] = JSON.generate(current_notificaions) - result = current_notificaions.include? @notification.id - end - end - - render json: result - end + before_filter :admin_required protected - def admin_required - redirect_to :root unless (current_person.is_admin? || profile.is_admin?(current_person)) + def target + profile end end diff --git a/plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb b/plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb index cf6fea6..6607d95 100644 --- a/plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb +++ b/plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb @@ -12,4 +12,37 @@ class EnvironmentNotificationPluginPublicController < PublicController end end + def close_notification + result = false + + if logged_in? + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.find(params[:notification_id]) + + if @notification + @notification.users << current_user + result = @notification.users.include?(current_user) + end + end + + render json: result + end + + def hide_notification + result = false + + if logged_in? + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.find(params[:notification_id]) + + if @notification + current_notificaions = [] + current_notificaions = JSON.parse(cookies[:hide_notifications]) unless cookies[:hide_notifications].blank? + current_notificaions << @notification.id unless current_notificaions.include? @notification.id + cookies[:hide_notifications] = JSON.generate(current_notificaions) + result = current_notificaions.include? @notification.id + end + end + + render json: result + end + end diff --git a/plugins/environment_notification/lib/environment_notification_plugin.rb b/plugins/environment_notification/lib/environment_notification_plugin.rb index c663e17..823f567 100644 --- a/plugins/environment_notification/lib/environment_notification_plugin.rb +++ b/plugins/environment_notification/lib/environment_notification_plugin.rb @@ -14,7 +14,7 @@ class EnvironmentNotificationPlugin < Noosfero::Plugin def js_files %w( - public/environment_notification_plugin.js + environment_notification_plugin.js ) end diff --git a/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb index f623bb2..73f0716 100644 --- a/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb +++ b/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb @@ -26,16 +26,12 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas end end - scope :active, lambda{|target| { :conditions => { :target_id => target.id, :active => true } } } + scope :active, lambda{|target| { :conditions => { :target_id => (target.kind_of?(Organization) ? [target.id, target.environment.id] : target.id), :active => true } } } # TODO: Change method body to scopes def self.visibles(target, user, controller_path) notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(target).order('updated_at DESC') - if target.kind_of?(Organization) && target.environment.present? - env_notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(target.environment).order('updated_at DESC') - end - if user active_notifications_ids = notifications.pluck(:id) - user.environment_notifications.pluck(:id) diff --git a/plugins/environment_notification/public/environment_notification_plugin.js b/plugins/environment_notification/public/environment_notification_plugin.js index 0556aa1..ddc7194 100644 --- a/plugins/environment_notification/public/environment_notification_plugin.js +++ b/plugins/environment_notification/public/environment_notification_plugin.js @@ -14,7 +14,7 @@ var id = notification.attr("data-notification"); $.ajax({ - url: noosfero_root()+"/admin/plugin/environment_notification/close_notification", + url: noosfero_root()+'/plugin/environment_notification/public/close_notification', type: "POST", data: {notification_id: id}, success: function(response) { @@ -28,7 +28,7 @@ var id = notification.attr("data-notification"); $.ajax({ - url: noosfero_root()+"/admin/plugin/environment_notification/hide_notification", + url: noosfero_root()+'/plugin/environment_notification/public/hide_notification', type: "POST", data: {notification_id: id}, success: function(response) { -- libgit2 0.21.2