Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MasterFilter instance mode and tests

  • Loading branch information...
commit 0385113df86bc22f40ab5d545dc4232e1e3196fa 1 parent a53ecbc
Mislav Marohnić authored
28 lib/active_reload/master_filter.rb
... ... @@ -1,12 +1,32 @@
1 1 module ActiveReload
2   - # MasterFilter should be used as an around filter in your controllers that require certain actions to use the Master DB for reads as well as writes
  2 + # MasterFilter is an around filter for controllers that require certain actions
  3 + # to use the master database for both reads and writes
  4 + #
  5 + # Usage:
  6 + # # class level:
  7 + # around_filter ActiveReload::MasterFilter
  8 + #
  9 + # # instance level:
  10 + # around_filter ActiveReload::MasterFilter.new(MyModel)
3 11 class MasterFilter
4 12 def self.filter(controller, &block)
5   - if ActiveRecord::Base.connection.respond_to?(:with_master)
6   - ActiveRecord::Base.connection.with_master(&block)
  13 + with_master(&block)
  14 + end
  15 +
  16 + def self.with_master(klass = ActiveRecord::Base)
  17 + if klass.connection.masochistic?
  18 + klass.connection.with_master { yield }
7 19 else
8   - yield block
  20 + yield
9 21 end
10 22 end
  23 +
  24 + def initialize(klass)
  25 + @klass = klass
  26 + end
  27 +
  28 + def filter(controller, &block)
  29 + self.class.with_master(@klass, &block)
  30 + end
11 31 end
12 32 end
55 test/connection_proxy_test.rb
@@ -4,6 +4,7 @@
4 4 require 'active_support/test_case'
5 5 require 'active_record'
6 6 require 'active_reload/connection_proxy'
  7 +require 'active_reload/master_filter'
7 8
8 9 RAILS_ENV = 'test'
9 10
@@ -154,4 +155,58 @@ def test_transactions_run_on_master
154 155 end
155 156 assert_equal :slave, connection.current_type
156 157 end
  158 +end
  159 +
  160 +class MasterFilterTest < MasochismTestCase
  161 + setup :prepare
  162 +
  163 + def prepare
  164 + enable_masochism
  165 + @controller = mock
  166 + end
  167 +
  168 + def test_yields_in_master_block
  169 + yielded = false
  170 + ActiveReload::MasterFilter.filter(@controller) do
  171 + assert_equal :master, connection.current_type
  172 + yielded = true
  173 + end
  174 + assert yielded
  175 + end
  176 +
  177 + def test_doesnt_yield_in_master_without_masochism
  178 + connection.expects(:masochistic?).returns(false)
  179 +
  180 + yielded = false
  181 + ActiveReload::MasterFilter.filter(@controller) do
  182 + assert_equal :slave, connection.current_type
  183 + yielded = true
  184 + end
  185 + assert yielded
  186 + end
  187 +
  188 + def test_instance_mode
  189 + model = mock
  190 + model.stubs(:connection).returns(connection)
  191 +
  192 + yielded = false
  193 + ActiveReload::MasterFilter.new(model).filter(@controller) do
  194 + assert_equal :master, connection.current_type
  195 + yielded = true
  196 + end
  197 + assert yielded
  198 + end
  199 +
  200 + def test_instance_mode_without_masochism
  201 + model = mock
  202 + conn = mock
  203 + model.stubs(:connection).returns(conn)
  204 + conn.expects(:masochistic?).returns(false)
  205 +
  206 + yielded = false
  207 + ActiveReload::MasterFilter.new(model).filter(@controller) do
  208 + yielded = true
  209 + end
  210 + assert yielded
  211 + end
157 212 end

0 comments on commit 0385113

Please sign in to comment.
Something went wrong with that request. Please try again.