File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
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
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff line change 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}
You can’t perform that action at this time.
0 commit comments