-
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
12179 create Answers table and migrate existing response data to populate it #966
Open
DevneyHamilton
wants to merge
32
commits into
develop
Choose a base branch
from
12179_answers_migration
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 26 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
472b045
12132: first pass at migration code, make_answers method working for …
DevneyHamilton 8d055ca
12132: notes on how things currently work and changes to make
DevneyHamilton 2f409ed
12132: adds answer table in schema.rb
DevneyHamilton 5a0c013
12132: make answers method on response set
DevneyHamilton a5c831a
Merge branch 'develop' into 12132_response_refactor
DevneyHamilton 27d45f8
12132: remove duplicate field in schema from merge, and add wait time…
DevneyHamilton 92de0e3
WIP 12132: attempting specs on answers table to custom data and back …
DevneyHamilton 737a5dc
12132: saving form data to answers table, needs auto tests
DevneyHamilton cccc5d2
12132: adding answers and saving, reloading questionnaire for that lo…
DevneyHamilton f779c1d
12179: version with answer row for every question on a response set
DevneyHamilton 0db9d79
12179: one time changes rake task to make answers
DevneyHamilton f8de5b5
WIP failing specs on 12179
DevneyHamilton 506add7
WIP 12197: more specs passing
DevneyHamilton 7cba41a
12197: remove response blank specs, and fix some of the boolean handl…
DevneyHamilton 7119f49
12179: fixing not applicable boolean madness
DevneyHamilton d2348c7
12179: fix displaying linked document
DevneyHamilton 3442136
12179: make validations on answer more expansive; just check there is…
DevneyHamilton 5a21afe
12197: WIP enhanced data export working
DevneyHamilton 9a0e644
12179: cleaning up debugging from work making data exports work with…
DevneyHamilton 61377bf
12179: keep handling string numeric answers in data exports
DevneyHamilton 7b7b952
12179: create shared specs between enhanced and numeric data exports,…
DevneyHamilton b76367e
12179: WIP specs breaking because internal names not unique, which is…
DevneyHamilton ab44b97
12179: specs passing after addressing problem with question factory c…
DevneyHamilton c8bdab4
12179: clean up debug code, specs passing and manual tests passing
DevneyHamilton c6d7571
12179: fix problem where data exports did not support exporting from …
DevneyHamilton 5a3a089
12179: add boolean flag to filtered question serializer so inheritanc…
DevneyHamilton 01ff40c
12179: cleaning up comments etc
DevneyHamilton 88e241c
12179: fix handling of boolean answers, which were saving unanswered …
DevneyHamilton a76c04e
12197: ensure internal names unique
DevneyHamilton 2966a21
Merge branch 'develop' into 12179_answers_migration
DevneyHamilton 76b93c5
12179: specs passing, added workaround where internal names for non-g…
DevneyHamilton 99daf10
12179: greatly simplify database migrations and move them to run afte…
DevneyHamilton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
class Answer < ApplicationRecord | ||
belongs_to :response_set, optional: false | ||
belongs_to :question, optional: false | ||
delegate :data_type, to: :question | ||
validate :has_data | ||
validate :question_is_not_group | ||
|
||
# this method is temporary for spr 2022 overhaul | ||
def compare_to_custom_data | ||
custom_data_raw_data = response_set.custom_data[question.id.to_s] | ||
custom_data_response = Response.new(loan: response_set.loan, question: question, response_set: response_set, data: custom_data_raw_data) | ||
answer_response = Response.new(loan: response_set.loan, question: question, response_set: response_set, data: raw_value) | ||
methods = [:loan, :question, :response_set, :text, :number, :boolean, :rating, :url, :start_cell, :end_cell, :owner, :breakeven, :business_canvas, :not_applicable] | ||
methods.each do |m| | ||
custom_data_value = custom_data_response.send(m) | ||
answer_value = answer_response.send(m) | ||
unless custom_data_value == answer_value | ||
raise "ERROR for answer #{id}: for RS #{response_set.id} custom data value for #{m} is #{custom_data_value} but is #{answer_value} for answer #{id}" | ||
end | ||
end | ||
end | ||
|
||
def to_s | ||
"RS: #{response_set.question_set.kind}, Q: #{question.label.to_s} | NA: #{not_applicable}; text: #{text_data}; numeric: #{numeric_data}; boolean: #{boolean_data}; doc: #{linked_document_data}; breakeven: #{breakeven_data}; canvas: #{business_canvas_data}" | ||
end | ||
|
||
def blank? | ||
!not_applicable? && | ||
text_data.blank? && | ||
numeric_data.blank? && | ||
boolean_data.blank? && | ||
linked_document_data_blank? && | ||
business_canvas_blank? && | ||
breakeven_data_blank? | ||
end | ||
|
||
def linked_document_data_blank? | ||
linked_document_data.blank? || json_answer_blank?(linked_document_data) | ||
end | ||
|
||
def business_canvas_blank? | ||
business_canvas_data.blank? || json_answer_blank?(business_canvas_data) | ||
end | ||
|
||
def breakeven_data_blank? | ||
breakeven_data.blank? || json_answer_blank?(breakeven_data) | ||
end | ||
|
||
def self.json_answer_blank?(answer_json) | ||
answer_json.values.all?{|v| v.blank?} | ||
end | ||
|
||
# expects 'raw_value' type json e.g. the value of a "field_110" key in form submission | ||
# or the value of a q_id key e.g. "5126" in custom_data | ||
def self.contains_answer_data?(hash_data) | ||
hash_data.each do |key, value| | ||
if %w(text number rating url start_cell end_cell).include?(key) | ||
return true unless value.blank? | ||
elsif key == "not_applicable" | ||
return true if value == "yes" | ||
elsif key == "boolean" | ||
return true unless value.nil? | ||
elsif %w(business_canvas).include?(key) | ||
return true unless self.json_answer_blank?(value) | ||
elsif %w(breakeven).include?(key) | ||
value.each do |subkey, subvalue| | ||
if %w(products fixed_costs).include?(subkey) | ||
subvalue.each {|i| return true unless self.json_answer_blank?(i)} | ||
else | ||
return true unless subvalue.empty? | ||
end | ||
end | ||
end | ||
end | ||
return false | ||
end | ||
|
||
# this method is temporary for spr 2022 overhaul | ||
# doesn't save blank answers | ||
def self.save_from_form_field_params(question, fields, response_set) | ||
unless question.group? || !self.contains_answer_data?(fields) | ||
not_applicable = fields.key?("not_applicable") ? (fields["not_applicable"] == "yes") : "no" | ||
text_data = fields.key?("text") ? fields["text"] : nil | ||
numeric_data = if fields.key?("number") | ||
fields["number"] | ||
elsif fields.key?("rating") | ||
fields["rating"] | ||
else | ||
nil | ||
end | ||
boolean_data = fields.key?("boolean") ? (fields["boolean"] == "yes") : nil | ||
breakeven_data = fields.key?("breakeven") ? fields["breakeven"] : nil | ||
business_canvas_data = fields.key?("business_canvas") ? fields["business_canvas"] : nil | ||
linked_document_data = fields.key?("url") ? {"url": fields["url"] } : {"url": ""} | ||
linked_document_data["start_cell"] = fields.key?("start_cell") ? fields["start_cell"] : "" | ||
linked_document_data["end_cell"] = fields.key?("end_cell") ? fields["end_cell"] : "" | ||
answer = Answer.find_or_create_by(response_set: response_set, question: question) | ||
answer.update!({ | ||
not_applicable: not_applicable, | ||
text_data: text_data, | ||
numeric_data: numeric_data, | ||
boolean_data: boolean_data, | ||
breakeven_data: breakeven_data, | ||
business_canvas_data: business_canvas_data, | ||
linked_document_data: linked_document_data | ||
}) | ||
end | ||
end | ||
|
||
def question_is_not_group | ||
question.data_type != "group" | ||
end | ||
|
||
def has_data | ||
errors.add("Answer contains no data") unless | ||
not_applicable || | ||
text_data.present? || | ||
numeric_data.present? || | ||
!boolean_data.nil? || | ||
linked_document_data.present? || | ||
business_canvas_data.present? || | ||
breakeven_data.present? | ||
end | ||
|
||
# temp method for spr 2022 overhaul | ||
def raw_value | ||
json = {} | ||
json["not_applicable"] = self.not_applicable ? "yes" : "no" | ||
if self.text_data.present? | ||
json["text"] = self.text_data | ||
end | ||
unless self.boolean_data.nil? | ||
json["boolean"] = self.boolean_data ? "yes" : "no" | ||
end | ||
if self.breakeven_data.present? | ||
json["breakeven"] = self.breakeven_data | ||
end | ||
if self.business_canvas_data.present? | ||
json["business_canvas"] = self.business_canvas_data | ||
end | ||
if self.numeric_data.present? | ||
if self.question.data_type == "range" | ||
json["rating"] = self.numeric_data | ||
else | ||
json["number"] = self.numeric_data | ||
end | ||
end | ||
if self.linked_document_data.present? | ||
json["url"] = self.linked_document_data["url"] | ||
json["start_cell"] = self.linked_document_data["start_cell"] | ||
json["end_cell"] = self.linked_document_data["end_cell"] | ||
end | ||
json | ||
end | ||
|
||
# temp method for spr 2022 overhaul | ||
# return the value of json that would be in legacy custom_data field on response set for this answer's question | ||
def custom_data_json | ||
return {"#{self.question.json_key}": self.raw_value} | ||
end | ||
|
||
def answer_for_csv(allow_text_like_numeric: false) | ||
return nil if not_applicable | ||
|
||
case question.data_type | ||
when "text" | ||
text_data | ||
when "number", "currency", "percentage", "range" | ||
if allow_text_like_numeric || (true if Float(numeric_data) rescue false) | ||
numeric_data.to_s | ||
else | ||
nil | ||
end | ||
when "boolean" | ||
boolean_data.nil? ? nil : (boolean_data ? "yes" : "no") | ||
# "breakeven" and "business_canvas" never exported to csv | ||
else | ||
raise "invalid question data type #{question.data_type}" | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
the previous version of this meant that if one is in a division that inherits question set(s) from ancestor divisions when making a data export, those inherited question sets were skipped.