Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace Subclasses with Strategies: Step Three
* Move remaining common API into strategies
  • Loading branch information
jferris committed Feb 5, 2013
1 parent 9c2ddc6 commit db3658c
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 67 deletions.
10 changes: 0 additions & 10 deletions example_app/app/models/multiple_choice_question.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions example_app/app/models/multiple_choice_submittable.rb
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions example_app/app/models/open_question.rb
@@ -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
15 changes: 15 additions & 0 deletions example_app/app/models/open_submittable.rb
@@ -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
2 changes: 1 addition & 1 deletion example_app/app/models/question.rb
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions example_app/app/models/scale_question.rb
Expand Up @@ -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
14 changes: 14 additions & 0 deletions example_app/app/models/scale_submittable.rb
@@ -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
17 changes: 0 additions & 17 deletions example_app/spec/models/multiple_choice_question_spec.rb
Expand Up @@ -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
18 changes: 18 additions & 0 deletions example_app/spec/models/multiple_choice_submittable_spec.rb
@@ -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)
Expand Down
13 changes: 0 additions & 13 deletions example_app/spec/models/open_question_spec.rb
@@ -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
18 changes: 17 additions & 1 deletion example_app/spec/models/open_submittable_spec.rb
@@ -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')

Expand Down
13 changes: 0 additions & 13 deletions example_app/spec/models/scale_question_spec.rb
Expand Up @@ -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
17 changes: 16 additions & 1 deletion example_app/spec/models/scale_submittable_spec.rb
@@ -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')

Expand Down

0 comments on commit db3658c

Please sign in to comment.