Skip to content

Commit 4fd00a8

Browse files
committed
Extract Decorator Step Three
* Move decorator body into decorator
1 parent 9d0274f commit 4fd00a8

3 files changed

Lines changed: 39 additions & 30 deletions

File tree

example_app/app/models/survey.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@ class Survey < ActiveRecord::Base
99

1010
def summaries_using(summarizer, options = {})
1111
questions.map do |question|
12-
summary_or_hidden_answer(summarizer, question, options[:answered_by])
13-
end
14-
end
15-
16-
private
17-
18-
def summary_or_hidden_answer(summarizer, question, answered_by)
19-
hider = UnansweredQuestionHider.new
20-
if hider.hide_unanswered_question?(question, answered_by)
21-
hider.hide_answer_to_question(question)
22-
else
23-
question.summary_using(summarizer)
12+
UnansweredQuestionHider.new.summary_or_hidden_answer(
13+
summarizer,
14+
question,
15+
options[:answered_by]
16+
)
2417
end
2518
end
2619
end

example_app/app/models/unanswered_question_hider.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
class UnansweredQuestionHider
22
NO_ANSWER = "You haven't answered this question".freeze
33

4+
def summary_or_hidden_answer(summarizer, question, user)
5+
if hide_unanswered_question?(question, user)
6+
hide_answer_to_question(question)
7+
else
8+
question.summary_using(summarizer)
9+
end
10+
end
11+
12+
private
13+
414
def hide_answer_to_question(question)
515
Summary.new(question.title, NO_ANSWER)
616
end

example_app/spec/models/unanswered_question_hider_spec.rb

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
require 'spec_helper'
22

3-
describe UnansweredQuestionHider, '#hide_answer_to_question' do
4-
it 'returns a hidden summary' do
5-
question = build_stubbed(:question)
3+
describe UnansweredQuestionHider, '#summary_or_hidden_answer' do
4+
it 'returns a hidden summary given a user without an answer' do
5+
summarizer = stub('summarizer')
6+
user = build_stubbed(:user)
7+
question = stub_answered_question(user, false)
68
hider = UnansweredQuestionHider.new
79

8-
result = hider.hide_answer_to_question(question)
10+
result = hider.summary_or_hidden_answer(summarizer, question, user)
911

1012
result.title.should eq question.title
1113
result.value.should eq UnansweredQuestionHider::NO_ANSWER
1214
end
13-
end
1415

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
16+
it 'delegates to the summarizer given a user with an answer' do
17+
summary = stub('summary')
2518
user = build_stubbed(:user)
2619
question = stub_answered_question(user, true)
20+
summarizer = stub_summarizer(question, summary)
2721
hider = UnansweredQuestionHider.new
2822

29-
hider.hide_unanswered_question?(question, user).should be_false
23+
result = hider.summary_or_hidden_answer(summarizer, question, user)
24+
25+
result.should eq summary
3026
end
3127

32-
it 'returns false without a user' do
28+
it 'delegates to the summarizer without a user' do
29+
summary = stub('summary')
3330
question = build_stubbed(:question)
31+
summarizer = stub_summarizer(question, summary)
3432
hider = UnansweredQuestionHider.new
3533

36-
hider.hide_unanswered_question?(question, nil).should be_false
34+
result = hider.summary_or_hidden_answer(summarizer, question, nil)
35+
36+
result.should eq summary
37+
end
38+
39+
def stub_summarizer(question, summary)
40+
stub('summarizer').tap do |summarizer|
41+
question.stubs(:summary_using).with(summarizer).returns(summary)
42+
end
3743
end
3844

3945
def stub_answered_question(user, answered)

0 commit comments

Comments
 (0)