Permalink
Browse files

Replace Subclasses with Strategies: Step Twelve

* Pass the type when instantiating the strategy
  • Loading branch information...
jferris committed Feb 7, 2013
1 parent 662e508 commit a3b36db9f0ec2d66e0ec1e7732662732380e6fc8
@@ -32,7 +32,7 @@ def update

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

@@ -3,7 +3,7 @@ def new
@question = Question.find(params[:question_id])
assign_type
@new_question = type.constantize.new
@new_question.build_submittable({})
@new_question.build_submittable(type, {})
end

def create
@@ -20,7 +20,7 @@ def most_recent_answer_text
answers.most_recent.text
end

def build_submittable(attributes)
def build_submittable(type, attributes)
submittable_class = type.sub('Question', 'Submittable').constantize
self.submittable = submittable_class.new(attributes.merge(question: self))
end
@@ -34,7 +34,7 @@ def switch_to(type, new_attributes, submittable_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(submittable_attributes)
new_question.build_submittable(type, submittable_attributes)
new_question.id = id

begin
@@ -17,11 +17,11 @@
end

describe Question, '#build_submittable' do
it 'builds a submittable with its type and the given attributes' do
it 'builds a submittable with the given type and attributes' do
expected_value = 1.day.ago
question = build(:open_question)
question = build(:question)

question.build_submittable(created_at: expected_value)
question.build_submittable('OpenSubmittable', created_at: expected_value)

question.submittable.created_at.should eq expected_value
question.submittable.should be_a(OpenSubmittable)

0 comments on commit a3b36db

Please sign in to comment.