Permalink
Browse files

Extract Decorator Step Four

* Promote parameters to instance variables
  • Loading branch information...
jferris committed Feb 21, 2013
1 parent 4fd00a8 commit 72801b57dbe4a7dc20091e8734b1cb1abfd1cd16
@@ -9,11 +9,8 @@ class Survey < ActiveRecord::Base

def summaries_using(summarizer, options = {})
questions.map do |question|
UnansweredQuestionHider.new.summary_or_hidden_answer(
summarizer,
question,
options[:answered_by]
)
UnansweredQuestionHider.new(summarizer, options[:answered_by]).
summary_or_hidden_answer(question)
end
end
end
@@ -1,11 +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)
def initialize(summarizer, user)
@summarizer = summarizer
@user = user
end

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

@@ -15,7 +20,7 @@ def hide_answer_to_question(question)
Summary.new(question.title, NO_ANSWER)
end

def hide_unanswered_question?(question, user)
user && !question.answered_by?(user)
def hide_unanswered_question?(question)
@user && !question.answered_by?(@user)
end
end
@@ -5,9 +5,9 @@
summarizer = stub('summarizer')
user = build_stubbed(:user)
question = stub_answered_question(user, false)
hider = UnansweredQuestionHider.new
hider = UnansweredQuestionHider.new(summarizer, user)

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

result.title.should eq question.title
result.value.should eq UnansweredQuestionHider::NO_ANSWER
@@ -18,9 +18,9 @@
user = build_stubbed(:user)
question = stub_answered_question(user, true)
summarizer = stub_summarizer(question, summary)
hider = UnansweredQuestionHider.new
hider = UnansweredQuestionHider.new(summarizer, user)

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

result.should eq summary
end
@@ -29,9 +29,9 @@
summary = stub('summary')
question = build_stubbed(:question)
summarizer = stub_summarizer(question, summary)
hider = UnansweredQuestionHider.new
hider = UnansweredQuestionHider.new(summarizer, nil)

result = hider.summary_or_hidden_answer(summarizer, question, nil)
result = hider.summary_or_hidden_answer(question)

result.should eq summary
end

0 comments on commit 72801b5

Please sign in to comment.