Skip to content

Commit

Permalink
Introduce BulletmarkRepairer::Thread to avoid to handle Thread directly
Browse files Browse the repository at this point in the history
  • Loading branch information
makicamel committed Nov 8, 2023
1 parent bb7af64 commit 51f051e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/bulletmark_repairer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'bulletmark_repairer/loaded_associations'
require 'bulletmark_repairer/markers'
require 'bulletmark_repairer/patcher'
require 'bulletmark_repairer/thread'

module BulletmarkRepairer
class Error < StandardError; end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ module BulletmarkRepairer
module ActiveRecord
module QueryMethod
def includes(*args)
Thread.current[:bulletmark_repaier_loaded_associations][model.name][:includes].add(args)
BulletmarkRepairer::Thread.add(name: model.name, method_type: :includes, args: args)
super(args)
end

def eager_load(*args)
Thread.current[:bulletmark_repaier_loaded_associations][model.name][:eager_load].add(args)
BulletmarkRepairer::Thread.add(name: model.name, method_type: :eager_load, args: args)
super(args)
end

def preload(*args)
Thread.current[:bulletmark_repaier_loaded_associations][model.name][:preload].add(args)
BulletmarkRepairer::Thread.add(name: model.name, method_type: :preload, args: args)
super(args)
end
end
Expand Down
11 changes: 4 additions & 7 deletions lib/bulletmark_repairer/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@ def initialize(app)
end

def call(env)
Thread.current[:bulletmark_repaier_loaded_associations] = Hash.new do |hash, key|
hash[key] = { includes: Set.new, eager_load: Set.new, preload: Set.new }
end
@app.call(env)
ensure
begin
if Thread.current[:bullet_notification_collector].notifications_present?
if ::Thread.current[:bullet_notification_collector].notifications_present?
BulletmarkRepairer::Patcher.execute(
notifications: Thread.current[:bullet_notification_collector],
notifications: ::Thread.current[:bullet_notification_collector],
controller: env['action_dispatch.request.parameters']['controller'],
action: env['action_dispatch.request.parameters']['action'],
loaded_associations: Thread.current[:bulletmark_repaier_loaded_associations]
loaded_associations: BulletmarkRepairer::Thread.current
)
end
rescue StandardError => e
raise e if BulletmarkRepairer.config.debug?
end
Thread.current[:bulletmark_repaier_loaded_associations].clear
BulletmarkRepairer::Thread.clear
end
end
end
30 changes: 30 additions & 0 deletions lib/bulletmark_repairer/thread.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'rails'

module BulletmarkRepairer
class Thread
class << self
def current
touch
end

def add(name:, method_type:, args:)
touch
::Thread.current[:bulletmark_repaier_loaded_associations][name][method_type].add(args)
end

def clear
::Thread.current[:bulletmark_repaier_loaded_associations] = nil
end

private

def touch
::Thread.current[:bulletmark_repaier_loaded_associations] ||= Hash.new do |hash, key|
hash[key] = { includes: Set.new, eager_load: Set.new, preload: Set.new }
end
end
end
end
end

0 comments on commit 51f051e

Please sign in to comment.