Permalink
Browse files

Replace Subclasses with Strategies: Step Eleven

* Move remaining strategy-specific state into strategies
  • Loading branch information...
jferris committed Feb 7, 2013
1 parent 41b49f4 commit 662e50874a377f8050ea2ad1326a7a4e47125f86
@@ -41,14 +41,12 @@ def type
end

def question_params
params.
require(:question).
permit(:title, :options_attributes)
params.require(:question).permit(:title)
end

def submittable_params
if submittable_attributes = params[:question][:submittable_attributes]
submittable_attributes.permit(:minimum, :maximum)
submittable_attributes.permit(:minimum, :maximum, :options_attributes)
else
{}
end
@@ -1,5 +1,2 @@
class MultipleChoiceQuestion < Question
has_many :options, foreign_key: :question_id

accepts_nested_attributes_for :options, reject_if: :all_blank
end
@@ -1,6 +1,9 @@
class MultipleChoiceSubmittable < ActiveRecord::Base
has_many :options, foreign_key: :question_id
has_one :question, as: :submittable

accepts_nested_attributes_for :options, reject_if: :all_blank

def breakdown
total = answers.count
counts = answers.group(:text).order('COUNT(*) DESC').count
@@ -28,8 +31,4 @@ def score(text)
def answers
question.answers
end

def options
question.options
end
end
@@ -1,5 +1,5 @@
<ol>
<% multiple_choice_question.options.each do |option| -%>
<% submittable.options.each do |option| -%>
<li>
<%= submission_fields.radio_button :text, option.text, id: dom_id(option) %>
<%= content_tag :label, option.text, for: dom_id(option) %>
@@ -1,3 +1,9 @@
<%= form.fields_for(:options, submittable.options_for_form) do |option_fields| -%>
<%= option_fields.input :text, label: 'Option' %>
<%= form.fields_for(:submittable) do |submittable_fields| -%>
<%= submittable_fields.fields_for(
:options,
submittable.options_for_form
) do |option_fields| -%>

<%= option_fields.input :text, label: 'Option' %>
<% end -%>
<% end -%>
@@ -0,0 +1,33 @@
class BackfillSubmittables < ActiveRecord::Migration
def up
backfill 'open'
backfill 'multiple_choice'
end

def down
connection.delete 'DELETE FROM open_submittables'
connection.delete 'DELETE FROM multiple_choice_submittables'
end

private

def backfill(type)
say_with_time "Backfilling #{type} submittables" do
connection.update(<<-SQL)
UPDATE questions
SET
submittable_id = id,
submittable_type = '#{type.camelize}Submittable'
WHERE type = '#{type.camelize}Question'
SQL
connection.insert(<<-SQL)
INSERT INTO #{type}_submittables
(id, created_at, updated_at)
SELECT
id, created_at, updated_at
FROM questions
WHERE questions.type = '#{type.camelize}Question'
SQL
end
end
end
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130131211856) do
ActiveRecord::Schema.define(:version => 20130207164259) do

create_table "answers", :force => true do |t|
t.integer "completion_id", :null => false
@@ -11,6 +11,15 @@
end

factory :multiple_choice_submittable do
ignore do
options_texts { [] }
end

options do |attributes|
attributes.options_texts.map do |text|
FactoryGirl.build(:option, text: text, question_id: attributes.id)
end
end
end

factory :open_submittable do
@@ -25,16 +34,6 @@
sequence(:title) { |n| "Question #{n}" }

factory :multiple_choice_question, class: 'MultipleChoiceQuestion' do
ignore do
options_texts { [] }
end

options do |attributes|
attributes.options_texts.map do |text|
FactoryGirl.build(:option, text: text, question_id: attributes.id)
end
end

submittable factory: :multiple_choice_submittable
end

@@ -14,7 +14,10 @@

def add_options_with_text(*texts)
texts.each_with_index do |text, index|
fill_in "question_options_attributes_#{index}_text", with: text
fill_in(
"question_submittable_attributes_options_attributes_#{index}_text",
with: text
)
end
end
end
@@ -4,8 +4,8 @@
scenario 'with valid answer' do
sign_in
view_survey_with_question :multiple_choice,
title: 'What is your favorite color?',
options_texts: %w(Red Blue)
{ title: 'What is your favorite color?' },
{ options_texts: %w(Red Blue) }
choose 'Red'
submit_answers
answer_to_question('What is your favorite color?').

This file was deleted.

Oops, something went wrong.
@@ -1,13 +1,18 @@
describe MultipleChoiceSubmittable do
it { should have_many(:options) }
it { should have_one(:question) }
end

describe MultipleChoiceSubmittable, '#breakdown' do
it 'returns a percentage breakdown' do
survey = create(:survey)
submittable = create(
:multiple_choice_submittable,
options_texts: %w(Blue Red)
)
question = create(
:multiple_choice_question,
options_texts: %w(Blue Red),
submittable: submittable,
survey: survey
)
submittable = MultipleChoiceSubmittable.new(question: question)
@@ -22,15 +27,13 @@

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: question)
submittable = MultipleChoiceSubmittable.new(options: [])
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: question)
submittable = MultipleChoiceSubmittable.new(options: options)
submittable.options_for_form.map(&:text).should match_array(['hey', 'hello'])
end
end
@@ -39,11 +42,11 @@
it 'returns the score for the option with the given text' do
question = build_stubbed(:multiple_choice_question)
submittable = MultipleChoiceSubmittable.new(question: question)
question.options.target.stubs(score: 2)
submittable.options.target.stubs(score: 2)

result = submittable.score('two')

question.options.target.should have_received(:score).with('two')
submittable.options.target.should have_received(:score).with('two')
result.should eq 2
end
end
@@ -18,11 +18,16 @@ def scored_multiple_choice_question(title, texts_and_scores)
build(:option, text: text, score: score)
end

submittable = create(
:multiple_choice_submittable,
options: options
)

create(
:multiple_choice_question,
title: title,
options: options,
survey: survey
submittable: submittable,
survey: survey,
title: title
)
end

0 comments on commit 662e508

Please sign in to comment.