Skip to content

Commit 84bcdb7

Browse files
authored
Switch to build_csv_rows to yield via find_each to dedupe (#2469)
1 parent fdbc027 commit 84bcdb7

3 files changed

Lines changed: 29 additions & 49 deletions

File tree

app/models/application_record.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ class ApplicationRecord < ActiveRecord::Base
44
self.abstract_class = true
55

66
def self.build_csv_rows(objects)
7-
return [] if objects.blank?
7+
return if objects.blank?
88

99
klass = objects.klass
10-
data = [["#{klass.name.underscore}_info"]]
1110
attributes = if klass.const_defined?(:USER_DATA_ATTRIBUTES)
1211
klass.const_get(:USER_DATA_ATTRIBUTES)
1312
else
1413
klass.column_names
1514
end
16-
data << attributes
17-
objects.each do |object|
18-
data << attributes.map { |attribute| object.send(attribute.to_sym) }
19-
end
20-
data
15+
yield ["#{klass.name.underscore}_info"]
16+
yield attributes
17+
objects.find_each { |object| yield attributes.map { |attribute| object.send(attribute.to_sym) } }
2118
end
2219
end

app/models/user.rb

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,19 @@ def build_csv_data
187187
yield ['user_info']
188188
yield USER_DATA_ATTRIBUTES
189189
yield USER_DATA_ATTRIBUTES.map { |attribute| send(attribute.to_sym) }
190-
Group.build_csv_rows(groups).each { |row| yield row }
191-
GroupMember.build_csv_rows(group_members).each { |row| yield row }
192-
Category.build_csv_rows(categories).each { |row| yield row }
193-
Medication.build_csv_rows(medications).each { |row| yield row }
194-
Strategy.build_csv_rows(strategies).each { |row| yield row }
195-
Moment.build_csv_rows(moments).each { |row| yield row }
196-
Notification.build_csv_rows(notifications).each { |row| yield row }
197-
Mood.build_csv_rows(moods).each { |row| yield row }
198-
CarePlanContact.build_csv_rows(care_plan_contacts).each { |row| yield row }
199-
Allyship.build_csv_rows(allyships).each { |row| yield row }
200-
MeetingMember.build_csv_rows(meeting_members).each { |row| yield row }
201-
build_comment_csv_data.each { |row| yield row }
202-
build_led_group_meeting_csv_data.each { |row| yield row }
190+
Group.build_csv_rows(groups) { |row| yield row }
191+
GroupMember.build_csv_rows(group_members) { |row| yield row }
192+
Category.build_csv_rows(categories) { |row| yield row }
193+
Medication.build_csv_rows(medications) { |row| yield row }
194+
Strategy.build_csv_rows(strategies) { |row| yield row }
195+
Moment.build_csv_rows(moments) { |row| yield row }
196+
Notification.build_csv_rows(notifications) { |row| yield row }
197+
Mood.build_csv_rows(moods) { |row| yield row }
198+
CarePlanContact.build_csv_rows(care_plan_contacts) { |row| yield row }
199+
Allyship.build_csv_rows(allyships) { |row| yield row }
200+
MeetingMember.build_csv_rows(meeting_members) { |row| yield row }
201+
build_comment_csv_data { |row| yield row }
202+
build_led_group_meeting_csv_data { |row| yield row }
203203
end
204204

205205
def generate_data_request
@@ -250,28 +250,25 @@ def delete_stale_data_file
250250
private
251251

252252
def build_comment_csv_data
253-
moment_ids = moments.map(&:id)
254-
strategy_ids = strategies.map(&:id)
255-
data = []
256-
data += Comment.build_csv_rows(
253+
moment_ids = Moment.where(user_id: id).pluck(:id)
254+
strategy_ids = Strategy.where(user_id: id).pluck(:id)
255+
Comment.build_csv_rows(
257256
Comment.where(commentable_type: 'Moment', commentable_id: moment_ids)
258-
)
259-
data += Comment.build_csv_rows(
257+
) { |row| yield row }
258+
Comment.build_csv_rows(
260259
Comment.where(commentable_type: 'Strategy', commentable_id: strategy_ids)
261-
)
262-
data
260+
) { |row| yield row }
263261
end
264262

265263
def build_led_group_meeting_csv_data
266-
leader_group_ids = group_members.where(leader: true).pluck(:group_id)
267-
return [] if leader_group_ids.empty?
264+
leader_group_ids = GroupMember.where(user_id: id, leader: true).pluck(:group_id)
265+
return if leader_group_ids.empty?
268266

269267
leader_meetings = Meeting.where(group_id: leader_group_ids)
270-
data = Meeting.build_csv_rows(leader_meetings)
271-
data += Comment.build_csv_rows(
268+
Meeting.build_csv_rows(leader_meetings) { |row| yield row }
269+
Comment.build_csv_rows(
272270
Comment.where(commentable_type: 'Meeting', commentable_id: leader_meetings.pluck(:id))
273-
)
274-
data
271+
) { |row| yield row }
275272
end
276273

277274
def oauth_provided?

app/models/users/data_request.rb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ class DataRequest < ApplicationRecord
2020
deleted: 4
2121
}.freeze
2222

23-
ASSOCIATIONS_TO_EXPORT = %i[
24-
allyships
25-
group_members
26-
groups
27-
categories
28-
medications
29-
strategies
30-
moments
31-
notifications
32-
moods
33-
care_plan_contacts
34-
meeting_members
35-
].freeze
36-
3723
DEFAULT_FILE_PATH = Rails.root.join('tmp/csv_data')
3824

3925
belongs_to :user, class_name: '::User'
@@ -71,7 +57,7 @@ def enqueue_download_request
7157
end
7258

7359
def create_csv
74-
user = User.includes(*ASSOCIATIONS_TO_EXPORT).find(user_id)
60+
user = User.find(user_id)
7561
begin
7662
require 'csv'
7763
require 'zlib'

0 commit comments

Comments
 (0)