Skip to content

Commit b535171

Browse files
committed
Add Question suffix to question_type
* Makes question type more readable as a class name * First step towards subclasses
1 parent a53319f commit b535171

File tree

8 files changed

+33
-20
lines changed

8 files changed

+33
-20
lines changed

example_app/app/models/question.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Question < ActiveRecord::Base
22
include ActiveModel::ForbiddenAttributesProtection
33

4-
SUBMITTABLE_TYPES = %w(Open MultipleChoice Scale).freeze
4+
QUESTION_TYPES = %w(OpenQuestion MultipleChoiceQuestion ScaleQuestion).freeze
55

66
validates :maximum, presence: true, if: :scale?
77
validates :minimum, presence: true, if: :scale?
8-
validates :question_type, presence: true, inclusion: SUBMITTABLE_TYPES
8+
validates :question_type, presence: true, inclusion: QUESTION_TYPES
99
validates :title, presence: true
1010

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

1717
def summary
1818
case question_type
19-
when 'MultipleChoice'
19+
when 'MultipleChoiceQuestion'
2020
summarize_multiple_choice_answers
21-
when 'Open'
21+
when 'OpenQuestion'
2222
summarize_open_answers
23-
when 'Scale'
23+
when 'ScaleQuestion'
2424
summarize_scale_answers
2525
end
2626
end
@@ -32,7 +32,7 @@ def steps
3232
private
3333

3434
def scale?
35-
question_type == 'Scale'
35+
question_type == 'ScaleQuestion'
3636
end
3737

3838
def summarize_multiple_choice_answers

example_app/app/views/questions/_question.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<li>
33
<%= submission_fields.label :text, label: question.title %>
44

5-
<% if question.question_type == 'MultipleChoice' -%>
5+
<% if question.question_type == 'MultipleChoiceQuestion' -%>
66
<ol>
77
<% question.options.each do |option| -%>
88
<li>
@@ -13,7 +13,7 @@
1313
</ol>
1414
<% end -%>
1515

16-
<% if question.question_type == 'Scale' -%>
16+
<% if question.question_type == 'ScaleQuestion' -%>
1717
<ol>
1818
<% question.steps.each do |step| -%>
1919
<li>
@@ -24,7 +24,7 @@
2424
</ol>
2525
<% end -%>
2626

27-
<% if question.question_type == 'Open' -%>
27+
<% if question.question_type == 'OpenQuestion' -%>
2828
<%= submission_fields.text_field :text %>
2929
<% end -%>
3030
</li>

example_app/app/views/questions/new.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
<%= form.input :title %>
55

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

12-
<% if @question.question_type == 'Scale' -%>
12+
<% if @question.question_type == 'ScaleQuestion' -%>
1313
<%= form.input :minimum %>
1414
<%= form.input :maximum %>
1515
<% end -%>

example_app/app/views/surveys/show.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44
<%= link_to(
55
'Add Multiple Choice Question',
6-
new_survey_question_path(@survey, question_type: 'MultipleChoice')
6+
new_survey_question_path(@survey, question_type: 'MultipleChoiceQuestion')
77
) %>
88

99
<%= link_to(
1010
'Add Open Question',
11-
new_survey_question_path(@survey, question_type: 'Open')
11+
new_survey_question_path(@survey, question_type: 'OpenQuestion')
1212
) %>
1313

1414
<%= link_to(
1515
'Add Scale Question',
16-
new_survey_question_path(@survey, question_type: 'Scale')
16+
new_survey_question_path(@survey, question_type: 'ScaleQuestion')
1717
) %>
1818

1919
<%= simple_form_for [@survey, @completion] do |form| -%>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class AddQuestionSuffixToQuestionType < ActiveRecord::Migration
2+
def up
3+
connection.update(<<-SQL)
4+
UPDATE questions SET question_type = question_type || 'Question'
5+
SQL
6+
end
7+
8+
def down
9+
connection.update(<<-SQL)
10+
UPDATE questions SET question_type = REPLACE(question_type, 'Question', '')
11+
SQL
12+
end
13+
end

example_app/db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20121128194213) do
14+
ActiveRecord::Schema.define(:version => 20121128221331) do
1515

1616
create_table "answers", :force => true do |t|
1717
t.integer "completion_id", :null => false

example_app/spec/factories/application.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
factory :question do
1919
survey
2020
title 'Question'
21-
question_type 'Open'
21+
question_type 'OpenQuestion'
2222

2323
factory :multiple_choice_question do
2424
ignore do
@@ -31,15 +31,15 @@
3131
end
3232
end
3333

34-
question_type 'MultipleChoice'
34+
question_type 'MultipleChoiceQuestion'
3535
end
3636

3737
factory :open_question do
38-
question_type 'Open'
38+
question_type 'OpenQuestion'
3939
end
4040

4141
factory :scale_question do
42-
question_type 'Scale'
42+
question_type 'ScaleQuestion'
4343
end
4444
end
4545

example_app/spec/models/question_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
describe Question do
44
it { should validate_presence_of :question_type }
55

6-
Question::SUBMITTABLE_TYPES.each do |type|
6+
Question::QUESTION_TYPES.each do |type|
77
it { should allow_value(type).for(:question_type) }
88
end
99

0 commit comments

Comments
 (0)