Skip to content

Commit

Permalink
Fix issues with ResponseDomain being linked from col on question grids
Browse files Browse the repository at this point in the history
  • Loading branch information
simonreed committed Jul 26, 2019
1 parent 6abc5e4 commit 47c97a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ def update_cols(cols)
else
cols.sort_by! { |x| x[:order] }
cols.each do |col|
if col[:rd].nil?
rd = self.rds_qs.find_by_code_id col[:value]
rd.delete unless rd.nil?
else
rd = self.instrument.association(col[:rd][:type].tableize).reader.find col[:rd][:id]
self.rds_qs.create question: self, response_domain: rd, code_id: col[:value], instrument_id: Prefix[self.instrument_id]
existing_rd = self.rds_qs.where(code_id: col[:value]).first
# Get the ResponseDomain from the col hash. If the type and id aren't
# found in the db or if we're removing an assoication then this
# will rescue to nil.
new_rd = self.instrument.association(col[:rd][:type].tableize).reader.find(col[:rd][:id]) rescue nil
if existing_rd && new_rd != existing_rd
existing_rd.delete
end
if new_rd
self.rds_qs.create question: self, response_domain: new_rd, code_id: col[:value], instrument_id: Prefix[self.instrument_id]
end
end
end
Expand Down Expand Up @@ -104,4 +108,4 @@ def no_duplicates
end
end
end
end
end
1 change: 1 addition & 0 deletions test/fixtures/rds_qs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5974,6 +5974,7 @@ RdsQs_374:
response_domain_type: ResponseDomainCode
question_id: 196
question_type: QuestionGrid
code_id: 1
created_at: !ruby/object:ActiveSupport::TimeWithZone
utc: &748 2016-11-08 12:22:42.896142000 Z
zone: *2
Expand Down
25 changes: 25 additions & 0 deletions test/models/question_grid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,29 @@ class QuestionGridTest < ActiveSupport::TestCase
assert_kind_of ActiveRecord::Associations::CollectionProxy, @question_grid.cc_questions
assert_equal @question_grid.constructs, @question_grid.cc_questions
end

test '.update cols will remove existing assocation if cols have no response domain defined' do
assert_difference('RdsQs.count', -1) do
cols = @question_grid.horizontal_code_list.codes.map{|code| { value: code.value, rd: nil}}
@question_grid.update_cols(cols)
end
end

test '.update cols will update existing assocation if cols has a different response domain' do
response_domain_code = response_domain_codes :ResponseDomainCode_1
assert_difference('RdsQs.count', 0) do
cols = @question_grid.horizontal_code_list.codes.map{|code| { value: code.value, rd: { type: response_domain_code.class.name, id: response_domain_code.id }}}
@question_grid.update_cols(cols)
end
assert_equal @question_grid.rds_qs.first.response_domain, response_domain_code
end

test '.update cols will create new assocation if no exisitng rds_qs was found' do
response_domain_code = response_domain_codes :ResponseDomainCode_1
@question_grid.rds_qs.delete_all
assert_difference('RdsQs.count', 1) do
cols = @question_grid.horizontal_code_list.codes.map{|code| { value: code.value, rd: { type: response_domain_code.class.name, id: response_domain_code.id }}}
@question_grid.update_cols(cols)
end
end
end

0 comments on commit 47c97a9

Please sign in to comment.