[noosfero/noosfero][master] 2 commits: Handle related suggestions when a profile is destroyed

Daniela Feitosa gitlab at gitlab.com
Wed Jun 3 08:40:22 BRT 2015


Daniela Feitosa pushed to branch master at Noosfero / noosfero


Commits:
4aa45dd8 by Arthur Del Esposte at 2015-06-03T08:15:26Z
Handle related suggestions when a profile is destroyed

- - - - -
4de02a20 by Arthur Del Esposte at 2015-06-03T08:15:26Z
Add migration to remove broken profile suggestions

- - - - -


11 changed files:

- app/controllers/my_profile/friends_controller.rb
- app/controllers/my_profile/memberships_controller.rb
- app/models/add_friend.rb
- app/models/person.rb
- app/models/profile.rb
- app/models/profile_suggestion.rb
- + db/migrate/20150507204849_remove_broken_profile_suggestions.rb
- test/functional/friends_controller_test.rb
- test/functional/memberships_controller_test.rb
- test/unit/profile_suggestion_test.rb
- test/unit/profile_test.rb


Changes:

=====================================
app/controllers/my_profile/friends_controller.rb
=====================================
--- a/app/controllers/my_profile/friends_controller.rb
+++ b/app/controllers/my_profile/friends_controller.rb
@@ -1,9 +1,9 @@
 class FriendsController < MyProfileController
-  
+
   protect 'manage_friends', :profile
-  
+
   def index
-    @suggestions = profile.profile_suggestions.of_person.enabled.includes(:suggestion).limit(per_page)
+    @suggestions = profile.suggested_profiles.of_person.enabled.includes(:suggestion).limit(per_page)
     if is_cache_expired?(profile.manage_friends_cache_key(params))
       @friends = profile.friends.paginate(:per_page => per_page, :page => params[:npage])
     end
@@ -18,7 +18,7 @@ class FriendsController < MyProfileController
   end
 
   def suggest
-    @suggestions = profile.profile_suggestions.of_person.enabled.includes(:suggestion).limit(per_page)
+    @suggestions = profile.suggested_profiles.of_person.enabled.includes(:suggestion).limit(per_page)
   end
 
   def remove_suggestion
@@ -26,13 +26,13 @@ class FriendsController < MyProfileController
     redirect_to :action => 'suggest' unless @person
     if @person && request.post?
       profile.remove_suggestion(@person)
-      @suggestions = profile.profile_suggestions.of_person.enabled.includes(:suggestion).limit(per_page)
+      @suggestions = profile.suggested_profiles.of_person.enabled.includes(:suggestion).limit(per_page)
       render :partial => 'shared/profile_suggestions_list', :locals => { :suggestions => @suggestions, :collection => :friends_suggestions, :per_page => params[:per_page] || per_page }
     end
   end
 
   def connections
-    @suggestion = profile.profile_suggestions.of_person.enabled.find_by_suggestion_id(params[:id])
+    @suggestion = profile.suggested_profiles.of_person.enabled.find_by_suggestion_id(params[:id])
     if @suggestion
       @tags = @suggestion.tag_connections
       @profiles = @suggestion.profile_connections


=====================================
app/controllers/my_profile/memberships_controller.rb
=====================================
--- a/app/controllers/my_profile/memberships_controller.rb
+++ b/app/controllers/my_profile/memberships_controller.rb
@@ -40,7 +40,7 @@ class MembershipsController < MyProfileController
   end
 
   def suggest
-    @suggestions = profile.profile_suggestions.of_community.enabled.includes(:suggestion).limit(per_page)
+    @suggestions = profile.suggested_profiles.of_community.enabled.includes(:suggestion).limit(per_page)
   end
 
   def remove_suggestion
@@ -49,13 +49,13 @@ class MembershipsController < MyProfileController
     redirect_to :action => 'suggest' unless @community
     if @community && request.post?
       profile.remove_suggestion(@community)
-      @suggestions = profile.profile_suggestions.of_community.enabled.includes(:suggestion).limit(custom_per_page)
+      @suggestions = profile.suggested_profiles.of_community.enabled.includes(:suggestion).limit(custom_per_page)
       render :partial => 'shared/profile_suggestions_list', :locals => { :suggestions => @suggestions, :collection => :communities_suggestions, :per_page => custom_per_page}
     end
   end
 
   def connections
-    @suggestion = profile.profile_suggestions.of_community.enabled.find_by_suggestion_id(params[:id])
+    @suggestion = profile.suggested_profiles.of_community.enabled.find_by_suggestion_id(params[:id])
     if @suggestion
       @tags = @suggestion.tag_connections
       @profiles = @suggestion.profile_connections


=====================================
app/models/add_friend.rb
=====================================
--- a/app/models/add_friend.rb
+++ b/app/models/add_friend.rb
@@ -54,7 +54,7 @@ class AddFriend < Task
   end
 
   def remove_from_suggestion_list(task)
-    suggestion = task.requestor.profile_suggestions.find_by_suggestion_id task.target.id
+    suggestion = task.requestor.suggested_profiles.find_by_suggestion_id task.target.id
     suggestion.disable if suggestion
   end
 end


