Permalink
Browse files

Extract Decorator Step Two

* Move conditional method into decorator
  • Loading branch information...
jferris committed Feb 21, 2013
1 parent af2e831 commit 9d0274f408232b082520e9901f404e4f1ba9c050
@@ -16,14 +16,11 @@ def summaries_using(summarizer, options = {})
private

def summary_or_hidden_answer(summarizer, question, answered_by)
if hide_unanswered_question?(question, answered_by)
UnansweredQuestionHider.new.hide_answer_to_question(question)
hider = UnansweredQuestionHider.new
if hider.hide_unanswered_question?(question, answered_by)
hider.hide_answer_to_question(question)
else
question.summary_using(summarizer)
end
end

def hide_unanswered_question?(question, answered_by)
answered_by && !question.answered_by?(answered_by)
end
end
@@ -4,4 +4,8 @@ class UnansweredQuestionHider
def hide_answer_to_question(question)
Summary.new(question.title, NO_ANSWER)
end

def hide_unanswered_question?(question, user)
user && !question.answered_by?(user)
end
end
@@ -11,3 +11,34 @@
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
user = build_stubbed(:user)
question = stub_answered_question(user, true)
hider = UnansweredQuestionHider.new

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

it 'returns false without a user' do
question = build_stubbed(:question)
hider = UnansweredQuestionHider.new

hider.hide_unanswered_question?(question, nil).should be_false
end

def stub_answered_question(user, answered)
build_stubbed(:question).tap do |question|
question.stubs(:answered_by?).with(user).returns(answered)
end
end
end

0 comments on commit 9d0274f

Please sign in to comment.