Commit cea3d3b0e4918c5a1a6cc4ab552f62c5ff706558

Authored by Marcos Pereira
1 parent d23ec096

adds methods/scope to models, followers group, tests

app/jobs/notify_activity_to_profiles_job.rb
... ... @@ -20,7 +20,7 @@ class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id)
20 20 ActionTrackerNotification.create(:profile_id => tracked_action.user.id, :action_tracker_id => tracked_action.id)
21 21  
22 22 # Notify all friends
23   - ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})")
  23 + #ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})")
24 24  
25 25 # Notify all followers
26 26 ActionTrackerNotification.connection.execute("INSERT INTO action_tracker_notifications(profile_id, action_tracker_id) SELECT f.follower_id, #{tracked_action.id} FROM profile_followers AS f WHERE profile_id=#{tracked_action.user.id} AND (f.follower_id NOT IN (SELECT atn.profile_id FROM action_tracker_notifications AS atn WHERE atn.action_tracker_id = #{tracked_action.id})) AND (f.follower_id IN SELECT friendships.friend_id FROM friendships WHERE friendships.person_id = #{tracked_action.user.id} OR SELECT profiles.public FROM profiles where id=#{tracked_action.user.id})")
... ...
app/models/friendship.rb
... ... @@ -14,6 +14,7 @@ class Friendship < ApplicationRecord
14 14 after_destroy do |friendship|
15 15 Friendship.update_cache_counter(:friends_count, friendship.person, -1)
16 16 Friendship.update_cache_counter(:friends_count, friendship.friend, -1)
  17 + friendship.person.unfollow(friendship.friend)
17 18 end
18 19  
19 20 def self.remove_friendship(person1, person2)
... ...
app/models/person.rb
... ... @@ -196,7 +196,17 @@ class Person < Profile
196 196 friendship = self.friendships.build
197 197 friendship.friend = friend
198 198 friendship.group = group
199   - friendship.save
  199 + follow(friend, group) if friendship.save
  200 + end
  201 + end
  202 +
  203 + def follow(profile, group = nil)
  204 + unless self.following_profiles.include?(profile)
  205 + profile_follower = ProfileFollower.new
  206 + profile_follower.profile = profile
  207 + profile_follower.follower = self
  208 + profile_follower.group = group
  209 + profile_follower.save
200 210 end
201 211 end
202 212  
... ... @@ -208,6 +218,10 @@ class Person < Profile
208 218 Friendship.where(friend_id: friend, person_id: id).first.destroy
209 219 end
210 220  
  221 + def unfollow(profile)
  222 + ProfileFollower.where(follower_id: id, profile_id: profile.id).first.destroy
  223 + end
  224 +
211 225 FIELDS = %w[
212 226 description
213 227 image
... ...
app/models/profile_follower.rb
... ... @@ -5,5 +5,6 @@ class ProfileFollower < ApplicationRecord
5 5 belongs_to :follower, :class_name => 'Person', :foreign_key => :follower_id
6 6  
7 7 validates_presence_of :profile_id, :follower_id
  8 + validates :profile_id, :uniqueness => {:scope => :follower_id, :message => "can't follow twice the same profile"}
8 9  
9 10 end
... ...
db/migrate/20160608123748_create_profile_followers_table.rb
1 1 class CreateProfileFollowersTable < ActiveRecord::Migration
2 2 def change
3 3 create_table :profile_followers do |t|
4   - t.column :profile_id, :integer
5   - t.column :follower_id, :integer
  4 + t.column :profile_id, :integer
  5 + t.column :follower_id, :integer
  6 + t.column :group, :string
6 7 end
7 8 end
8 9 end
... ...
test/unit/friendship_test.rb
... ... @@ -72,4 +72,53 @@ class FriendshipTest &lt; ActiveSupport::TestCase
72 72 assert_not_includes p2.friends(true), p1
73 73 end
74 74  
  75 + should 'add follower when adding friend' do
  76 + p1 = create_user('testuser1').person
  77 + p2 = create_user('testuser2').person
  78 +
  79 + assert_difference 'ProfileFollower.count', 2 do
  80 + p1.add_friend(p2, 'friends')
  81 + p2.add_friend(p1, 'friends')
  82 + end
  83 +
  84 + assert_includes p1.followers(true), p2
  85 + assert_includes p2.followers(true), p1
  86 + end
  87 +
  88 + should 'remove follower when a friend removal occurs' do
  89 + p1 = create_user('testuser1').person
  90 + p2 = create_user('testuser2').person
  91 +
  92 + p1.add_friend(p2, 'friends')
  93 + p2.add_friend(p1, 'friends')
  94 +
  95 + Friendship.remove_friendship(p1, p2)
  96 +
  97 + assert_not_includes p1.followers(true), p2
  98 + assert_not_includes p2.followers(true), p1
  99 + end
  100 +
  101 + should 'keep friendship intact when stop following' do
  102 + p1 = create_user('testuser1').person
  103 + p2 = create_user('testuser2').person
  104 +
  105 + p1.add_friend(p2, 'friends')
  106 + p2.add_friend(p1, 'friends')
  107 +
  108 + p1.unfollow(p2)
  109 +
  110 + assert_includes p1.friends(true), p2
  111 + assert_includes p2.friends(true), p1
  112 + end
  113 +
  114 + should 'do not add friendship when start following' do
  115 + p1 = create_user('testuser1').person
  116 + p2 = create_user('testuser2').person
  117 +
  118 + p1.follow(p2, 'favorites')
  119 + p2.follow(p1, 'favorites')
  120 +
  121 + assert_not_includes p1.friends(true), p2
  122 + assert_not_includes p2.friends(true), p1
  123 + end
75 124 end
... ...
test/unit/profile_followers_test.rb 0 → 100644
... ... @@ -0,0 +1,55 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class ProfileFollowersTest < ActiveSupport::TestCase
  4 +
  5 + should 'a person follow another' do
  6 + p1 = create_user('person_test').person
  7 + p2 = create_user('person_test_2').person
  8 +
  9 + assert_difference 'ProfileFollower.count' do
  10 + p1.follow(p2)
  11 + end
  12 +
  13 + assert_includes p2.followers(true), p1
  14 + assert_not_includes p1.followers(true), p2
  15 + end
  16 +
  17 +
  18 + should 'a person unfollow another person' do
  19 + p1 = create_user('person_test').person
  20 + p2 = create_user('person_test_2').person
  21 +
  22 + p1.follow(p2)
  23 +
  24 + assert_difference 'ProfileFollower.count', -1 do
  25 + p1.unfollow(p2)
  26 + end
  27 +
  28 + assert_not_includes p2.followers(true), p1
  29 + end
  30 +
  31 + should 'get the followed persons for a profile' do
  32 + p1 = create_user('person_test').person
  33 + p2 = create_user('person_test_2').person
  34 + p3 = create_user('person_test_3').person
  35 +
  36 + p1.follow(p2)
  37 + p1.follow(p3)
  38 +
  39 + assert_equivalent p1.following_profiles, [p2,p3]
  40 + assert_equivalent Profile.following_profiles(p1), [p2,p3]
  41 + end
  42 +
  43 + should 'not follow same person twice' do
  44 + p1 = create_user('person_test').person
  45 + p2 = create_user('person_test_2').person
  46 +
  47 + assert_difference 'ProfileFollower.count' do
  48 + p1.follow(p2)
  49 + p1.follow(p2)
  50 + end
  51 +
  52 + assert_equivalent p1.following_profiles, [p2]
  53 + assert_equivalent p2.followers, [p1]
  54 + end
  55 +end
... ...