Skip to content

Commit 9d0274f

Browse files
committed
Extract Decorator Step Two
* Move conditional method into decorator
1 parent af2e831 commit 9d0274f

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎example_app/app/models/survey.rb‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ def summaries_using(summarizer, options = {})
1616
private
1717

1818
def summary_or_hidden_answer(summarizer, question, answered_by)
19-
if hide_unanswered_question?(question, answered_by)
20-
UnansweredQuestionHider.new.hide_answer_to_question(question)
19+
hider = UnansweredQuestionHider.new
20+
if hider.hide_unanswered_question?(question, answered_by)
21+
hider.hide_answer_to_question(question)
2122
else
2223
question.summary_using(summarizer)
2324
end
2425
end
25-
26-
def hide_unanswered_question?(question, answered_by)
27-
answered_by && !question.answered_by?(answered_by)
28-
end
2926
end

‎example_app/app/models/unanswered_question_hider.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class UnansweredQuestionHider
44
def hide_answer_to_question(question)
55
Summary.new(question.title, NO_ANSWER)
66
end
7+
8+
def hide_unanswered_question?(question, user)
9+
user && !question.answered_by?(user)
10+
end
711
end

‎example_app/spec/models/unanswered_question_hider_spec.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,34 @@
1111
result.value.should eq UnansweredQuestionHider::NO_ANSWER
1212
end
1313
end
14+
15+
describe UnansweredQuestionHider, '#hide_unanswered_question?' do
16+
it 'returns true given a user without an answer' do
17+
user = build_stubbed(:user)
18+
question = stub_answered_question(user, false)
19+
hider = UnansweredQuestionHider.new
20+
21+
hider.hide_unanswered_question?(question, user).should be_true
22+
end
23+
24+
it 'returns false given a user with an answer' do
25+
user = build_stubbed(:user)
26+
question = stub_answered_question(user, true)
27+
hider = UnansweredQuestionHider.new
28+
29+
hider.hide_unanswered_question?(question, user).should be_false
30+
end
31+
32+
it 'returns false without a user' do
33+
question = build_stubbed(:question)
34+
hider = UnansweredQuestionHider.new
35+
36+
hider.hide_unanswered_question?(question, nil).should be_false
37+
end
38+
39+
def stub_answered_question(user, answered)
40+
build_stubbed(:question).tap do |question|
41+
question.stubs(:answered_by?).with(user).returns(answered)
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)