Commit eecf7d16e9b06490dc1ffe790aa759d665e335f7

Authored by Marcos Pereira
1 parent 01e2d605

add create notification myprofile controller

plugins/environment_notification/controllers/environment_notification_plugin_myprofile_controller.rb 0 → 100644
... ... @@ -0,0 +1,104 @@
  1 +class EnvironmentNotificationPluginMyProfileController < MyProfileController
  2 +
  3 + helper EnvironmentNotificationHelper
  4 + include EnvironmentNotificationHelper
  5 +
  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
  98 +
  99 + protected
  100 + def admin_required
  101 + redirect_to :root unless profile.is_admin?(current_user.person)
  102 + end
  103 +
  104 +end
... ...
plugins/environment_notification/lib/ext/environment.rb
1 1 require_dependency 'environment'
2 2  
3 3 class Environment
4   - has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification'
  4 + has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification', :as => :target
5 5 end
... ...
plugins/environment_notification/lib/ext/organization.rb
1 1 require_dependency 'organization'
2 2  
3 3 class Organization
4   - has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification'
  4 + has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification', :as => :target
5 5 end
... ...