From 211e635e02365fe7a5087b150807885a5c8255be Mon Sep 17 00:00:00 2001 From: Eduardo Vital Date: Tue, 21 Jul 2015 10:58:30 -0300 Subject: [PATCH] Create environment notification plugin --- plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb | 24 ++++++++++++++++++++++++ plugins/environment_notification/lib/environment_notification_plugin.rb | 29 +++++++++++++++++++++++++++++ plugins/environment_notification/lib/environment_notifications_user.rb | 10 ++++++++++ plugins/environment_notification/lib/ext/environment.rb | 5 +++++ plugins/environment_notification/lib/ext/user.rb | 6 ++++++ plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb | 2 ++ plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb | 27 +++++++++++++++++++++++++++ plugins/environment_notification/models/environment_notification_plugin/information_notification.rb | 2 ++ plugins/environment_notification/models/environment_notification_plugin/success_notification.rb | 2 ++ plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb | 2 ++ plugins/environment_notification/public/environment_notification_plugin.js | 40 ++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/public/images/close.png | Bin 0 -> 240 bytes plugins/environment_notification/public/images/hide.png | Bin 0 -> 389 bytes plugins/environment_notification/public/images/show.png | Bin 0 -> 364 bytes plugins/environment_notification/style.css | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/test/functional/home_controller_test.rb | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb | 27 +++++++++++++++++++++++++++ plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb | 1 + plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb | 40 ++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb | 1 + plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb | 24 ++++++++++++++++++++++++ 23 files changed, 721 insertions(+), 0 deletions(-) create mode 100644 plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb create mode 100644 plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb create mode 100644 plugins/environment_notification/lib/environment_notification_plugin.rb create mode 100644 plugins/environment_notification/lib/environment_notifications_user.rb create mode 100644 plugins/environment_notification/lib/ext/environment.rb create mode 100644 plugins/environment_notification/lib/ext/user.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/information_notification.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/success_notification.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb create mode 100644 plugins/environment_notification/public/environment_notification_plugin.js create mode 100644 plugins/environment_notification/public/images/close.png create mode 100644 plugins/environment_notification/public/images/hide.png create mode 100644 plugins/environment_notification/public/images/show.png create mode 100644 plugins/environment_notification/style.css create mode 100644 plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb create mode 100644 plugins/environment_notification/test/functional/home_controller_test.rb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb diff --git a/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb b/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb new file mode 100644 index 0000000..bd562b9 --- /dev/null +++ b/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb @@ -0,0 +1,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 diff --git a/plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb b/plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb new file mode 100644 index 0000000..79d9e6f --- /dev/null +++ b/plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb @@ -0,0 +1,24 @@ +class CreateNotificationTable < ActiveRecord::Migration + def up + create_table :environment_notifications do |t| + t.text :message + t.integer :environment_id + t.string :type + t.boolean :active + t.boolean :display_only_in_homepage, :default => false + t.boolean :display_to_all_users, :default => false + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :environment_notifications_users, id: false do |t| + t.belongs_to :environment_notification, index: true + t.belongs_to :user, index: true + end + end + + def down + drop_table :environment_notifications + drop_table :environment_notifications_users + end +end diff --git a/plugins/environment_notification/lib/environment_notification_plugin.rb b/plugins/environment_notification/lib/environment_notification_plugin.rb new file mode 100644 index 0000000..f5add4f --- /dev/null +++ b/plugins/environment_notification/lib/environment_notification_plugin.rb @@ -0,0 +1,29 @@ +class EnvironmentNotificationPlugin < Noosfero::Plugin + + def self.plugin_name + "Environment Notifications Plugin" + end + + def self.plugin_description + _("A plugin for environment notifications.") + end + + def stylesheet? + true + end + + def js_files + %w( + public/environment_notification_plugin.js + ) + end + + def body_beginning + expanded_template('environment_notification_plugin_admin/show_notification.html.erb') + end + + def admin_panel_links + {:title => _('Notification Manager'), :url => {:controller => 'environment_notification_plugin_admin', :action => 'index'}} + end + +end diff --git a/plugins/environment_notification/lib/environment_notifications_user.rb b/plugins/environment_notification/lib/environment_notifications_user.rb new file mode 100644 index 0000000..69d63b8 --- /dev/null +++ b/plugins/environment_notification/lib/environment_notifications_user.rb @@ -0,0 +1,10 @@ +class EnvironmentNotificationsUser < ActiveRecord::Base + self.table_name = "environment_notifications_users" + + belongs_to :user + belongs_to :environment_notification, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification' + + attr_accessible :user_id, :environment_notification_id + + validates_uniqueness_of :user_id, :scope => :environment_notification_id +end diff --git a/plugins/environment_notification/lib/ext/environment.rb b/plugins/environment_notification/lib/ext/environment.rb new file mode 100644 index 0000000..0fd9264 --- /dev/null +++ b/plugins/environment_notification/lib/ext/environment.rb @@ -0,0 +1,5 @@ +require_dependency 'environment' + +class Environment + has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification' +end diff --git a/plugins/environment_notification/lib/ext/user.rb b/plugins/environment_notification/lib/ext/user.rb new file mode 100644 index 0000000..2cb316e --- /dev/null +++ b/plugins/environment_notification/lib/ext/user.rb @@ -0,0 +1,6 @@ +require_dependency 'user' + +class User + has_many :environment_notifications_users + has_many :environment_notifications, :through => :environment_notifications_users +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb new file mode 100644 index 0000000..35a6ce1 --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb @@ -0,0 +1,2 @@ +class EnvironmentNotificationPlugin::DangerNotification < EnvironmentNotificationPlugin::EnvironmentNotification +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 new file mode 100644 index 0000000..157810e --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb @@ -0,0 +1,27 @@ +class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Base + + self.table_name = "environment_notifications" + + TYPE_LIST = [ + "EnvironmentNotificationPlugin::WarningNotification", + "EnvironmentNotificationPlugin::SuccessNotification", + "EnvironmentNotificationPlugin::InformationNotification", + "EnvironmentNotificationPlugin::DangerNotification" + ] + + attr_accessible :message, :environment_id, :active, :type, :display_only_in_homepage, :display_to_all_users + + has_many :environment_notifications_users + has_many :users, :through => :environment_notifications_users + + validates_presence_of :message + validates_presence_of :environment_id + validate :notification_type_must_be_in_type_list + + def notification_type_must_be_in_type_list + unless TYPE_LIST.include?(type) + errors.add(:type, "invalid notification type") + end + end + +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/information_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/information_notification.rb new file mode 100644 index 0000000..661ecdb --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/information_notification.rb @@ -0,0 +1,2 @@ +class EnvironmentNotificationPlugin::InformationNotification < EnvironmentNotificationPlugin::EnvironmentNotification +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/success_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/success_notification.rb new file mode 100644 index 0000000..22db359 --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/success_notification.rb @@ -0,0 +1,2 @@ +class EnvironmentNotificationPlugin::SuccessNotification < EnvironmentNotificationPlugin::EnvironmentNotification +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb new file mode 100644 index 0000000..378bd5a --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb @@ -0,0 +1,2 @@ +class EnvironmentNotificationPlugin::WarningNotification < EnvironmentNotificationPlugin::EnvironmentNotification +end diff --git a/plugins/environment_notification/public/environment_notification_plugin.js b/plugins/environment_notification/public/environment_notification_plugin.js new file mode 100644 index 0000000..6250172 --- /dev/null +++ b/plugins/environment_notification/public/environment_notification_plugin.js @@ -0,0 +1,40 @@ +(function($) { + "use strict"; + + function notificationBar() { + var completeMessage = $(".notification-bar").remove(); + $("#content-inner").before(completeMessage); + }, + + function hideNotification(){ + var notification = $(this).parent(); + var id = notification.attr("data-notification"); + + $.ajax({ + url: noosfero_root()+"/admin/plugin/environment_notification/hide_notification", + type: "POST", + data: {notification_id: id}, + success: function(response) { + notification.fadeOut(); + } + }); + } + + function mceRestrict() { + tinyMCE.init({ + menubar : false, + selector: "textarea", + plugins: [ + "autolink link" + ], + toolbar: "bold italic underline | link" + }); + } + + jQuery(document).ready(function(){ + notificationBar(); + $(".notification-close").on("click", hideNotification); + mceRestrict(); + }); + +})(jQuery); \ No newline at end of file diff --git a/plugins/environment_notification/public/images/close.png b/plugins/environment_notification/public/images/close.png new file mode 100644 index 0000000..e51bbb9 Binary files /dev/null and b/plugins/environment_notification/public/images/close.png differ diff --git a/plugins/environment_notification/public/images/hide.png b/plugins/environment_notification/public/images/hide.png new file mode 100644 index 0000000..5804c92 Binary files /dev/null and b/plugins/environment_notification/public/images/hide.png differ diff --git a/plugins/environment_notification/public/images/show.png b/plugins/environment_notification/public/images/show.png new file mode 100644 index 0000000..477815e Binary files /dev/null and b/plugins/environment_notification/public/images/show.png differ diff --git a/plugins/environment_notification/style.css b/plugins/environment_notification/style.css new file mode 100644 index 0000000..a8d9d12 --- /dev/null +++ b/plugins/environment_notification/style.css @@ -0,0 +1,141 @@ +.notification-bar { + display: block; +} + +.notification:hover { + opacity: 0.8; +} + +#notification-manager { + overflow: auto; +} + +.notification .notification-close { + background: url(public/images/close.png) no-repeat; + background-position: center; + width: 20px; + height: 20px; +} + +.warningnotification, +.informationnotification, +.successnotification, +.dangernotification { + margin-bottom: 10px; + padding: 7px 10px; + border-radius: 5px; + border: 1px solid blue; + font-size: 16px; + color: white; + overflow: auto; +} + +.warningnotification p, +.informationnotification p, +.successnotification p, +.dangernotification p { + margin: 0px; +} + +.warningnotification { + background: #EEA236; + border: 1px solid #EEA236; +} + +.informationnotification { + background: #5BC0DE; + border: 1px solid #46B8DA; +} + +.successnotification { + background: #5CB85C; + border: 1px solid #4CAE4C; +} + +.dangernotification { + background: #C9302C; + border: 1px solid #AC2925; +} + +a.button.icon-deactivate { + background: url(public/images/hide.png) no-repeat; + background-position: center; +} + +a.button.icon-activate { + background: url(public/images/show.png) no-repeat; + background-position: center; +} + +.notification-line { + display: inline; + padding-top: 10px; + vertical-align: middle; + border-bottom: 1px solid #ccc; +} + +.notification-title-bar { + float: left; + width: 100%; + font-style: 14px; + font-weight: 700; + border-bottom: 2px solid black; + padding: 9px 0; +} + +.notification-title { + width: 80%; + float: left; + text-align: center; +} + +.action-title { + width: 20%; + float: left; + text-align: center; +} + +.notification-action { + width: 18%; + float: left; + height: 30px; + padding-top: 9px; +} + +.main-bar .button, +.notification-action .button { + border-radius: 3px; +} + +.notification-message { + width: 82%; + float: left; +} + +.new-notification { + float: right; + width: auto; +} + +.back-button { + float: left; +} + +.main-bar { + display: inline; + width: 100%; +} + +.notification-bar .notification .notification-message { + width: 90%; + float: left; +} + +.notification-bar .notification .notification-close { + background: url(public/images/close.png) no-repeat; + background-position: center; + width: 20px; + height: 20px; + float: right; + cursor: pointer; +} diff --git a/plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb b/plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb new file mode 100644 index 0000000..db9eb38 --- /dev/null +++ b/plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb @@ -0,0 +1,153 @@ +require File.expand_path(File.dirname(__FILE__)) + '/../../../../test/test_helper' +require( + File.expand_path(File.dirname(__FILE__)) + + '/../../controllers/environment_notification_plugin_admin_controller' +) + +class EnvironmentNotificationPluginAdminController; def rescue_action(e) raise e end; +end + +class EnvironmentNotificationPluginAdminControllerTest < ActionController::TestCase + def setup + @controller = EnvironmentNotificationPluginAdminController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @person = create_user('person').person + + @environment = Environment.default + @environment.enable_plugin('EnvironmentNotificationPlugin') + @environment.save! + + login_as(@person.user.login) + end + + attr_accessor :person + + should 'an admin be able to create a notification' do + @environment.add_admin(@person) + post :new, :notifications => { + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + } + assert_redirected_to :action => 'index' + notification = EnvironmentNotificationPlugin::EnvironmentNotification.last + assert_equal "Message", notification.message + assert notification.active + assert_equal "EnvironmentNotificationPlugin::DangerNotification", notification.type + end + + should 'an user not to be able to create a notification' do + post :new, :notifications => { + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + } + assert_redirected_to :root + assert_nil EnvironmentNotificationPlugin::EnvironmentNotification.last + end + + should 'an admin be able to edit a notification' do + @environment.add_admin(@person) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :edit, :id => @notification.id, :notifications => { + :message => "Edited Message", + :active => false, + :type => "EnvironmentNotificationPlugin::WarningNotification" + } + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.last + assert_redirected_to :action => 'index' + assert_equal "Edited Message", @notification.message + assert !@notification.active + assert_equal "EnvironmentNotificationPlugin::WarningNotification", @notification.type + end + + should 'an user not to be able to edit a notification' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :edit, :notifications => { + :message => "Edited Message", + :active => false, + :type => "EnvironmentNotificationPlugin::DangerNotification" + } + @notification.reload + assert_redirected_to :root + assert_equal "Message", @notification.message + assert @notification.active + end + + should 'an admin be able to destroy a notification' do + @environment.add_admin(@person) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + delete :destroy, :id => @notification.id + assert_nil EnvironmentNotificationPlugin::EnvironmentNotification.find_by_id(@notification.id) + end + + should 'an user not to be able to destroy a notification' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + delete :destroy, :id => @notification.id + + assert_redirected_to :root + assert_not_nil EnvironmentNotificationPlugin::EnvironmentNotification.find_by_id(@notification.id) + end + + should 'an admin be able to change Notification status' do + @environment.add_admin(@person) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :change_status, :id => @notification.id + assert_redirected_to :action => 'index' + + @notification.reload + assert !@notification.active + end + + should 'an user not be able to change Notification status' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :change_status, :id => @notification.id + assert_redirected_to :root + + @notification.reload + assert @notification.active + end + + should 'a logged in user be able to permanently hide notifications' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :hide_notification, :notification_id => @notification.id + assert_equal "true", @response.body + assert_equal true, @notification.users.include?(@person.user) + end +end diff --git a/plugins/environment_notification/test/functional/home_controller_test.rb b/plugins/environment_notification/test/functional/home_controller_test.rb new file mode 100644 index 0000000..e73a63c --- /dev/null +++ b/plugins/environment_notification/test/functional/home_controller_test.rb @@ -0,0 +1,105 @@ +require File.expand_path(File.dirname(__FILE__)) + '/../../../../test/test_helper' +require 'home_controller' + +class HomeController; def rescue_action(e) raise e end; +end + +class HomeControllerTest < ActionController::TestCase + def setup + @controller = HomeController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @person = create_user('person').person + + @environment = Environment.default + @environment.enable_plugin('EnvironmentNotificationPlugin') + @environment.save! + end + + attr_accessor :person + + should 'an active notification be displayed on home page for a logged in user' do + login_as(@person.user.login) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + get :index + assert_match /Hello, this is a Notification Message/, @response.body + end + + + should 'an active notification not be displayed on home page for unlogged user' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + get :index + assert_no_match /Hello, this is a Notification Message/, @response.body + end + + should 'an active notification be displayed on home page for unlogged user' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :display_to_all_users => true, + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + get :index + assert_match /Hello, this is a Notification Message/, @response.body + end + + should 'only display the notification with display_to_all_users option for unlogged user ' do + @notification1 = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is an old Notification Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + + @notification2 = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a new Notification Message", + :display_to_all_users => true, + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + + + get :index + assert_no_match /Hello, this is a Notification Message/, @response.body + assert_match /Hello, this is a new Notification Message/, @response.body + end + + should 'an inactive notification not be displayed on home page' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :active => false, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + get :index + assert_no_match /Hello, this is a Notification Message/, @response.body + end + + + should 'an active notification not be displayed to a logged in user after been closed by him' do + login_as(@person.user.login) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + @notification.users << @person.user + @notification.save! + assert_equal true, @notification.users.include?(@person.user) + get :index + assert_no_match /Hello, this is a Notification Message/, @response.body + end +end diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb new file mode 100644 index 0000000..f18cd90 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb @@ -0,0 +1,27 @@ +<% abstract_options = {:value => @notification.message, :style => 'width: 100%; height: 200px;'} %> + +<%= button :back, _('Back'), :controller => 'environment_notification_plugin_admin' %> + +<%= form_for :notifications do |f| %> + + <%= render :file => 'shared/tiny_mce' %> + + <%= labelled_form_field(_("Enter your message here:"), f.text_area(:message, abstract_options)) %> + + <%= labelled_form_field(_('Notifications Status'), select(:notifications, :active, options_for_select_with_title({"Active" => true, "Inactive" => false}, @notification.active))) %> + + <%= labelled_form_field(_('Notifications Type'), select(:notifications, :type, options_for_select_with_title({"Information" => "EnvironmentNotificationPlugin::InformationNotification", "Warning" => "EnvironmentNotificationPlugin::WarningNotification", "Success" => "EnvironmentNotificationPlugin::SuccessNotification", "Danger" => "EnvironmentNotificationPlugin::DangerNotification"}, @notification.type))) %> + +
+ <%= labelled_check_box(_("Display only in the homepage"), 'notifications[display_only_in_homepage]', '1', @notification.display_only_in_homepage?) %> +
+ +
+ <%= labelled_check_box(_("Display to not logged users too"), 'notifications[display_to_all_users]', '1', @notification.display_to_all_users?) %> +
+ + <% button_bar do %> + <%= submit_button 'save', _('Save'), :cancel => { :action => 'index' } %> + <% end %> + +<% end %> diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb new file mode 100644 index 0000000..2872e82 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb @@ -0,0 +1 @@ +<%= render :partial => "form" %> diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb new file mode 100644 index 0000000..6978526 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb @@ -0,0 +1,40 @@ +
+
+

