From 96313d04a4c9211646bc23bc7f226b98b5198a4d Mon Sep 17 00:00:00 2001 From: tcocca Date: Tue, 29 Jun 2010 22:13:52 -0400 Subject: [PATCH] Should not be able to follow yourself (cherry picking from commit 861d276f034f027420c8f698a0fba0a7417e2dd3) --- lib/acts_as_follower.rb | 4 +++- test/acts_as_follower_test.rb | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/acts_as_follower.rb b/lib/acts_as_follower.rb index eb2f7ea..a243610 100644 --- a/lib/acts_as_follower.rb +++ b/lib/acts_as_follower.rb @@ -38,7 +38,9 @@ module ActiveRecord #:nodoc: def follow(followable) follow = get_follow(followable) unless follow - Follow.create(:followable => followable, :follower => self) + if self != followable + Follow.create(:followable => followable, :follower => self) + end end end diff --git a/test/acts_as_follower_test.rb b/test/acts_as_follower_test.rb index eb2dae5..f1bf204 100644 --- a/test/acts_as_follower_test.rb +++ b/test/acts_as_follower_test.rb @@ -38,7 +38,7 @@ class ActsAsFollowerTest < Test::Unit::TestCase end end - context "follow" do + context "follow a friend" do setup do @jon.follow(@sam) end @@ -55,6 +55,23 @@ class ActsAsFollowerTest < Test::Unit::TestCase end end + context "follow yourself" do + setup do + @jon.follow(@jon) + end + + should_not_change("Follow count") { Follow.count } + should_not_change("@jon.follow_count") { @jon.follow_count } + + should "not set the follower" do + assert_not_equal @jon, Follow.last.follower + end + + should "not set the followable" do + assert_not_equal @jon, Follow.last.followable + end + end + context "stop_following" do setup do @sam.stop_following(@jon) -- libgit2 0.21.0