Permalink
Browse files

Replace Subclasses with Strategies: Step Fifteen

* Simplify type switching
  • Loading branch information...
jferris committed Feb 7, 2013
1 parent c6f0e54 commit 5f4a14ff6c43bf5b846d1c58d7509861c6fe3ac1
@@ -1,37 +1,25 @@
class TypesController < ApplicationController
def new
@question = Question.find(params[:question_id])
assign_type
@new_question = Question.new
@new_question.build_submittable(type, {})
@question.build_submittable(type, {})
end

def create
@question = Question.find(params[:question_id])
@new_question = @question.switch_to(
@question.switch_to(
type,
question_params,
submittable_attributes
)

if @new_question.errors.empty?
if @question.errors.empty?
redirect_to @question.survey
else
assign_type
render 'new'
end
end

private

def assign_type
@new_type = type.underscore
end

def question_params
params[:question].except(:submittable_attributes)
end

def submittable_attributes
params[:question][:submittable_attributes] || {}
end
@@ -34,21 +34,14 @@ def summarize(summarizer)
Summary.new(title, value)
end

def switch_to(type, new_attributes, submittable_attributes)
attributes = self.attributes.merge(new_attributes)
cloned_attributes = attributes.except('id', 'type', 'submittable_type')
new_question = Question.new(cloned_attributes)
new_question.build_submittable(type, submittable_attributes)
new_question.id = id

begin
Question.transaction do
destroy
new_question.save!
def switch_to(type, attributes)
old_submittable = submittable
build_submittable type, attributes

transaction do
if save
old_submittable.destroy
end
rescue ActiveRecord::RecordInvalid
end

new_question
end
end
@@ -1,13 +1,14 @@
<%= simple_form_for(
@new_question,
@question,
as: :question,
url: question_types_path(@question)
url: question_types_path(@question),
method: :post
) do |form| -%>

<%= form.hidden_field :submittable_type %>
<%= render(
"#{@new_type}s/#{@new_type}_form",
question: @new_question,
"#{@question.submittable.to_partial_path}_form",
submittable: @question.submittable,
form: form
) %>
<%= form.submit 'Change Type' %>
@@ -65,26 +65,27 @@
it 'changes the question type and deletes the old question when valid' do
question = create(:open_question)

new_question = question.switch_to(
question.switch_to(
'ScaleQuestion',
{},
{ minimum: 1, maximum: 2 })
{ minimum: 1, maximum: 2 }
)

new_question.errors.should be_empty
submittable = new_question.submittable
question.errors.should be_empty
submittable = question.submittable
submittable.should be_a(ScaleSubmittable)
submittable.minimum.should eq 1
submittable.maximum.should eq 2
Question.count.should eq 1
OpenSubmittable.count.should eq 0
end

it 'leaves the question alone when the new attributes are invalid' do
question = create(:open_question)

new_question = question.switch_to('ScaleQuestion', {}, { minimum: 1 })
question.switch_to('ScaleQuestion', { minimum: 1 })

new_question.errors.should be_present
new_question.submittable.should be_a(ScaleSubmittable)
new_question.submittable.minimum.should eq 1
question.errors.should be_present
question.submittable.should be_a(ScaleSubmittable)
question.submittable.minimum.should eq 1
end
end

0 comments on commit 5f4a14f

Please sign in to comment.