Skip to content

Commit

Permalink
Replace Subclasses with Strategies: Step Four
Browse files Browse the repository at this point in the history
* Move subclass-specific behavior into strategies
  • Loading branch information
jferris committed Feb 5, 2013
1 parent db3658c commit 2bce7f7
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
6 changes: 1 addition & 5 deletions example_app/app/models/multiple_choice_question.rb
Expand Up @@ -4,11 +4,7 @@ class MultipleChoiceQuestion < Question
accepts_nested_attributes_for :options, reject_if: :all_blank

def options_for_form
if options.any?
options
else
[Option.new, Option.new, Option.new]
end
submittable.options_for_form
end

def submittable
Expand Down
8 changes: 8 additions & 0 deletions example_app/app/models/multiple_choice_submittable.rb
Expand Up @@ -13,6 +13,14 @@ def breakdown
percents.join(', ')
end

def options_for_form
if options.any?
options
else
[Option.new, Option.new, Option.new]
end
end

def score(text)
options.score(text)
end
Expand Down
2 changes: 1 addition & 1 deletion example_app/app/models/scale_question.rb
Expand Up @@ -3,7 +3,7 @@ class ScaleQuestion < Question
validates :minimum, presence: true

def steps
(minimum..maximum).to_a
submittable.steps
end

def submittable
Expand Down
4 changes: 4 additions & 0 deletions example_app/app/models/scale_submittable.rb
Expand Up @@ -11,6 +11,10 @@ def score(text)
text.to_i
end

def steps
(@question.minimum..@question.maximum).to_a
end

private

def answers
Expand Down
13 changes: 0 additions & 13 deletions example_app/spec/models/multiple_choice_question_spec.rb
@@ -1,16 +1,3 @@
describe MultipleChoiceQuestion do
it { should have_many(:options) }
end

describe MultipleChoiceQuestion, '#options_for_form' do
it 'adds empty options when none are present' do
question = build_stubbed(:multiple_choice_question, options: [])
question.options_for_form.count.should == 3
end

it 'leaves existing options alone' do
options = [Option.new(text: 'hey'), Option.new(text: 'hello')]
question = build_stubbed(:multiple_choice_question, options: options)
question.options_for_form.map(&:text).should match_array(['hey', 'hello'])
end
end
15 changes: 15 additions & 0 deletions example_app/spec/models/multiple_choice_submittable_spec.rb
Expand Up @@ -16,6 +16,21 @@
end
end

describe MultipleChoiceSubmittable, '#options_for_form' do
it 'adds empty options when none are present' do
question = build_stubbed(:multiple_choice_question, options: [])
submittable = MultipleChoiceSubmittable.new(question)
submittable.options_for_form.count.should == 3
end

it 'leaves existing options alone' do
options = [Option.new(text: 'hey'), Option.new(text: 'hello')]
question = build_stubbed(:multiple_choice_question, options: options)
submittable = MultipleChoiceSubmittable.new(question)
submittable.options_for_form.map(&:text).should match_array(['hey', 'hello'])
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
7 changes: 0 additions & 7 deletions example_app/spec/models/scale_question_spec.rb
Expand Up @@ -4,10 +4,3 @@
it { should validate_presence_of(:maximum) }
it { should validate_presence_of(:minimum) }
end

describe ScaleQuestion, '#steps' do
it 'returns all numbers starting at the minimum and ending at the maximum' do
question = build_stubbed(:scale_question, minimum: 2, maximum: 5)
question.steps.should eq [2, 3, 4, 5]
end
end
8 changes: 8 additions & 0 deletions example_app/spec/models/scale_submittable_spec.rb
Expand Up @@ -22,3 +22,11 @@
result.should eq 5
end
end

describe ScaleSubmittable, '#steps' do
it 'returns all numbers starting at the minimum and ending at the maximum' do
question = build_stubbed(:scale_question, minimum: 2, maximum: 5)
submittable = ScaleSubmittable.new(question)
submittable.steps.should eq [2, 3, 4, 5]
end
end

0 comments on commit 2bce7f7

Please sign in to comment.