Skip to content

Commit 3a7a627

Browse files
authored
Merge pull request #72 from Angelmmiguel/71-controller-thread-safe
Make ActionController::Caching::Sweeper class thread safe
2 parents fb6b955 + 281f63e commit 3a7a627

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in active_record-observers.gemspec
44
gemspec
5+
gem "minitest", "~> 5.8.4"
56
gem 'rails', github: 'rails/rails', branch: '5-1-stable'
67
gem 'activeresource', github: 'rails/activeresource'
78

gemfiles/rails_4_2.gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
source "https://rubygems.org"
22

33
gem "mocha", require: false
4+
gem "minitest", "~> 5.8.4"
45
gem "rails", "~> 4.2.0"
56

67
gemspec path: "../"

gemfiles/rails_5_0.gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ source "https://rubygems.org"
22

33
# Specify your gem"s dependencies in active_record-observers.gemspec
44
gem "rails", github: "rails/rails", branch: "5-0-stable"
5+
gem "minitest", "~> 5.8.4"
56
gem "activeresource", github: "rails/activeresource"
67

78
gem "mocha", require: false

gemfiles/rails_master.gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ source "https://rubygems.org"
22

33
# Specify your gem"s dependencies in active_record-observers.gemspec
44
gem "rails", github: "rails/rails"
5+
gem "minitest", "~> 5.8.4"
56
gem "activeresource", github: "rails/activeresource"
67

78
gem "mocha", require: false

lib/rails/observers/action_controller/caching/sweeper.rb

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
module ActionController #:nodoc:
22
module Caching
33
class Sweeper < ActiveRecord::Observer #:nodoc:
4-
attr_accessor :controller
5-
64
def initialize(*args)
75
super
8-
@controller = nil
6+
self.controller = nil
7+
end
8+
9+
def controller
10+
Thread.current["observer:#{self.class.name}_controller"]
11+
end
12+
13+
def controller=(controller)
14+
Thread.current["observer:#{self.class.name}_controller"] = controller
915
end
1016

1117
def before(controller)
@@ -53,8 +59,12 @@ def callback(timing)
5359
end
5460

5561
def method_missing(method, *arguments, &block)
56-
return super unless @controller
57-
@controller.__send__(method, *arguments, &block)
62+
return super if controller.nil?
63+
controller.__send__(method, *arguments, &block)
64+
end
65+
66+
def respond_to_missing?(method, include_private = false)
67+
(controller.present? && controller.respond_to?(method)) || super
5868
end
5969
end
6070
end

test/helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'minitest/autorun'
2-
require 'mocha/mini_test'
2+
require 'mocha/minitest'
33
require 'active_record'
44
require 'rails'
55
require 'rails/observers/activerecord/active_record'

0 commit comments

Comments
 (0)