Permalink
Browse files

Replace Subclasses with Strategies: Step Two

* Pull up delegate method into base class
  • Loading branch information...
jferris committed Jan 31, 2013
1 parent 7747366 commit 9c2ddc65e7248bab1f010d8a2c74c8f994a8b26d
@@ -11,10 +11,6 @@ def options_for_form
end
end

def score(text)
submittable.score(text)
end

def breakdown
total = answers.count
counts = answers.group(:text).order('COUNT(*) DESC').count
@@ -1,8 +1,4 @@
class OpenQuestion < Question
def score(text)
submittable.score(text)
end

def breakdown
text_from_ordered_answers = answers.order(:created_at).pluck(:text)
text_from_ordered_answers.join(', ')
@@ -9,6 +9,7 @@ class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers

delegate :score, to: :submittable
delegate :title, to: :survey, prefix: true

def most_recent_answer_text
@@ -2,10 +2,6 @@ class ScaleQuestion < Question
validates :maximum, presence: true
validates :minimum, presence: true

def score(text)
submittable.score(text)
end

def steps
(minimum..maximum).to_a
end
@@ -15,18 +15,6 @@
end
end

describe MultipleChoiceQuestion, '#score' do
it 'returns the score for the option with the given text' do
question = create(:multiple_choice_question)
question.options.target.stubs(score: 2)

result = question.score('two')

question.options.target.should have_received(:score).with('two')
result.should eq 2
end
end

describe MultipleChoiceQuestion, '#breakdown' do
it 'returns a percentage breakdown' do
survey = create(:survey)
@@ -1,13 +1,3 @@
describe OpenQuestion, '#score' do
it 'returns zero' do
question = build_stubbed(:open_question)

result = question.score('anything')

result.should eq 0
end
end

describe OpenQuestion, '#breakdown' do
it 'returns all answers' do
survey = create(:survey)
@@ -5,16 +5,6 @@
it { should validate_presence_of(:minimum) }
end

describe ScaleQuestion, '#score' do
it 'returns the integer value of the text' do
question = build_stubbed(:scale_question)

result = question.score('5')

result.should eq 5
end
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)

0 comments on commit 9c2ddc6

Please sign in to comment.