Permalink
Browse files

Replace Subclasses with Strategies: Step Four

* Move subclass-specific behavior into strategies
  • Loading branch information...
jferris committed Jan 31, 2013
1 parent db3658c commit 2bce7f7b0812b417dc41af369d18b83e057419ac
@@ -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
@@ -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
@@ -3,7 +3,7 @@ class ScaleQuestion < Question
validates :minimum, presence: true

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

def submittable
@@ -11,6 +11,10 @@ def score(text)
text.to_i
end

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

private

def answers
@@ -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
@@ -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)
@@ -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
@@ -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.