Skip to content

Commit

Permalink
Extract Decorator Step Four
Browse files Browse the repository at this point in the history
* Promote parameters to instance variables
  • Loading branch information
jferris committed Feb 22, 2013
1 parent 4fd00a8 commit 72801b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
7 changes: 2 additions & 5 deletions example_app/app/models/survey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions example_app/app/models/unanswered_question_hider.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
12 changes: 6 additions & 6 deletions example_app/spec/models/unanswered_question_hider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 72801b5

Please sign in to comment.