Permalink
Browse files

Add Question suffix to question_type

* Makes question type more readable as a class name
* First step towards subclasses
  • Loading branch information...
jferris committed Nov 28, 2012
1 parent a53319f commit b53517146d6acdc393a90d8022d8869a170bb2d4
@@ -1,11 +1,11 @@
class Question < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection

SUBMITTABLE_TYPES = %w(Open MultipleChoice Scale).freeze
QUESTION_TYPES = %w(OpenQuestion MultipleChoiceQuestion ScaleQuestion).freeze

validates :maximum, presence: true, if: :scale?
validates :minimum, presence: true, if: :scale?
validates :question_type, presence: true, inclusion: SUBMITTABLE_TYPES
validates :question_type, presence: true, inclusion: QUESTION_TYPES
validates :title, presence: true

belongs_to :survey
@@ -16,11 +16,11 @@ class Question < ActiveRecord::Base

def summary
case question_type
when 'MultipleChoice'
when 'MultipleChoiceQuestion'
summarize_multiple_choice_answers
when 'Open'
when 'OpenQuestion'
summarize_open_answers
when 'Scale'
when 'ScaleQuestion'
summarize_scale_answers
end
end
@@ -32,7 +32,7 @@ def steps
private

def scale?
question_type == 'Scale'
question_type == 'ScaleQuestion'
end

def summarize_multiple_choice_answers
@@ -2,7 +2,7 @@
<li>
<%= submission_fields.label :text, label: question.title %>

<% if question.question_type == 'MultipleChoice' -%>
<% if question.question_type == 'MultipleChoiceQuestion' -%>
<ol>
<% question.options.each do |option| -%>
<li>
@@ -13,7 +13,7 @@
</ol>
<% end -%>

<% if question.question_type == 'Scale' -%>
<% if question.question_type == 'ScaleQuestion' -%>
<ol>
<% question.steps.each do |step| -%>
<li>
@@ -24,7 +24,7 @@
</ol>
<% end -%>

<% if question.question_type == 'Open' -%>
<% if question.question_type == 'OpenQuestion' -%>
<%= submission_fields.text_field :text %>
<% end -%>
</li>
@@ -3,13 +3,13 @@

<%= form.input :title %>

<% if @question.question_type == 'MultipleChoice' -%>
<% if @question.question_type == 'MultipleChoiceQuestion' -%>
<%= form.fields_for(:options) do |option_fields| -%>
<%= option_fields.input :text, label: 'Option' %>
<% end -%>
<% end -%>

<% if @question.question_type == 'Scale' -%>
<% if @question.question_type == 'ScaleQuestion' -%>
<%= form.input :minimum %>
<%= form.input :maximum %>
<% end -%>
@@ -3,17 +3,17 @@

<%= link_to(
'Add Multiple Choice Question',
new_survey_question_path(@survey, question_type: 'MultipleChoice')
new_survey_question_path(@survey, question_type: 'MultipleChoiceQuestion')
) %>

<%= link_to(
'Add Open Question',
new_survey_question_path(@survey, question_type: 'Open')
new_survey_question_path(@survey, question_type: 'OpenQuestion')
) %>

<%= link_to(
'Add Scale Question',
new_survey_question_path(@survey, question_type: 'Scale')
new_survey_question_path(@survey, question_type: 'ScaleQuestion')
) %>

<%= simple_form_for [@survey, @completion] do |form| -%>
@@ -0,0 +1,13 @@
class AddQuestionSuffixToQuestionType < ActiveRecord::Migration
def up
connection.update(<<-SQL)
UPDATE questions SET question_type = question_type || 'Question'
SQL
end

def down
connection.update(<<-SQL)
UPDATE questions SET question_type = REPLACE(question_type, 'Question', '')
SQL
end
end
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121128194213) do
ActiveRecord::Schema.define(:version => 20121128221331) do

create_table "answers", :force => true do |t|
t.integer "completion_id", :null => false
@@ -18,7 +18,7 @@
factory :question do
survey
title 'Question'
question_type 'Open'
question_type 'OpenQuestion'

factory :multiple_choice_question do
ignore do
@@ -31,15 +31,15 @@
end
end

question_type 'MultipleChoice'
question_type 'MultipleChoiceQuestion'
end

factory :open_question do
question_type 'Open'
question_type 'OpenQuestion'
end

factory :scale_question do
question_type 'Scale'
question_type 'ScaleQuestion'
end
end

@@ -3,7 +3,7 @@
describe Question do
it { should validate_presence_of :question_type }

Question::SUBMITTABLE_TYPES.each do |type|
Question::QUESTION_TYPES.each do |type|
it { should allow_value(type).for(:question_type) }
end

0 comments on commit b535171

Please sign in to comment.