Skip to content

Commit 6e6265d

Browse files
committed
Added safeguards on (un)?following.
User pages now only show one un?following link. And give a message if you're already un?following someone and you try it again.
1 parent e7630a1 commit 6e6265d

3 files changed

Lines changed: 28 additions & 6 deletions

File tree

controllers/hacker.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#find the hacker with the given name
1919
@hacker = Hacker.first(:username => params[:name])
2020

21+
#make sure we're not following them already
22+
if current_user.following? @hacker
23+
flash[:notice] = "You're already following #{params[:name]}."
24+
redirect "/hackers/#{current_user.username}"
25+
return
26+
end
27+
2128
#follow them!
2229
current_user.follow! @hacker
2330

@@ -36,8 +43,15 @@
3643

3744
#find the hacker with the given name
3845
@hacker = Hacker.first(:username => params[:name])
39-
40-
#follow them!
46+
47+
#make sure we're not following them already
48+
unless current_user.following? @hacker
49+
flash[:notice] = "You're already not following #{params[:name]}."
50+
redirect "/hackers/#{current_user.username}"
51+
return
52+
end
53+
54+
#unfollow them!
4155
current_user.unfollow! @hacker
4256

4357
#set a message

models/hacker.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,28 @@ def gravatar_url
7575
"http://www.gravatar.com/avatar/#{MD5::md5(email.downcase)}"
7676
end
7777

78-
#this function makes the hacker follow the followee
78+
#this method makes the hacker follow the followee
7979
def follow! followee
8080
following << followee
8181
save
8282
followee.followers << self
8383
followee.save
8484
end
8585

86-
#this function makes the hacker unfollow the followee
86+
#this method makes the hacker unfollow the followee
8787
def unfollow! followee
8888
following_ids.delete(followee.id)
8989
save
9090
followee.followers_ids.delete(id)
9191
followee.save
9292
end
9393

94+
#this method returns true if we're following the given Hacker, and
95+
#false otherwise
96+
def following? hacker
97+
following.include? hacker
98+
end
99+
94100
private
95101

96102
#we're going to use the SHA1 encryption method for now.

views/hackers/show.haml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66

77
- if logged_in? && @hacker.username != current_user.username
88
%a{:href => "/messages/new/to/#{@hacker.username}" }= "Send #{@hacker.username} a message"
9-
%a{:href => "/hackers/#{@hacker.username}/follow"} Follow #{@hacker.username}
10-
%a{:href => "/hackers/#{@hacker.username}/unfollow"} Unfollow #{@hacker.username}
9+
- if current_user.following? @hacker
10+
%a{:href => "/hackers/#{@hacker.username}/unfollow"} Unfollow #{@hacker.username}
11+
- else
12+
%a{:href => "/hackers/#{@hacker.username}/follow"} Follow #{@hacker.username}

0 commit comments

Comments
 (0)