Skip to content

Commit 59c3a45

Browse files
committed
Fix rubocop issues
1 parent 7a4b071 commit 59c3a45

File tree

6 files changed

+54
-35
lines changed

6 files changed

+54
-35
lines changed

app/services/reverse_geocoding/places/fetch_data.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def save_places(places_to_create, places_to_update)
134134
updated_at: Time.current
135135
}
136136
end
137-
Place.insert_all(place_attributes)
137+
Place.insert_all(place_attributes) # rubocop:disable Rails/SkipsModelValidations
138138
end
139139

140140
return unless places_to_update.any?

app/services/users/import_data/areas.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def filter_and_prepare_areas
5353
valid_areas << prepared_attributes if prepared_attributes
5454
end
5555

56-
Rails.logger.warn "Skipped #{skipped_count} areas with invalid or missing required data" if skipped_count > 0
56+
Rails.logger.warn "Skipped #{skipped_count} areas with invalid or missing required data" if skipped_count.positive?
5757

5858
valid_areas
5959
end
@@ -108,7 +108,9 @@ def bulk_import_areas(areas)
108108
batch_created = result.count
109109
total_created += batch_created
110110

111-
Rails.logger.debug "Processed batch of #{batch.size} areas, created #{batch_created}, total created: #{total_created}"
111+
Rails.logger.debug(
112+
"Processed batch of #{batch.size} areas, created #{batch_created}, total created: #{total_created}"
113+
)
112114
rescue StandardError => e
113115
Rails.logger.error "Failed to process area batch: #{e.message}"
114116
Rails.logger.error "Batch size: #{batch.size}"
@@ -120,9 +122,9 @@ def bulk_import_areas(areas)
120122

121123
def valid_area_data?(area_data)
122124
return false unless area_data.is_a?(Hash)
123-
return false unless area_data['name'].present?
124-
return false unless area_data['latitude'].present?
125-
return false unless area_data['longitude'].present?
125+
return false if area_data['name'].blank?
126+
return false if area_data['latitude'].blank?
127+
return false if area_data['longitude'].blank?
126128

127129
true
128130
rescue StandardError => e

app/services/users/import_data/notifications.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def filter_and_prepare_notifications
5252
valid_notifications << prepared_attributes if prepared_attributes
5353
end
5454

55-
if skipped_count > 0
55+
if skipped_count.positive?
5656
Rails.logger.warn "Skipped #{skipped_count} notifications with invalid or missing required data"
5757
end
5858

@@ -64,7 +64,7 @@ def prepare_notification_attributes(notification_data)
6464

6565
attributes['user_id'] = user.id
6666

67-
attributes['created_at'] = Time.current unless attributes['created_at'].present?
67+
attributes['created_at'] = Time.current if attributes['created_at'].blank?
6868

6969
attributes['updated_at'] = Time.current
7070

@@ -78,15 +78,7 @@ def prepare_notification_attributes(notification_data)
7878
def filter_existing_notifications(notifications)
7979
return notifications if notifications.empty?
8080

81-
existing_notifications_lookup = {}
82-
user.notifications.select(:title, :content, :created_at, :kind).each do |notification|
83-
primary_key = [notification.title.strip, notification.content.strip]
84-
85-
exact_key = [notification.title.strip, notification.content.strip, normalize_timestamp(notification.created_at)]
86-
87-
existing_notifications_lookup[primary_key] = true
88-
existing_notifications_lookup[exact_key] = true
89-
end
81+
lookup = build_existing_notifications_lookup
9082

9183
notifications.reject do |notification|
9284
title = notification[:title]&.strip
@@ -95,7 +87,7 @@ def filter_existing_notifications(notifications)
9587
primary_key = [title, content]
9688
exact_key = [title, content, normalize_timestamp(notification[:created_at])]
9789

98-
if existing_notifications_lookup[primary_key] || existing_notifications_lookup[exact_key]
90+
if lookup[primary_key] || lookup[exact_key]
9991
Rails.logger.debug "Notification already exists: #{notification[:title]}"
10092
true
10193
else
@@ -104,6 +96,18 @@ def filter_existing_notifications(notifications)
10496
end
10597
end
10698

99+
def build_existing_notifications_lookup
100+
lookup = {}
101+
user.notifications.select(:title, :content, :created_at, :kind).each do |notification|
102+
title = notification.title.strip
103+
content = notification.content.strip
104+
105+
lookup[[title, content]] = true
106+
lookup[[title, content, normalize_timestamp(notification.created_at)]] = true
107+
end
108+
lookup
109+
end
110+
107111
def normalize_timestamp(timestamp)
108112
case timestamp
109113
when String then Time.parse(timestamp).utc.to_i
@@ -131,7 +135,9 @@ def bulk_import_notifications(notifications)
131135
batch_created = result.count
132136
total_created += batch_created
133137

134-
Rails.logger.debug "Processed batch of #{batch.size} notifications, created #{batch_created}, total created: #{total_created}"
138+
Rails.logger.debug(
139+
"Processed batch of #{batch.size} notifications, created #{batch_created}, total: #{total_created}"
140+
)
135141
rescue StandardError => e
136142
Rails.logger.error "Failed to process notification batch: #{e.message}"
137143
Rails.logger.error "Batch size: #{batch.size}"
@@ -144,12 +150,12 @@ def bulk_import_notifications(notifications)
144150
def valid_notification_data?(notification_data)
145151
return false unless notification_data.is_a?(Hash)
146152

147-
unless notification_data['title'].present?
153+
if notification_data['title'].blank?
148154
Rails.logger.error "Failed to create notification: Validation failed: Title can't be blank"
149155
return false
150156
end
151157