=====================================
app/models/person.rb
=====================================
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -84,9 +84,9 @@ roles] }
   has_and_belongs_to_many :acepted_forums, :class_name => 'Forum', :join_table => 'terms_forum_people'
   has_and_belongs_to_many :articles_with_access, :class_name => 'Article', :join_table => 'article_privacy_exceptions'
 
-  has_many :profile_suggestions, :foreign_key => :person_id, :order => 'score DESC', :dependent => :destroy
-  has_many :suggested_people, :through => :profile_suggestions, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Person', true]
-  has_many :suggested_communities, :through => :profile_suggestions, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Community', true]
+  has_many :suggested_profiles, :class_name => 'ProfileSuggestion', :foreign_key => :person_id, :order => 'score DESC', :dependent => :destroy
+  has_many :suggested_people, :through => :suggested_profiles, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Person', true]
+  has_many :suggested_communities, :through => :suggested_profiles, :source => :suggestion, :conditions => ['profile_suggestions.suggestion_type = ? AND profile_suggestions.enabled = ?', 'Community', true]
 
   scope :more_popular, :order => 'friends_count DESC'
 
@@ -524,7 +524,7 @@ roles] }
   end
 
   def remove_suggestion(profile)
-    suggestion = profile_suggestions.find_by_suggestion_id profile.id
+    suggestion = suggested_profiles.find_by_suggestion_id profile.id
     suggestion.disable if suggestion
   end
 


=====================================
app/models/profile.rb
=====================================
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -1033,7 +1033,7 @@ private :generate_url, :url_options
   end
 
   def remove_from_suggestion_list(person)
-    suggestion = person.profile_suggestions.find_by_suggestion_id self.id
+    suggestion = person.suggested_profiles.find_by_suggestion_id self.id
     suggestion.disable if suggestion
   end
 


=====================================
app/models/profile_suggestion.rb
=====================================
--- a/app/models/profile_suggestion.rb
+++ b/app/models/profile_suggestion.rb
@@ -113,14 +113,14 @@ class ProfileSuggestion < ActiveRecord::Base
     suggested_profiles = all_suggestions(person)
     return if suggested_profiles.nil?
 
-    already_suggested_profiles = person.profile_suggestions.map(&:suggestion_id).join(',')
+    already_suggested_profiles = person.suggested_profiles.map(&:suggestion_id).join(',')
     suggested_profiles = suggested_profiles.where("profiles.id NOT IN (#{already_suggested_profiles})") if already_suggested_profiles.present?
     #TODO suggested_profiles = suggested_profiles.order('score DESC')
     suggested_profiles = suggested_profiles.limit(N_SUGGESTIONS)
     return if suggested_profiles.blank?
 
     suggested_profiles.each do |suggested_profile|
-      suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(suggested_profile.id)
+      suggestion = person.suggested_profiles.find_or_initialize_by_suggestion_id(suggested_profile.id)
       RULES.each do |rule, options|
         begin
           value = suggested_profile.send("#{rule}_count").to_i
@@ -273,7 +273,7 @@ class ProfileSuggestion < ActiveRecord::Base
   end
 
   def self.generate_profile_suggestions(person, force = false)
-    return if person.profile_suggestions.enabled.count >= MIN_LIMIT && !force
+    return if person.suggested_profiles.enabled.count >= MIN_LIMIT && !force
     Delayed::Job.enqueue ProfileSuggestionsJob.new(person.id) unless ProfileSuggestionsJob.exists?(person.id)
   end
 


=====================================
db/migrate/20150507204849_remove_broken_profile_suggestions.rb
=====================================
--- /dev/null
+++ b/db/migrate/20150507204849_remove_broken_profile_suggestions.rb
@@ -0,0 +1,9 @@
+class RemoveBrokenProfileSuggestions < ActiveRecord::Migration
+  def up
+    execute("DELETE FROM profile_suggestions WHERE suggestion_id NOT IN (SELECT id from profiles)")
+  end
+
+  def down
+    say "this migration can't be reverted"
+  end
+end


=====================================
test/functional/friends_controller_test.rb
=====================================
--- a/test/functional/friends_controller_test.rb
+++ b/test/functional/friends_controller_test.rb
@@ -46,7 +46,7 @@ class FriendsControllerTest < ActionController::TestCase
 
   should 'display find people button' do
     get :index, :profile => 'testuser'
-    assert_tag :tag => 'a', :content => 'Find people', :attributes => { :href => '/assets/people' }
+    assert_tag :tag => 'a', :content => 'Find people', :attributes => { :href => '/search/assets?asset=people' }
   end
 
   should 'not display invite friends button if any plugin tells not to' do
@@ -76,25 +76,25 @@ class FriendsControllerTest < ActionController::TestCase
   end
 
   should 'display people suggestions' do
-    profile.profile_suggestions.create(:suggestion => friend)
+    profile.suggested_profiles.create(:suggestion => friend)
     get :suggest, :profile => 'testuser'
     assert_tag :tag => 'a', :content => "+ #{friend.name}", :attributes => { :href => "/profile/#{friend.identifier}/add" }
   end
 
   should 'display button to add friend suggestion' do
