Permalink
Browse files

Extract Decorator Step Three

* Move decorator body into decorator
  • Loading branch information...
jferris committed Feb 21, 2013
1 parent 9d0274f commit 4fd00a885cf8759b236c336dc68fda2ccf03c1c6
@@ -9,18 +9,11 @@ class Survey < ActiveRecord::Base

def summaries_using(summarizer, options = {})
questions.map do |question|
summary_or_hidden_answer(summarizer, question, options[:answered_by])
end
end

private

def summary_or_hidden_answer(summarizer, question, answered_by)
hider = UnansweredQuestionHider.new
if hider.hide_unanswered_question?(question, answered_by)
hider.hide_answer_to_question(question)
else
question.summary_using(summarizer)
UnansweredQuestionHider.new.summary_or_hidden_answer(
summarizer,
question,
options[:answered_by]
)
end
end
end
@@ -1,6 +1,16 @@
class UnansweredQuestionHider
NO_ANSWER = "You haven't answered this question".freeze

def summary_or_hidden_answer(summarizer, question, user)
if hide_unanswered_question?(question, user)
hide_answer_to_question(question)
else
question.summary_using(summarizer)
end
end

private

def hide_answer_to_question(question)
Summary.new(question.title, NO_ANSWER)
end
@@ -1,39 +1,45 @@
require 'spec_helper'

describe UnansweredQuestionHider, '#hide_answer_to_question' do
it 'returns a hidden summary' do
question = build_stubbed(:question)
describe UnansweredQuestionHider, '#summary_or_hidden_answer' do
it 'returns a hidden summary given a user without an answer' do
summarizer = stub('summarizer')
user = build_stubbed(:user)
question = stub_answered_question(user, false)
hider = UnansweredQuestionHider.new

result = hider.hide_answer_to_question(question)
result = hider.summary_or_hidden_answer(summarizer, question, user)

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

describe UnansweredQuestionHider, '#hide_unanswered_question?' do
it 'returns true given a user without an answer' do
user = build_stubbed(:user)
question = stub_answered_question(user, false)
hider = UnansweredQuestionHider.new

hider.hide_unanswered_question?(question, user).should be_true
end

it 'returns false given a user with an answer' do
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)
hider = UnansweredQuestionHider.new

hider.hide_unanswered_question?(question, user).should be_false
result = hider.summary_or_hidden_answer(summarizer, question, user)

result.should eq summary
end

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

hider.hide_unanswered_question?(question, nil).should be_false
result = hider.summary_or_hidden_answer(summarizer, question, nil)

result.should eq summary
end

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

def stub_answered_question(user, answered)

0 comments on commit 4fd00a8

Please sign in to comment.