152-
unless notification_data['content'].present?
158+
if notification_data['content'].blank?
153159
Rails.logger.error "Failed to create notification: Validation failed: Content can't be blank"
154160
return false
155161
end

app/services/users/import_data/points.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ def finalize
5959
preload_reference_data unless @preloaded
6060
flush_batch
6161

62-
logger.info "Points import completed. Created: #{@total_created}. Processed #{@processed_count} valid points, skipped #{@skipped_count}."
62+
logger.info(
63+
"Points import completed. Created: #{@total_created}. " \
64+
"Processed #{@processed_count} valid points, skipped #{@skipped_count}."
65+
)
6366
@total_created
6467
end
6568

@@ -98,7 +101,9 @@ def flush_batch
98101
batch_created = result&.count.to_i
99102
@total_created += batch_created
100103

101-
logger.debug "Processed batch of #{@buffer.size} points, created #{batch_created}, total created: #{@total_created}"
104+
logger.debug(
105+
"Processed batch of #{@buffer.size} points, created #{batch_created}, total created: #{@total_created}"
106+
)
102107
rescue StandardError => e
103108
logger.error "Failed to process point batch: #{e.message}"
104109
logger.error "Batch size: #{@buffer.size}"
@@ -125,7 +130,7 @@ def preload_reference_data
125130
logger.debug "Loaded #{user.imports.size} imports with #{@imports_lookup.size} lookup keys"
126131

127132
@countries_lookup = {}
128-
Country.all.each do |country|
133+
Country.all.find_each do |country|
129134
@countries_lookup[[country.name, country.iso_a2, country.iso_a3]] = country
130135
@countries_lookup[country.name] = country
131136
end
@@ -143,17 +148,19 @@ def normalize_point_keys(points)
143148
all_keys = points.flat_map(&:keys).uniq
144149

145150
points.map do |point|
146-
all_keys.each_with_object({}) do |key, normalized|
147-
normalized[key] = point[key]
151+
all_keys.index_with do |key|
152+
point[key]
148153
end
149154
end
150155
end
151156

152157
def valid_point_data?(point_data)
153158
return false unless point_data.is_a?(Hash)
154-
return false unless point_data['timestamp'].present?
159+
return false if point_data['timestamp'].blank?
155160

156-
has_lonlat = point_data['lonlat'].present? && point_data['lonlat'].is_a?(String) && point_data['lonlat'].start_with?('POINT(')
161+
has_lonlat = point_data['lonlat'].present? &&
162+
point_data['lonlat'].is_a?(String) &&
163+
point_data['lonlat'].start_with?('POINT(')
157164
has_coordinates = point_data['longitude'].present? && point_data['latitude'].present?
158165

159166
has_lonlat || has_coordinates

app/services/users/import_data/stats.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def filter_and_prepare_stats
5252
valid_stats << prepared_attributes if prepared_attributes
5353
end
5454

55-
Rails.logger.warn "Skipped #{skipped_count} stats with invalid or missing required data" if skipped_count > 0
55+
Rails.logger.warn "Skipped #{skipped_count} stats with invalid or missing required data" if skipped_count.positive?
5656

5757
valid_stats
5858
end
@@ -107,7 +107,9 @@ def bulk_import_stats(stats)
107107
batch_created = result.count
108108
total_created += batch_created
109109

110-
Rails.logger.debug "Processed batch of #{batch.size} stats, created #{batch_created}, total created: #{total_created}"
110+
Rails.logger.debug(
111+
"Processed batch of #{batch.size} stats, created #{batch_created}, total created: #{total_created}"
112+
)
111113
rescue StandardError => e
112114
ExceptionReporter.call(e, 'Failed to process stat batch')
113115
end
@@ -118,17 +120,17 @@ def bulk_import_stats(stats)
118120
def valid_stat_data?(stat_data)
119121
return false unless stat_data.is_a?(Hash)
120122

121-
unless stat_data['year'].present?
123+
if stat_data['year'].blank?
122124
Rails.logger.error "Failed to create stat: Validation failed: Year can't be blank"
123125
return false
124126
end
125127

126-
unless stat_data['month'].present?
128+
if stat_data['month'].blank?
127129
Rails.logger.error "Failed to create stat: Validation failed: Month can't be blank"
128130
return false
129131
end
130132

131-
unless stat_data['distance'].present?
133+
if stat_data['distance'].blank?
132134
Rails.logger.error "Failed to create stat: Validation failed: Distance can't be blank"
133135
return false
134136
end

app/services/users/import_data/trips.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def filter_and_prepare_trips
5252
valid_trips << prepared_attributes if prepared_attributes
5353
end
5454

55-
Rails.logger.warn "Skipped #{skipped_count} trips with invalid or missing required data" if skipped_count > 0
55+
Rails.logger.warn "Skipped #{skipped_count} trips with invalid or missing required data" if skipped_count.positive?
5656

5757
valid_trips
5858
end
@@ -119,7 +119,9 @@ def bulk_import_trips(trips)
119119
batch_created = result.count
120120
total_created += batch_created
121121

122-
Rails.logger.debug "Processed batch of #{batch.size} trips, created #{batch_created}, total created: #{total_created}"
122+
Rails.logger.debug(
123+
"Processed batch of #{batch.size} trips, created #{batch_created}, total created: #{total_created}"
124+
)
123125
rescue StandardError => e
124126
ExceptionReporter.call(e, 'Failed to process trip batch')
125127
end

0 commit comments

Comments
 (0)