Skip to content

Commit

Permalink
Introduce single table inheritence for Question
Browse files Browse the repository at this point in the history
* Just introduces subclasses and type column
* Takes no advantage of polymorphism yet
  • Loading branch information
jferris committed Nov 29, 2012
1 parent b535171 commit c18ebeb
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example_app/app/controllers/questions_controller.rb
Expand Up @@ -19,7 +19,7 @@ def create
private

def build_question
@question = @survey.questions.new(question_params)
@question = @survey.questions.new(question_params, without_protection: true)
end

def question_params
Expand Down
2 changes: 2 additions & 0 deletions example_app/app/models/multiple_choice_question.rb
@@ -0,0 +1,2 @@
class MultipleChoiceQuestion < Question
end
2 changes: 2 additions & 0 deletions example_app/app/models/open_question.rb
@@ -0,0 +1,2 @@
class OpenQuestion < Question
end
2 changes: 2 additions & 0 deletions example_app/app/models/question.rb
Expand Up @@ -3,6 +3,8 @@ class Question < ActiveRecord::Base

QUESTION_TYPES = %w(OpenQuestion MultipleChoiceQuestion ScaleQuestion).freeze

set_inheritance_column 'question_type'

validates :maximum, presence: true, if: :scale?
validates :minimum, presence: true, if: :scale?
validates :question_type, presence: true, inclusion: QUESTION_TYPES
Expand Down
2 changes: 2 additions & 0 deletions example_app/app/models/scale_question.rb
@@ -0,0 +1,2 @@
class ScaleQuestion < Question
end
4 changes: 3 additions & 1 deletion example_app/app/views/surveys/show.html.erb
Expand Up @@ -18,7 +18,9 @@
<%= simple_form_for [@survey, @completion] do |form| -%>
<ol>
<%= render @questions, form: form %>
<% @questions.each do |question| -%>
<%= render 'questions/question', question: question, form: form %>
<% end -%>
</ol>
<%= form.submit 'Submit Answers' %>
<% end -%>

0 comments on commit c18ebeb

Please sign in to comment.