From eecf7d16e9b06490dc1ffe790aa759d665e335f7 Mon Sep 17 00:00:00 2001 From: Marcos Ronaldo Date: Mon, 21 Mar 2016 17:19:42 -0300 Subject: [PATCH] add create notification myprofile controller --- plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/lib/ext/environment.rb | 2 +- plugins/environment_notification/lib/ext/organization.rb | 2 +- 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb diff --git a/plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb b/plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb new file mode 100644 index 0000000..0e03072 --- /dev/null +++ b/plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb @@ -0,0 +1,104 @@ +class EnvironmentNotificationPluginMyProfileController < MyProfileController + + helper EnvironmentNotificationHelper + include EnvironmentNotificationHelper + + 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 + + protected + def admin_required + redirect_to :root unless profile.is_admin?(current_user.person) + end + +end diff --git a/plugins/environment_notification/lib/ext/environment.rb b/plugins/environment_notification/lib/ext/environment.rb index 0fd9264..097ea36 100644 --- a/plugins/environment_notification/lib/ext/environment.rb +++ b/plugins/environment_notification/lib/ext/environment.rb @@ -1,5 +1,5 @@ require_dependency 'environment' class Environment - has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification' + has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification', :as => :target end diff --git a/plugins/environment_notification/lib/ext/organization.rb b/plugins/environment_notification/lib/ext/organization.rb index e000468..2c3e369 100644 --- a/plugins/environment_notification/lib/ext/organization.rb +++ b/plugins/environment_notification/lib/ext/organization.rb @@ -1,5 +1,5 @@ require_dependency 'organization' class Organization - has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification' + has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification', :as => :target end -- libgit2 0.21.2