Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 29, 2012
1 parent a53319f commit b535171
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
12 changes: 6 additions & 6 deletions example_app/app/models/question.rb
@@ -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
Expand All @@ -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
Expand All @@ -32,7 +32,7 @@ def steps
private

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

def summarize_multiple_choice_answers
Expand Down
6 changes: 3 additions & 3 deletions example_app/app/views/questions/_question.html.erb
Expand Up @@ -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>
Expand All @@ -13,7 +13,7 @@
</ol>
<% end -%>
<% if question.question_type == 'Scale' -%>
<% if question.question_type == 'ScaleQuestion' -%>
<ol>
<% question.steps.each do |step| -%>
<li>
Expand All @@ -24,7 +24,7 @@
</ol>
<% end -%>
<% if question.question_type == 'Open' -%>
<% if question.question_type == 'OpenQuestion' -%>
<%= submission_fields.text_field :text %>
<% end -%>
</li>
Expand Down
4 changes: 2 additions & 2 deletions example_app/app/views/questions/new.html.erb
Expand Up @@ -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 -%>
Expand Down
6 changes: 3 additions & 3 deletions example_app/app/views/surveys/show.html.erb
Expand Up @@ -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| -%>
Expand Down
@@ -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
2 changes: 1 addition & 1 deletion example_app/db/schema.rb
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions example_app/spec/factories/application.rb
Expand Up @@ -18,7 +18,7 @@
factory :question do
survey
title 'Question'
question_type 'Open'
question_type 'OpenQuestion'

factory :multiple_choice_question do
ignore do
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion example_app/spec/models/question_spec.rb
Expand Up @@ -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

Expand Down

0 comments on commit b535171

Please sign in to comment.