Commit 01e2d605c905c64f83fff1d5cd39fb5f750dbe3d

Authored by Gabriel Silva
1 parent 9e77f7f1

Changes EnvironmentNotification reloation to polymorphic

Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
plugins/environment_notification/db/migrate/20160321190726_change_notification_relation_to_polymorphic.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class ChangeNotificationRelationToPolymorphic < ActiveRecord::Migration
  2 + def up
  3 + rename_column(:environment_notifications, :environment_id, :target_id)
  4 + add_column(:environment_notifications, :target_type, :string)
  5 +
  6 + execute("UPDATE environment_notifications SET target_type = 'Environment'")
  7 + end
  8 +
  9 + def down
  10 + rename_column(:environment_notifications, :target_id, :environment_id)
  11 + remove_column(:environment_notifications, :target_type)
  12 + end
  13 +end
... ...
plugins/environment_notification/lib/environment_notification_plugin.rb
... ... @@ -29,6 +29,19 @@ class EnvironmentNotificationPlugin &lt; Noosfero::Plugin
29 29 {:title => _('Notification Manager'), :url => {:controller => 'environment_notification_plugin_admin', :action => 'index'}}
30 30 end
31 31  
  32 + def control_panel_buttons
  33 + if context.profile.organization?
  34 + {
  35 + :title => _('Manage Notifications'),
  36 + :icon => 'important',
  37 + :url => {
  38 + :controller => 'environment_notification_plugin_myprofile',
  39 + :action => 'index'
  40 + }
  41 + }
  42 + end
  43 + end
  44 +
32 45 def account_controller_filters
33 46 block = proc do
34 47 if !logged_in?
... ...
plugins/environment_notification/lib/ext/organization.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +require_dependency 'organization'
  2 +
  3 +class Organization
  4 + has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification'
  5 +end
... ...
plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb
... ... @@ -9,13 +9,15 @@ class EnvironmentNotificationPlugin::EnvironmentNotification &lt; ActiveRecord::Bas
9 9 "EnvironmentNotificationPlugin::DangerNotification"
10 10 ]
11 11  
12   - attr_accessible :message, :environment_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title
  12 + attr_accessible :message, :target_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title
13 13  
14 14 has_many :environment_notifications_users
15 15 has_many :users, :through => :environment_notifications_users
16 16  
  17 + belongs_to :target, :polymorphic => true
  18 +
17 19 validates_presence_of :message
18   - validates_presence_of :environment_id
  20 + validates_presence_of :target_id
19 21 validate :notification_type_must_be_in_type_list
20 22  
21 23 def notification_type_must_be_in_type_list
... ... @@ -24,10 +26,11 @@ class EnvironmentNotificationPlugin::EnvironmentNotification &lt; ActiveRecord::Bas
24 26 end
25 27 end
26 28  
27   - scope :active, lambda{|environment| { :conditions => { :environment_id => environment.id, :active => true } } }
  29 + scope :active, lambda{|target| { :conditions => { :target_id => target.id, :active => true } } }
28 30  
29   - def self.visibles(environment, user, controller_path)
30   - notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(environment).order('updated_at DESC')
  31 + # TODO: Change method body to scopes
  32 + def self.visibles(target, user, controller_path)
  33 + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(target).order('updated_at DESC')
31 34  
32 35 if user
33 36 active_notifications_ids = notifications.pluck(:id) - user.environment_notifications.pluck(:id)
... ... @@ -44,7 +47,7 @@ class EnvironmentNotificationPlugin::EnvironmentNotification &lt; ActiveRecord::Bas
44 47 notifications
45 48 end
46 49  
47   - def self.with_popup(environment, user, previous_path)
48   - notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(environment, user, previous_path).where(display_popup: true)
  50 + def self.with_popup(target, user, previous_path)
  51 + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(target, user, previous_path).where(display_popup: true)
49 52 end
50 53 end
... ...