Skip to content

Instantly share code, notes, and snippets.

@dleve123
Created October 30, 2014 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dleve123/3bae62d321030289231f to your computer and use it in GitHub Desktop.
Save dleve123/3bae62d321030289231f to your computer and use it in GitHub Desktop.
Possible Refactoring for Survey Creation
class NewSurveyCreation
attr_reader :patient
def initialize(subdomain, params)
# store the data in instance variables
] end
def create!
set_patient_age!
set_bmi!
end
def set_patient_age
patient.age = to_age(date_of_birth);
answer_age_question!(age)
end
# Not needed and can be handled by a method missing
def set_bmi
answer_bmi_question
end
def to_age
# Do a lot of work
end
def method_missing
question = ImplicitQuestion.find_by(missing_method.to_s.split('_').last)
survey.responses.create(question: question, form_value: method)
hopkins_api.post(:new_creation)
end
def answer_age_question!
question = ImplicitQuestion.find_by(prompt: 'Age')
survey.responses.create(question: question, form_value: age)
end
end
def SurveysController
...
def create
...
new_survey_creation = NewSurveyCreation.new(request.subdomain, create_patient_params).create!
...
end
end
@dleve123
Copy link
Author

This will not execute. Just saving here to demonstrate the "heart" of a Service Object refactoring.

http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment