Skip to content

Commit

Permalink
Revert "make whitelist thread safe"
Browse files Browse the repository at this point in the history
This reverts commit 9cda9c2.
  • Loading branch information
flyerhzm committed Dec 12, 2020
1 parent 62945b7 commit 9c728cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/bullet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class << self
:stacktrace_includes,
:stacktrace_excludes,
:skip_html_injection
attr_reader :whitelist
attr_accessor :add_footer, :orm_patches_applied

available_notifiers = UniformNotifier::AVAILABLE_NOTIFIERS.map { |notifier| "#{notifier}=" }
Expand Down Expand Up @@ -97,27 +98,27 @@ def stacktrace_excludes

def add_whitelist(options)
reset_whitelist
Thread.current[:whitelist][options[:type]][options[:class_name]] ||= []
Thread.current[:whitelist][options[:type]][options[:class_name]] << options[:association].to_sym
@whitelist[options[:type]][options[:class_name]] ||= []
@whitelist[options[:type]][options[:class_name]] << options[:association].to_sym
end

def delete_whitelist(options)
reset_whitelist
Thread.current[:whitelist][options[:type]][options[:class_name]] ||= []
Thread.current[:whitelist][options[:type]][options[:class_name]].delete(options[:association].to_sym)
Thread.current[:whitelist][options[:type]].delete_if { |_key, val| val.empty? }
@whitelist[options[:type]][options[:class_name]] ||= []
@whitelist[options[:type]][options[:class_name]].delete(options[:association].to_sym)
@whitelist[options[:type]].delete_if { |_key, val| val.empty? }
end

def get_whitelist_associations(type, class_name)
Array(Thread.current[:whitelist][type][class_name])
Array(@whitelist[type][class_name])
end

def reset_whitelist
Thread.current[:whitelist] ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} }
@whitelist ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} }
end

def clear_whitelist
Thread.current[:whitelist] = nil
@whitelist = nil
end

def bullet_logger=(active)
Expand Down
2 changes: 1 addition & 1 deletion spec/bullet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
it 'is deleted from the whitelist successfully' do
Bullet.add_whitelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
Bullet.delete_whitelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
expect(Thread.current[:whitelist][:n_plus_one_query]).to eq({})
expect(Bullet.whitelist[:n_plus_one_query]).to eq({})
end
end

Expand Down

0 comments on commit 9c728cc

Please sign in to comment.