Skip to content

Commit a3b36db

Browse files
committed
Replace Subclasses with Strategies: Step Twelve
* Pass the type when instantiating the strategy
1 parent 662e508 commit a3b36db

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

example_app/app/controllers/questions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def update
3232

3333
def build_question
3434
@question = type.constantize.new(question_params)
35-
@question.build_submittable(submittable_params)
35+
@question.build_submittable(type, submittable_params)
3636
@question.survey = @survey
3737
end
3838

example_app/app/controllers/types_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def new
33
@question = Question.find(params[:question_id])
44
assign_type
55
@new_question = type.constantize.new
6-
@new_question.build_submittable({})
6+
@new_question.build_submittable(type, {})
77
end
88

99
def create

example_app/app/models/question.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def most_recent_answer_text
2020
answers.most_recent.text
2121
end
2222

23-
def build_submittable(attributes)
23+
def build_submittable(type, attributes)
2424
submittable_class = type.sub('Question', 'Submittable').constantize
2525
self.submittable = submittable_class.new(attributes.merge(question: self))
2626
end
@@ -34,7 +34,7 @@ def switch_to(type, new_attributes, submittable_attributes)
3434
attributes = self.attributes.merge(new_attributes)
3535
cloned_attributes = attributes.except('id', 'type', 'submittable_type')
3636
new_question = type.constantize.new(cloned_attributes)
37-
new_question.build_submittable(submittable_attributes)
37+
new_question.build_submittable(type, submittable_attributes)
3838
new_question.id = id
3939

4040
begin

example_app/spec/models/question_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
end
1818

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

24-
question.build_submittable(created_at: expected_value)
24+
question.build_submittable('OpenSubmittable', created_at: expected_value)
2525

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

0 commit comments

Comments
 (0)