diff --git a/plugins/environment_notification/db/migrate/20160321190726_change_notification_relation_to_polymorphic.rb b/plugins/environment_notification/db/migrate/20160321190726_change_notification_relation_to_polymorphic.rb new file mode 100644 index 0000000..faf67f6 --- /dev/null +++ b/plugins/environment_notification/db/migrate/20160321190726_change_notification_relation_to_polymorphic.rb @@ -0,0 +1,13 @@ +class ChangeNotificationRelationToPolymorphic < ActiveRecord::Migration + def up + rename_column(:environment_notifications, :environment_id, :target_id) + add_column(:environment_notifications, :target_type, :string) + + execute("UPDATE environment_notifications SET target_type = 'Environment'") + end + + def down + rename_column(:environment_notifications, :target_id, :environment_id) + remove_column(:environment_notifications, :target_type) + end +end diff --git a/plugins/environment_notification/lib/environment_notification_plugin.rb b/plugins/environment_notification/lib/environment_notification_plugin.rb index 2e92035..8331e40 100644 --- a/plugins/environment_notification/lib/environment_notification_plugin.rb +++ b/plugins/environment_notification/lib/environment_notification_plugin.rb @@ -29,6 +29,19 @@ class EnvironmentNotificationPlugin < Noosfero::Plugin {:title => _('Notification Manager'), :url => {:controller => 'environment_notification_plugin_admin', :action => 'index'}} end + def control_panel_buttons + if context.profile.organization? + { + :title => _('Manage Notifications'), + :icon => 'important', + :url => { + :controller => 'environment_notification_plugin_myprofile', + :action => 'index' + } + } + end + end + def account_controller_filters block = proc do if !logged_in? diff --git a/plugins/environment_notification/lib/ext/organization.rb b/plugins/environment_notification/lib/ext/organization.rb new file mode 100644 index 0000000..e000468 --- /dev/null +++ b/plugins/environment_notification/lib/ext/organization.rb @@ -0,0 +1,5 @@ +require_dependency 'organization' + +class Organization + has_many :environment_notifications, class_name: '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 index ad91678..7962afc 100644 --- a/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb +++ b/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb @@ -9,13 +9,15 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas "EnvironmentNotificationPlugin::DangerNotification" ] - attr_accessible :message, :environment_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title + attr_accessible :message, :target_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title has_many :environment_notifications_users has_many :users, :through => :environment_notifications_users + belongs_to :target, :polymorphic => true + validates_presence_of :message - validates_presence_of :environment_id + validates_presence_of :target_id validate :notification_type_must_be_in_type_list def notification_type_must_be_in_type_list @@ -24,10 +26,11 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas end end - scope :active, lambda{|environment| { :conditions => { :environment_id => environment.id, :active => true } } } + scope :active, lambda{|target| { :conditions => { :target_id => target.id, :active => true } } } - def self.visibles(environment, user, controller_path) - notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(environment).order('updated_at DESC') + # TODO: Change method body to scopes + def self.visibles(target, user, controller_path) + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(target).order('updated_at DESC') if user active_notifications_ids = notifications.pluck(:id) - user.environment_notifications.pluck(:id) @@ -44,7 +47,7 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas notifications end - def self.with_popup(environment, user, previous_path) - notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(environment, user, previous_path).where(display_popup: true) + def self.with_popup(target, user, previous_path) + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(target, user, previous_path).where(display_popup: true) end end -- libgit2 0.21.2