Permalink
Browse files

Extract Decorator Step Eight

* Invert control
  • Loading branch information...
jferris committed Feb 21, 2013
1 parent 77b22c5 commit 256a9c92e87ab40ad24789e4314d115d74ed78d8
@@ -1,11 +1,19 @@
class SummariesController < ApplicationController
def show
@survey = Survey.find(params[:survey_id])
@summaries = @survey.summaries_using(summarizer, constraints)
@summaries = @survey.summaries_using(decorated_summarizer)
end

private

def decorated_summarizer
if include_unanswered?
summarizer
else
UnansweredQuestionHider.new(summarizer, current_user)
end
end

def summarizer
summarizer_class.new(user: current_user)
end
@@ -14,14 +22,6 @@ def summarizer_class
"Summarizer::#{params[:id].classify}".constantize
end

def constraints
if include_unanswered?
{}
else
{ answered_by: current_user }
end
end

def include_unanswered?
params[:unanswered]
end
@@ -7,10 +7,9 @@ class Survey < ActiveRecord::Base
has_many :completions
has_many :questions

def summaries_using(summarizer, options = {})
def summaries_using(summarizer)
questions.map do |question|
hider = UnansweredQuestionHider.new(summarizer, options[:answered_by])
question.summary_using(hider)
question.summary_using(summarizer)
end
end
end
@@ -17,6 +17,6 @@ def summarize(question)
private

def hide_unanswered_question?(question)
@user && !question.answered_by?(@user)
!question.answered_by?(@user)
end
end
@@ -20,32 +20,6 @@
result.map(&:value).should == %w(result result)
end

it 'applies the given summarizer to each question answered by the user' do
user = build_stubbed(:user)
answered_question = stub_question(user: user, answered: true)
unanswered_question = stub_question(user: user, answered: false)
questions = [answered_question, unanswered_question]
questions.stubs(:answered_by).with(user).returns(questions)
survey = build_stubbed(:survey, questions: questions)
summarizer = stub_summarizer

result = survey.summaries_using(summarizer, answered_by: user)

should_summarize_questions [answered_question], summarizer
result.map(&:title).should eq questions.map(&:title)
result.map(&:value).
should eq ['result', "You haven't answered this question"]
end

def stub_question(arguments)
build_stubbed(:question).tap do |question|
question.
stubs(:answered_by?).
with(arguments[:user]).
returns(arguments[:answered])
end
end

def stub_summarizer
stub('summarizer', summarize: 'result')
end
@@ -23,16 +23,6 @@
result.should eq 'value'
end

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

result = hider.summarize(question)

result.should eq 'value'
end

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

0 comments on commit 256a9c9

Please sign in to comment.