Skip to content

Commit f0160ad

Browse files
committed
fix: import時に新規・変更がある場合のみ保存し、件数を集計して出力するよう修正
1 parent f0230ee commit f0160ad

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/tasks/import_news.rake

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,31 @@ namespace :news do
88

99
# entries を計算
1010
entries = raw['news'] || []
11+
new_count = 0
12+
updated_count = 0
1113

1214
entries.each do |attrs|
1315
news = News.find_or_initialize_by(url: attrs['url'])
16+
is_new = news.new_record?
17+
1418
news.assign_attributes(
1519
title: attrs['title'],
1620
published_at: attrs['published_at']
1721
)
18-
news.save!
19-
puts "[news] #{news.published_at.to_date} #{news.title}"
22+
23+
if is_new || news.changed?
24+
news.save!
25+
if is_new
26+
new_count += 1
27+
status = 'new'
28+
else
29+
updated_count += 1
30+
status = 'updated'
31+
end
32+
puts "[News] #{news.published_at.to_date} #{news.title} (#{status})"
33+
end
2034
end
2135

22-
puts "Imported #{entries.size} items."
36+
puts "Imported #{new_count + updated_count} items (#{new_count} new, #{updated_count} updated)."
2337
end
2438
end

0 commit comments

Comments
 (0)