Permalink
Browse files

Replace Subclasses with Strategies: Step Three

* Move remaining common API into strategies
  • Loading branch information...
jferris committed Jan 31, 2013
1 parent 9c2ddc6 commit db3658cd1c4601c07f49a7c666f57c00f5c22ffd
@@ -11,16 +11,6 @@ def options_for_form
end
end

def breakdown
total = answers.count
counts = answers.group(:text).order('COUNT(*) DESC').count
percents = counts.map do |text, count|
percent = (100.0 * count / total).round
"#{percent}% #{text}"
end
percents.join(', ')
end

def submittable
MultipleChoiceSubmittable.new(self)
end
@@ -3,12 +3,26 @@ def initialize(question)
@question = question
end

def breakdown
total = answers.count
counts = answers.group(:text).order('COUNT(*) DESC').count
percents = counts.map do |text, count|
percent = (100.0 * count / total).round
"#{percent}% #{text}"
end
percents.join(', ')
end

def score(text)
options.score(text)
end

private

def answers
@question.answers
end

def options
@question.options
end
@@ -1,10 +1,5 @@
class OpenQuestion < Question
def breakdown
text_from_ordered_answers = answers.order(:created_at).pluck(:text)
text_from_ordered_answers.join(', ')
end

def submittable
OpenSubmittable.new
OpenSubmittable.new(self)
end
end
@@ -1,5 +1,20 @@
class OpenSubmittable
def initialize(question)
@question = question
end

def breakdown
text_from_ordered_answers = answers.order(:created_at).pluck(:text)
text_from_ordered_answers.join(', ')
end

def score(text)
0
end

private

def answers
@question.answers
end
end
@@ -9,7 +9,7 @@ class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers

delegate :score, to: :submittable
delegate :breakdown, :score, to: :submittable
delegate :title, to: :survey, prefix: true

def most_recent_answer_text
@@ -6,11 +6,7 @@ def steps
(minimum..maximum).to_a
end

def breakdown
sprintf('Average: %.02f', answers.average('text'))
end

def submittable
ScaleSubmittable.new
ScaleSubmittable.new(self)
end
end
@@ -1,5 +1,19 @@
class ScaleSubmittable
def initialize(question)
@question = question
end

def breakdown
sprintf('Average: %.02f', answers.average('text'))
end

def score(text)
text.to_i
end

private

def answers
@question.answers
end
end
@@ -14,20 +14,3 @@
question.options_for_form.map(&:text).should match_array(['hey', 'hello'])
end
end

describe MultipleChoiceQuestion, '#breakdown' do
it 'returns a percentage breakdown' do
survey = create(:survey)
question = create(
:multiple_choice_question,
options_texts: %w(Blue Red),
survey: survey
)
taker = AnswerCreator.new(survey)
taker.answer question, 'Red'
taker.answer question, 'Blue'
taker.answer question, 'Red'

question.breakdown.should eq '67% Red, 33% Blue'
end
end
@@ -1,3 +1,21 @@
describe MultipleChoiceSubmittable, '#breakdown' do
it 'returns a percentage breakdown' do
survey = create(:survey)
question = create(
:multiple_choice_question,
options_texts: %w(Blue Red),
survey: survey
)
submittable = MultipleChoiceSubmittable.new(question)
taker = AnswerCreator.new(survey)
taker.answer question, 'Red'
taker.answer question, 'Blue'
taker.answer question, 'Red'

submittable.breakdown.should eq '67% Red, 33% Blue'
end
end

describe MultipleChoiceSubmittable, '#score' do
it 'returns the score for the option with the given text' do
question = build_stubbed(:multiple_choice_question)
@@ -1,13 +0,0 @@
describe OpenQuestion, '#breakdown' do
it 'returns all answers' do
survey = create(:survey)
question = create(:open_question, survey: survey)
taker = AnswerCreator.new(survey)

taker.answer question, 'Hey'
taker.answer question, 'Hi'
taker.answer question, 'Hello'

question.breakdown.should eq 'Hey, Hi, Hello'
end
end
@@ -1,6 +1,22 @@
describe OpenSubmittable, '#breakdown' do
it 'returns all answers' do
survey = create(:survey)
question = create(:open_question, survey: survey)
submittable = OpenSubmittable.new(question)
taker = AnswerCreator.new(survey)

taker.answer question, 'Hey'
taker.answer question, 'Hi'
taker.answer question, 'Hello'

submittable.breakdown.should eq 'Hey, Hi, Hello'
end
end

describe OpenSubmittable, '#score' do
it 'returns zero' do
submittable = OpenSubmittable.new
question = build_stubbed(:open_question)
submittable = OpenSubmittable.new(question)

result = submittable.score('anything')

@@ -11,16 +11,3 @@
question.steps.should eq [2, 3, 4, 5]
end
end

describe ScaleQuestion, '#breakdown' do
it 'returns the average' do
survey = create(:survey)
question = create(:scale_question, minimum: 0, maximum: 10, survey: survey)
taker = AnswerCreator.new(survey)
taker.answer question, 6
taker.answer question, 6
taker.answer question, 8

question.breakdown.should eq 'Average: 6.67'
end
end
@@ -1,6 +1,21 @@
describe ScaleSubmittable, '#breakdown' do
it 'returns the average' do
survey = create(:survey)
question = create(:scale_question, minimum: 0, maximum: 10, survey: survey)
submittable = ScaleSubmittable.new(question)
taker = AnswerCreator.new(survey)
taker.answer question, 6
taker.answer question, 6
taker.answer question, 8

submittable.breakdown.should eq 'Average: 6.67'
end
end

describe ScaleSubmittable, '#score' do
it 'returns the integer value of the text' do
submittable = ScaleSubmittable.new
question = build_stubbed(:scale_question)
submittable = ScaleSubmittable.new(question)

result = submittable.score('5')

0 comments on commit db3658c

Please sign in to comment.