Skip to content

Commit a247145

Browse files
tricknotesclaude
andcommitted
Move GitHub fetch from controllers to rake task
- Remove StarEvent.fetch_and_upsert calls from controllers (activities, dashboard, stars) - controllers now read from DB only - Remove User#fetch_star_events (no longer needed) - Add lib/tasks/fetch_star_events.rake for periodic background fetch - Remove fetch_and_upsert stub from rails_helper (no longer needed) - Skip Settings.url_options in test env to avoid BASE_URL port leaking into action_mailer.default_url_options Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 301e15d commit a247145

7 files changed

Lines changed: 19 additions & 10 deletions

File tree

app/controllers/activities_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class ActivitiesController < ApplicationController
44

55
def starring
66
@user = current_user
7-
@user.fetch_star_events(since: 1.day.ago)
87
@star_events = @user.star_events_by_followings_with_me.latest(1.day.ago)
98
end
109

app/controllers/dashboard_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ class DashboardController < ApplicationController
22
before_action :require_login, :assign_current_user
33

44
def show
5-
StarEvent.fetch_and_upsert(client: @user.github_client, logins: [@user.username], since: 7.days.ago)
65
@star_events = StarEvent.by(@user.username).latest(7.days.ago).newly
76
@starred_events = StarEvent.owner(@user.username).latest(7.days.ago).newly
87
end

app/controllers/stars_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class StarsController < ApplicationController
55

66
def index
77
@user = User.find_or_fetch_by_username(params[:username])
8-
StarEvent.fetch_and_upsert(client: Settings.github_client, logins: [@user.username], since: 7.days.ago)
98
@star_events = StarEvent.by(@user.username).latest(7.days.ago).newly
109
end
1110
end

app/models/user.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ def star_events_by_followings_with_me
6161
StarEvent.all_by(following_names + [username])
6262
end
6363

64-
def fetch_star_events(since:)
65-
following_names = followings.map { |following| following['login'] }
66-
StarEvent.fetch_and_upsert(client: github_client, logins: following_names + [username], since: since)
67-
end
68-
6964
def followings
7065
return @followings if @followings
7166

config/application.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class Application < Rails::Application
4040
config.generators.system_tests = nil
4141

4242
config.after_initialize do
43-
Rails.application.default_url_options = Settings.url_options
43+
unless Rails.env.test?
44+
Rails.application.default_url_options = Settings.url_options
45+
end
4446
end
4547
end
4648
end

lib/tasks/fetch_star_events.rake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace :star_events do
2+
desc 'Fetch star events for all users and their followings from GitHub'
3+
task fetch: :environment do
4+
since = 7.days.ago
5+
6+
User.find_each do |user|
7+
following_names = user.followings.map { |following| following['login'] }
8+
logins = (following_names + [user.username]).uniq
9+
10+
Rails.logger.info "Fetching star events for #{user.username} (and #{following_names.size} followings)"
11+
StarEvent.fetch_and_upsert(client: user.github_client, logins: logins, since: since)
12+
rescue => e
13+
Rails.logger.error "Failed to fetch star events for #{user.username}: #{e.message}"
14+
end
15+
end
16+
end

spec/rails_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
end
2222

2323
config.before :each do
24-
allow(StarEvent).to receive(:fetch_and_upsert)
2524
clear_mail_box
2625
end
2726

0 commit comments

Comments
 (0)