From cea3d3b0e4918c5a1a6cc4ab552f62c5ff706558 Mon Sep 17 00:00:00 2001 From: Marcos Ronaldo Date: Mon, 13 Jun 2016 11:31:23 -0300 Subject: [PATCH] adds methods/scope to models, followers group, tests --- app/jobs/notify_activity_to_profiles_job.rb | 2 +- app/models/friendship.rb | 1 + app/models/person.rb | 16 +++++++++++++++- app/models/profile_follower.rb | 1 + db/migrate/20160608123748_create_profile_followers_table.rb | 5 +++-- test/unit/friendship_test.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/profile_followers_test.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 test/unit/profile_followers_test.rb diff --git a/app/jobs/notify_activity_to_profiles_job.rb b/app/jobs/notify_activity_to_profiles_job.rb index a104bd2..0fabd89 100644 --- a/app/jobs/notify_activity_to_profiles_job.rb +++ b/app/jobs/notify_activity_to_profiles_job.rb @@ -20,7 +20,7 @@ class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id) ActionTrackerNotification.create(:profile_id => tracked_action.user.id, :action_tracker_id => tracked_action.id) # Notify all friends - 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})") + #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})") # Notify all followers 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})") diff --git a/app/models/friendship.rb b/app/models/friendship.rb index c497b1b..9630d4b 100644 --- a/app/models/friendship.rb +++ b/app/models/friendship.rb @@ -14,6 +14,7 @@ class Friendship < ApplicationRecord after_destroy do |friendship| Friendship.update_cache_counter(:friends_count, friendship.person, -1) Friendship.update_cache_counter(:friends_count, friendship.friend, -1) + friendship.person.unfollow(friendship.friend) end def self.remove_friendship(person1, person2) diff --git a/app/models/person.rb b/app/models/person.rb index eb87533..8a6d98e 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -196,7 +196,17 @@ class Person < Profile friendship = self.friendships.build friendship.friend = friend friendship.group = group - friendship.save + follow(friend, group) if friendship.save + end + end + + def follow(profile, group = nil) + unless self.following_profiles.include?(profile) + profile_follower = ProfileFollower.new + profile_follower.profile = profile + profile_follower.follower = self + profile_follower.group = group + profile_follower.save end end @@ -208,6 +218,10 @@ class Person < Profile Friendship.where(friend_id: friend, person_id: id).first.destroy end + def unfollow(profile) + ProfileFollower.where(follower_id: id, profile_id: profile.id).first.destroy + end + FIELDS = %w[ description image diff --git a/app/models/profile_follower.rb b/app/models/profile_follower.rb index c979e30..d389bdd 100644 --- a/app/models/profile_follower.rb +++ b/app/models/profile_follower.rb @@ -5,5 +5,6 @@ class ProfileFollower < ApplicationRecord belongs_to :follower, :class_name => 'Person', :foreign_key => :follower_id validates_presence_of :profile_id, :follower_id + validates :profile_id, :uniqueness => {:scope => :follower_id, :message => "can't follow twice the same profile"} end diff --git a/db/migrate/20160608123748_create_profile_followers_table.rb b/db/migrate/20160608123748_create_profile_followers_table.rb index 5419d40..e83e708 100644 --- a/db/migrate/20160608123748_create_profile_followers_table.rb +++ b/db/migrate/20160608123748_create_profile_followers_table.rb @@ -1,8 +1,9 @@ class CreateProfileFollowersTable < ActiveRecord::Migration def change create_table :profile_followers do |t| - t.column :profile_id, :integer - t.column :follower_id, :integer + t.column :profile_id, :integer + t.column :follower_id, :integer + t.column :group, :string end end end diff --git a/test/unit/friendship_test.rb b/test/unit/friendship_test.rb index 8ab5729..6ab087a 100644 --- a/test/unit/friendship_test.rb +++ b/test/unit/friendship_test.rb @@ -72,4 +72,53 @@ class FriendshipTest < ActiveSupport::TestCase assert_not_includes p2.friends(true), p1 end + should 'add follower when adding friend' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + + assert_difference 'ProfileFollower.count', 2 do + p1.add_friend(p2, 'friends') + p2.add_friend(p1, 'friends') + end + + assert_includes p1.followers(true), p2 + assert_includes p2.followers(true), p1 + end + + should 'remove follower when a friend removal occurs' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + + p1.add_friend(p2, 'friends') + p2.add_friend(p1, 'friends') + + Friendship.remove_friendship(p1, p2) + + assert_not_includes p1.followers(true), p2 + assert_not_includes p2.followers(true), p1 + end + + should 'keep friendship intact when stop following' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + + p1.add_friend(p2, 'friends') + p2.add_friend(p1, 'friends') + + p1.unfollow(p2) + + assert_includes p1.friends(true), p2 + assert_includes p2.friends(true), p1 + end + + should 'do not add friendship when start following' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + + p1.follow(p2, 'favorites') + p2.follow(p1, 'favorites') + + assert_not_includes p1.friends(true), p2 + assert_not_includes p2.friends(true), p1 + end end diff --git a/test/unit/profile_followers_test.rb b/test/unit/profile_followers_test.rb new file mode 100644 index 0000000..0c74578 --- /dev/null +++ b/test/unit/profile_followers_test.rb @@ -0,0 +1,55 @@ +require_relative "../test_helper" + +class ProfileFollowersTest < ActiveSupport::TestCase + + should 'a person follow another' do + p1 = create_user('person_test').person + p2 = create_user('person_test_2').person + + assert_difference 'ProfileFollower.count' do + p1.follow(p2) + end + + assert_includes p2.followers(true), p1 + assert_not_includes p1.followers(true), p2 + end + + + should 'a person unfollow another person' do + p1 = create_user('person_test').person + p2 = create_user('person_test_2').person + + p1.follow(p2) + + assert_difference 'ProfileFollower.count', -1 do + p1.unfollow(p2) + end + + assert_not_includes p2.followers(true), p1 + end + + should 'get the followed persons for a profile' do + p1 = create_user('person_test').person + p2 = create_user('person_test_2').person + p3 = create_user('person_test_3').person + + p1.follow(p2) + p1.follow(p3) + + assert_equivalent p1.following_profiles, [p2,p3] + assert_equivalent Profile.following_profiles(p1), [p2,p3] + end + + should 'not follow same person twice' do + p1 = create_user('person_test').person + p2 = create_user('person_test_2').person + + assert_difference 'ProfileFollower.count' do + p1.follow(p2) + p1.follow(p2) + end + + assert_equivalent p1.following_profiles, [p2] + assert_equivalent p2.followers, [p1] + end +end -- libgit2 0.21.2