Skip to content

Commit db3658c

Browse files
committed
Replace Subclasses with Strategies: Step Three
* Move remaining common API into strategies
1 parent 9c2ddc6 commit db3658c

13 files changed

+97
-67
lines changed

example_app/app/models/multiple_choice_question.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ def options_for_form
1111
end
1212
end
1313

14-
def breakdown
15-
total = answers.count
16-
counts = answers.group(:text).order('COUNT(*) DESC').count
17-
percents = counts.map do |text, count|
18-
percent = (100.0 * count / total).round
19-
"#{percent}% #{text}"
20-
end
21-
percents.join(', ')
22-
end
23-
2414
def submittable
2515
MultipleChoiceSubmittable.new(self)
2616
end

example_app/app/models/multiple_choice_submittable.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@ def initialize(question)
33
@question = question
44
end
55

6+
def breakdown
7+
total = answers.count
8+
counts = answers.group(:text).order('COUNT(*) DESC').count
9+
percents = counts.map do |text, count|
10+
percent = (100.0 * count / total).round
11+
"#{percent}% #{text}"
12+
end
13+
percents.join(', ')
14+
end
15+
616
def score(text)
717
options.score(text)
818
end
919

1020
private
1121

22+
def answers
23+
@question.answers
24+
end
25+
1226
def options
1327
@question.options
1428
end
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
class OpenQuestion < Question
2-
def breakdown
3-
text_from_ordered_answers = answers.order(:created_at).pluck(:text)
4-
text_from_ordered_answers.join(', ')
5-
end
6-
72
def submittable
8-
OpenSubmittable.new
3+
OpenSubmittable.new(self)
94
end
105
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
class OpenSubmittable
2+
def initialize(question)
3+
@question = question
4+
end
5+
6+
def breakdown
7+
text_from_ordered_answers = answers.order(:created_at).pluck(:text)
8+
text_from_ordered_answers.join(', ')
9+
end
10+
211
def score(text)
312
0
413
end
14+
15+
private
16+
17+
def answers
18+
@question.answers
19+
end
520
end

example_app/app/models/question.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Question < ActiveRecord::Base
99
belongs_to :survey
1010
has_many :answers
1111

12-
delegate :score, to: :submittable
12+
delegate :breakdown, :score, to: :submittable
1313
delegate :title, to: :survey, prefix: true
1414

1515
def most_recent_answer_text

example_app/app/models/scale_question.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ def steps
66
(minimum..maximum).to_a
77
end
88

9-
def breakdown
10-
sprintf('Average: %.02f', answers.average('text'))
11-
end
12-
139
def submittable
14-
ScaleSubmittable.new
10+
ScaleSubmittable.new(self)
1511
end
1612
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
class ScaleSubmittable
2+
def initialize(question)
3+
@question = question
4+
end
5+
6+
def breakdown
7+
sprintf('Average: %.02f', answers.average('text'))
8+
end
9+
210
def score(text)
311
text.to_i
412
end
13+
14+
private
15+
16+
def answers
17+
@question.answers
18+
end
519
end

example_app/spec/models/multiple_choice_question_spec.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,3 @@
1414
question.options_for_form.map(&:text).should match_array(['hey', 'hello'])
1515
end
1616
end
17-
18-
describe MultipleChoiceQuestion, '#breakdown' do
19-
it 'returns a percentage breakdown' do
20-
survey = create(:survey)
21-
question = create(
22-
:multiple_choice_question,
23-
options_texts: %w(Blue Red),
24-
survey: survey
25-
)
26-
taker = AnswerCreator.new(survey)
27-
taker.answer question, 'Red'
28-
taker.answer question, 'Blue'
29-
taker.answer question, 'Red'
30-
31-
question.breakdown.should eq '67% Red, 33% Blue'
32-
end
33-
end

example_app/spec/models/multiple_choice_submittable_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
describe MultipleChoiceSubmittable, '#breakdown' do
2+
it 'returns a percentage breakdown' do
3+
survey = create(:survey)
4+
question = create(
5+
:multiple_choice_question,
6+
options_texts: %w(Blue Red),
7+
survey: survey
8+
)
9+
submittable = MultipleChoiceSubmittable.new(question)
10+
taker = AnswerCreator.new(survey)
11+
taker.answer question, 'Red'
12+
taker.answer question, 'Blue'
13+
taker.answer question, 'Red'
14+
15+
submittable.breakdown.should eq '67% Red, 33% Blue'
16+
end
17+
end
18+
119
describe MultipleChoiceSubmittable, '#score' do
220
it 'returns the score for the option with the given text' do
321
question = build_stubbed(:multiple_choice_question)
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +0,0 @@
1-
describe OpenQuestion, '#breakdown' do
2-
it 'returns all answers' do
3-
survey = create(:survey)
4-
question = create(:open_question, survey: survey)
5-
taker = AnswerCreator.new(survey)
6-
7-
taker.answer question, 'Hey'
8-
taker.answer question, 'Hi'
9-
taker.answer question, 'Hello'
10-
11-
question.breakdown.should eq 'Hey, Hi, Hello'
12-
end
13-
end

example_app/spec/models/open_submittable_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
describe OpenSubmittable, '#breakdown' do
2+
it 'returns all answers' do
3+
survey = create(:survey)
4+
question = create(:open_question, survey: survey)
5+
submittable = OpenSubmittable.new(question)
6+
taker = AnswerCreator.new(survey)
7+
8+
taker.answer question, 'Hey'
9+
taker.answer question, 'Hi'
10+
taker.answer question, 'Hello'
11+
12+
submittable.breakdown.should eq 'Hey, Hi, Hello'
13+
end
14+
end
15+
116
describe OpenSubmittable, '#score' do
217
it 'returns zero' do
3-
submittable = OpenSubmittable.new
18+
question = build_stubbed(:open_question)
19+
submittable = OpenSubmittable.new(question)
420

521
result = submittable.score('anything')
622

example_app/spec/models/scale_question_spec.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,3 @@
1111
question.steps.should eq [2, 3, 4, 5]
1212
end
1313
end
14-
15-
describe ScaleQuestion, '#breakdown' do
16-
it 'returns the average' do
17-
survey = create(:survey)
18-
question = create(:scale_question, minimum: 0, maximum: 10, survey: survey)
19-
taker = AnswerCreator.new(survey)
20-
taker.answer question, 6
21-
taker.answer question, 6
22-
taker.answer question, 8
23-
24-
question.breakdown.should eq 'Average: 6.67'
25-
end
26-
end

example_app/spec/models/scale_submittable_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
describe ScaleSubmittable, '#breakdown' do
2+
it 'returns the average' do
3+
survey = create(:survey)
4+
question = create(:scale_question, minimum: 0, maximum: 10, survey: survey)
5+
submittable = ScaleSubmittable.new(question)
6+
taker = AnswerCreator.new(survey)
7+
taker.answer question, 6
8+
taker.answer question, 6
9+
taker.answer question, 8
10+
11+
submittable.breakdown.should eq 'Average: 6.67'
12+
end
13+
end
14+
115
describe ScaleSubmittable, '#score' do
216
it 'returns the integer value of the text' do
3-
submittable = ScaleSubmittable.new
17+
question = build_stubbed(:scale_question)
18+
submittable = ScaleSubmittable.new(question)
419

520
result = submittable.score('5')
621

0 commit comments

Comments
 (0)