-    profile.profile_suggestions.create(:suggestion => friend)
+    profile.suggested_profiles.create(:suggestion => friend)
     get :suggest, :profile => 'testuser'
     assert_tag :tag => 'a', :attributes => { :href => "/profile/#{friend.identifier}/add" }
   end
 
   should 'display button to remove people suggestion' do
-    profile.profile_suggestions.create(:suggestion => friend)
+    profile.suggested_profiles.create(:suggestion => friend)
     get :suggest, :profile => 'testuser'
     assert_tag :tag => 'a', :attributes => { :href => /\/myprofile\/testuser\/friends\/remove_suggestion\/#{friend.identifier}/ }
   end
 
   should 'remove suggestion of friend' do
-    suggestion = profile.profile_suggestions.create(:suggestion => friend)
+    suggestion = profile.suggested_profiles.create(:suggestion => friend)
     post :remove_suggestion, :profile => 'testuser', :id => friend.identifier
 
     assert_response :success


=====================================
test/functional/memberships_controller_test.rb
=====================================
--- a/test/functional/memberships_controller_test.rb
+++ b/test/functional/memberships_controller_test.rb
@@ -331,35 +331,35 @@ class MembershipsControllerTest < ActionController::TestCase
 
   should 'display list suggestions button' do
     community = fast_create(Community)
-    profile.profile_suggestions.create(:suggestion => community)
+    profile.suggested_profiles.create(:suggestion => community)
     get :index, :profile => 'testuser'
     assert_tag :tag => 'a', :content => 'See some suggestions of communities...', :attributes => { :href => "/myprofile/testuser/memberships/suggest" }
   end
 
   should 'display communities suggestions' do
     community = fast_create(Community)
-    profile.profile_suggestions.create(:suggestion => community)
+    profile.suggested_profiles.create(:suggestion => community)
     get :suggest, :profile => 'testuser'
     assert_tag :tag => 'a', :content => "+ #{community.name}", :attributes => { :href => "/profile/#{community.identifier}/join" }
   end
 
   should 'display button to join on community suggestion' do
     community = fast_create(Community)
-    profile.profile_suggestions.create(:suggestion => community)
+    profile.suggested_profiles.create(:suggestion => community)
     get :suggest, :profile => 'testuser'
     assert_tag :tag => 'a', :attributes => { :href => "/profile/#{community.identifier}/join" }
   end
 
   should 'display button to remove community suggestion' do
     community = fast_create(Community)
-    profile.profile_suggestions.create(:suggestion => community)
+    profile.suggested_profiles.create(:suggestion => community)
     get :suggest, :profile => 'testuser'
     assert_tag :tag => 'a', :attributes => { :href => /\/myprofile\/testuser\/memberships\/remove_suggestion\/#{community.identifier}/ }
   end
 
   should 'remove suggestion of community' do
     community = fast_create(Community)
-    suggestion = profile.profile_suggestions.create(:suggestion => community)
+    suggestion = profile.suggested_profiles.create(:suggestion => community)
     post :remove_suggestion, :profile => 'testuser', :id => community.identifier
 
     assert_response :success


=====================================
test/unit/profile_suggestion_test.rb
=====================================
--- a/test/unit/profile_suggestion_test.rb
+++ b/test/unit/profile_suggestion_test.rb
@@ -142,7 +142,7 @@ class ProfileSuggestionTest < ActiveSupport::TestCase
     p2 = create_user('testuser2').person
     p3 = create_user('testuser3').person
     p4 = create_user('testuser4').person
-    p5 = create_user('testuser4').person
+    p5 = create_user('testuser5').person
 
     p1.add_friend(p2)
     p1.add_friend(p3)
@@ -212,8 +212,8 @@ class ProfileSuggestionTest < ActiveSupport::TestCase
 
     ProfileSuggestion.expects(:calculate_suggestions)
 
-    person.profile_suggestions.enabled.last.disable
-    person.profile_suggestions.enabled.last.destroy
+    person.suggested_profiles.enabled.last.disable
+    person.suggested_profiles.enabled.last.destroy
     process_delayed_job_queue
   end
 


=====================================
test/unit/profile_test.rb
=====================================
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -2139,6 +2139,16 @@ class ProfileTest < ActiveSupport::TestCase
     assert_equal false, ProfileSuggestion.find(suggestion.id).enabled
   end
 
+  should 'destroy related suggestion if profile is destroyed' do
+    person = fast_create(Person)
+    suggested_person = fast_create(Person)
+    suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_person, :enabled => true)
+
+    assert_difference 'ProfileSuggestion.find_all_by_suggestion_id(suggested_person.id).count', -1 do
+      suggested_person.destroy
+    end
+  end
+
   should 'enable profile visibility' do
     profile = fast_create(Profile)
 



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/b4000b019a691d08b639fcf8ccbe5bd866bada05...4de02a20556018c58a13de42b258c3b2e779ce09
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150603/92a7ca19/attachment-0001.html>


More information about the Noosfero-dev mailing list