Skip to content

Commit 2bce7f7

Browse files
committed
Replace Subclasses with Strategies: Step Four
* Move subclass-specific behavior into strategies
1 parent db3658c commit 2bce7f7

File tree

8 files changed

+37
-26
lines changed

8 files changed

+37
-26
lines changed

example_app/app/models/multiple_choice_question.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ class MultipleChoiceQuestion < Question
44
accepts_nested_attributes_for :options, reject_if: :all_blank
55

66
def options_for_form
7-
if options.any?
8-
options
9-
else
10-
[Option.new, Option.new, Option.new]
11-
end
7+
submittable.options_for_form
128
end
139

1410
def submittable

example_app/app/models/multiple_choice_submittable.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def breakdown
1313
percents.join(', ')
1414
end
1515

16+
def options_for_form
17+
if options.any?
18+
options
19+
else
20+
[Option.new, Option.new, Option.new]
21+
end
22+
end
23+
1624
def score(text)
1725
options.score(text)
1826
end

example_app/app/models/scale_question.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ScaleQuestion < Question
33
validates :minimum, presence: true
44

55
def steps
6-
(minimum..maximum).to_a
6+
submittable.steps
77
end
88

99
def submittable

example_app/app/models/scale_submittable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def score(text)
1111
text.to_i
1212
end
1313

14+
def steps
15+
(@question.minimum..@question.maximum).to_a
16+
end
17+
1418
private
1519

1620
def answers
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
11
describe MultipleChoiceQuestion do
22
it { should have_many(:options) }
33
end
4-
5-
describe MultipleChoiceQuestion, '#options_for_form' do
6-
it 'adds empty options when none are present' do
7-
question = build_stubbed(:multiple_choice_question, options: [])
8-
question.options_for_form.count.should == 3
9-
end
10-
11-
it 'leaves existing options alone' do
12-
options = [Option.new(text: 'hey'), Option.new(text: 'hello')]
13-
question = build_stubbed(:multiple_choice_question, options: options)
14-
question.options_for_form.map(&:text).should match_array(['hey', 'hello'])
15-
end
16-
end

example_app/spec/models/multiple_choice_submittable_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616
end
1717
end
1818

19+
describe MultipleChoiceSubmittable, '#options_for_form' do
20+
it 'adds empty options when none are present' do
21+
question = build_stubbed(:multiple_choice_question, options: [])
22+
submittable = MultipleChoiceSubmittable.new(question)
23+
submittable.options_for_form.count.should == 3
24+
end
25+
26+
it 'leaves existing options alone' do
27+
options = [Option.new(text: 'hey'), Option.new(text: 'hello')]
28+
question = build_stubbed(:multiple_choice_question, options: options)
29+
submittable = MultipleChoiceSubmittable.new(question)
30+
submittable.options_for_form.map(&:text).should match_array(['hey', 'hello'])
31+
end
32+
end
33+
1934
describe MultipleChoiceSubmittable, '#score' do
2035
it 'returns the score for the option with the given text' do
2136
question = build_stubbed(:multiple_choice_question)

example_app/spec/models/scale_question_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,3 @@
44
it { should validate_presence_of(:maximum) }
55
it { should validate_presence_of(:minimum) }
66
end
7-
8-
describe ScaleQuestion, '#steps' do
9-
it 'returns all numbers starting at the minimum and ending at the maximum' do
10-
question = build_stubbed(:scale_question, minimum: 2, maximum: 5)
11-
question.steps.should eq [2, 3, 4, 5]
12-
end
13-
end

example_app/spec/models/scale_submittable_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@
2222
result.should eq 5
2323
end
2424
end
25+
26+
describe ScaleSubmittable, '#steps' do
27+
it 'returns all numbers starting at the minimum and ending at the maximum' do
28+
question = build_stubbed(:scale_question, minimum: 2, maximum: 5)
29+
submittable = ScaleSubmittable.new(question)
30+
submittable.steps.should eq [2, 3, 4, 5]
31+
end
32+
end

0 commit comments

Comments
 (0)