Permalink
Browse files

Replace Subclasses with Strategies: Step Eight

* Introduce polymorphic association for strategy
* Build the strategy when building the root type
  • Loading branch information...
jferris committed Feb 7, 2013
1 parent e4809cd commit 7d6e294ef8d0e427f83710f74448768da80af2d4
@@ -32,6 +32,7 @@ def update

def build_question
@question = type.constantize.new(question_params)
@question.build_submittable
@question.survey = @survey
end

@@ -6,6 +6,7 @@ class Question < ActiveRecord::Base
validates :type, presence: true, inclusion: QUESTION_TYPES
validates :title, presence: true

belongs_to :submittable, polymorphic: true
belongs_to :survey
has_many :answers

@@ -16,9 +17,9 @@ def most_recent_answer_text
answers.most_recent.text
end

def submittable
def build_submittable
submittable_class = type.sub('Question', 'Submittable').constantize
submittable_class.new(question: self)
self.submittable = submittable_class.new(question: self)
end

def summarize(summarizer)
@@ -30,6 +31,7 @@ def switch_to(type, new_attributes)
attributes = self.attributes.merge(new_attributes)
cloned_attributes = attributes.except('id', 'type', 'submittable_type')
new_question = type.constantize.new(cloned_attributes)
new_question.build_submittable
new_question.id = id

begin
@@ -0,0 +1,6 @@
class AddSubmittableTypeAndIdToQuestions < ActiveRecord::Migration
def change
add_column :questions, :submittable_id, :integer
add_column :questions, :submittable_type, :string
end
end
@@ -67,13 +67,15 @@
end

create_table "questions", :force => true do |t|
t.string "title", :null => false
t.string "type", :null => false
t.integer "survey_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "title", :null => false
t.string "type", :null => false
t.integer "survey_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "minimum"
t.integer "maximum"
t.integer "submittable_id"
t.string "submittable_type"
end

create_table "scale_submittables", :force => true do |t|
@@ -10,6 +10,12 @@
user
end

factory :multiple_choice_submittable do
end

factory :open_submittable do
end

factory :option do
text 'Hello'
end
@@ -28,17 +34,25 @@
FactoryGirl.build(:option, text: text, question_id: attributes.id)
end
end

submittable factory: :multiple_choice_submittable
end

factory :open_question, class: 'OpenQuestion' do
submittable factory: :open_submittable
end

factory :scale_question, class: 'ScaleQuestion' do
minimum 1
maximum 2

submittable factory: :scale_submittable
end
end

factory :scale_submittable do
end

factory :survey do
sequence(:title) { |n| "Survey #{n}" }
author factory: :user
@@ -11,10 +11,21 @@

it { should validate_presence_of :title }

it { should belong_to(:submittable) }
it { should belong_to(:survey) }
it { should have_many(:answers) }
end

describe Question, '#build_submittable' do
it 'builds a submittable with its type' do
question = build(:open_question)

question.build_submittable

question.submittable.should be_a(OpenSubmittable)
end
end

describe Question, '#most_recent_answer_text' do
it 'returns the text for the latest answer' do
question = create(:open_question)
@@ -36,13 +47,6 @@
end
end

describe Question, '#submittable' do
it 'instantiates a submittable based on its type' do
question = create(:open_question)
question.submittable.should be_a(OpenSubmittable)
end
end

describe Question, '#summarize' do
it 'builds a summary with the result from the summarizer' do
question = build_stubbed(:question)

0 comments on commit 7d6e294

Please sign in to comment.