<%= _("Environment Notifications") %>

+
+
+
+ <%= button :new, _('New Notification'), {:action => :new}, :method => :get %> +
+
+ <%= button :back, _('Back to control panel'), {:controller => 'admin_panel', :action => :index}, :method => :get %> +
+
+ +
+
+ <%= _('Notifications') %> +
+
+ <%= _('Actions') %> +
+
+ + <% @notifications.each do |notification| %> +
+
+ <%= truncate(notification.message, length: 50) %> +
+
+ <% if notification.active? %> + <%= button_without_text :deactivate, _('Deactivate'), {:action => :change_status, :id => notification}, :method => :post, :confirm => _("Do you want to change the status of this notification?") %> + <% else %> + <%= button_without_text :activate, _('Activate'), {:action => :change_status, :id => notification}, :method => :post, :confirm => _("Do you want to change the status of this notification?") %> + <% end %> + <%= button_without_text :edit, _('Edit'), {:action => 'edit', :id => notification.id} if !remove_content_button(:edit, notification) %> + <%= button_without_text :delete, _('Delete'), {:action => :destroy, :id => notification}, :method => :delete, :confirm => _("Do you want to delete this notification?") %> +
+
+ <% end %> +
+ diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb new file mode 100644 index 0000000..2872e82 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb @@ -0,0 +1 @@ +<%= render :partial => "form" %> diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb new file mode 100644 index 0000000..5140e71 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb @@ -0,0 +1,24 @@ +
+ <% if logged_in? %> + <% @notifications = environment.environment_notifications.order('updated_at DESC').where(active: true) %> + <% @deactivated = current_user.environment_notifications %> + <% @notifications = @notifications - @deactivated %> + <% else %> + <% @notifications = environment.environment_notifications.order('updated_at DESC').where(active: true) %> + <% end %> + + <% @notifications.each do |notification| %> + <% if controller_path == "home" || !notification.display_only_in_homepage? %> + <% if logged_in? || notification.display_to_all_users? %> +
notification" data-notification="<%=notification.id%>"> +
+ <%= notification.message %> +
+ <% if logged_in? %> +
+ <% end %> +
+ <% end %> + <% end %> + <% end %> +
-- libgit2 0.21.2