Created
October 30, 2014 22:31
-
-
Save dleve123/3bae62d321030289231f to your computer and use it in GitHub Desktop.
Possible Refactoring for Survey Creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def SurveysController | |
... | |
def create | |
... | |
new_survey_creation = NewSurveyCreation.new(request.subdomain, create_patient_params).create! | |
... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/