-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(survey): [PPT-67|PPT-101] update /surveys/questions endpoint #257
feat(survey): [PPT-67|PPT-101] update /surveys/questions endpoint #257
Conversation
if question_model.persisted? && Survey::Answer.query.where(question_id: question_model.id).count > 0 | ||
question_model.soft_delete | ||
question_model.clear_persisted |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having hard time in understanding the logic here. We are updating the field in database, but mutating ivars to represent state which is different from db.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question_model.persisted?
is true
if the model is in the database.
Survey::Answer.query.where(question_id: question_model.id).count > 0
is true
if there are any linked answers.
So if the model is in the database and there are linked answers, then:
question_model.soft_delete
we mark it as deletedquestion_model.clear_persisted
clears the persistent state so that our changed model gets inserted as a new row in the database and gets a new id.
By doing this we ensure that questions with linked answers are never changed as changing the question could change the meaning of the answers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, Clear does not have the ability to cancel update/insert in the lifecycle hooks, so you either have to work around that or move the logic to the controllers (putting it in the controllers, feels like a future bug waiting to happen).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question_model.soft_delete
uses Clear::SQL.execute Clear::SQL.raw(...)
to bypass the lifecycle hooks. If it did not bypass the lifecycle hooks then it would create an infinite loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Get endpoint for survey questions:
Update endpoint for survey questions:
Delete endpoint for survey questions: