Permalink
Browse files

Extract Decorator Step Six

* Flip composition
  • Loading branch information...
jferris committed Feb 21, 2013
1 parent 61ca678 commit 876ec976aa63e48cdb648cd4a647ade0fcce5ff3
@@ -9,8 +9,8 @@ class Survey < ActiveRecord::Base

def summaries_using(summarizer, options = {})
questions.map do |question|
UnansweredQuestionHider.new(summarizer, options[:answered_by]).
summarize(question)
hider = UnansweredQuestionHider.new(summarizer, options[:answered_by])
question.summary_using(hider)
end
end
end
@@ -10,14 +10,14 @@ def summarize(question)
if hide_unanswered_question?(question)
hide_answer_to_question(question)
else
question.summary_using(@summarizer)
@summarizer.summarize(question)
end
end

private

def hide_answer_to_question(question)
Summary.new(question.title, NO_ANSWER)
NO_ANSWER
end

def hide_unanswered_question?(question)
@@ -9,36 +9,33 @@

result = hider.summarize(question)

result.title.should eq question.title
result.value.should eq UnansweredQuestionHider::NO_ANSWER
result.should eq UnansweredQuestionHider::NO_ANSWER
end

it 'delegates to the summarizer given a user with an answer' do
summary = stub('summary')
user = build_stubbed(:user)
question = stub_answered_question(user, true)
summarizer = stub_summarizer(question, summary)
summarizer = stub_summarizer(question, 'value')
hider = UnansweredQuestionHider.new(summarizer, user)

result = hider.summarize(question)

result.should eq summary
result.should eq 'value'
end

it 'delegates to the summarizer without a user' do
summary = stub('summary')
question = build_stubbed(:question)
summarizer = stub_summarizer(question, summary)
summarizer = stub_summarizer(question, 'value')
hider = UnansweredQuestionHider.new(summarizer, nil)

result = hider.summarize(question)

result.should eq summary
result.should eq 'value'
end

def stub_summarizer(question, summary)
def stub_summarizer(question, value)
stub('summarizer').tap do |summarizer|
question.stubs(:summary_using).with(summarizer).returns(summary)
summarizer.stubs(:summarize).with(question).returns(value)
end
end

0 comments on commit 876ec97

Please sign in to comment.