Skip to content

Commit 5c5276b

Browse files
abhiraosteveklabnik
authored andcommitted
Moving following logic to a policy class
Backed up by spec
1 parent d22fbaf commit 5c5276b

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

app/models/following_policy.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class FollowingPolicy
2+
def initialize(follower, target)
3+
@follower = follower
4+
@target = target
5+
end
6+
7+
def can_follow?
8+
!following_self? && !already_following?
9+
end
10+
11+
def already_following?
12+
@follower.following?(@target)
13+
end
14+
15+
def following_self?
16+
@follower == @target
17+
end
18+
end

spec/following_policy_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require_relative '../app/models/following_policy'
2+
3+
describe FollowingPolicy do
4+
before(:each) do
5+
@followed = stub()
6+
@follower = stub()
7+
end
8+
9+
it 'can follow another' do
10+
@follower.should_receive(:following?).with(@followed).and_return(false)
11+
policy = FollowingPolicy.new(@follower, @followed)
12+
policy.can_follow?.should be_true
13+
end
14+
15+
it "cannot follow self" do
16+
policy = FollowingPolicy.new(@follower, @follower)
17+
policy.can_follow?.should be_false
18+
end
19+
20+
it "cannot follow twice" do
21+
@follower.should_receive(:following?).with(@followed).and_return(true)
22+
policy = FollowingPolicy.new(@follower,@followed)
23+
policy.can_follow?.should be_false
24+
end
25+
26+
end

0 commit comments

Comments
 (0)