diff --git a/lib/ready_set/controller_extension.rb b/lib/ready_set/controller_extension.rb index d5a01c1..d15f984 100644 --- a/lib/ready_set/controller_extension.rb +++ b/lib/ready_set/controller_extension.rb @@ -26,11 +26,8 @@ module ControllerExtension def self.route_to_readyset(*actions, **options, &block) # Use double splat (**) to pass options as keyword arguments around_action(*actions, **options) do |_controller, action_block| - # TODO: Decouple the role symbol, have it pull from a dev-configurable location. - ActiveRecord::Base.connected_to(role: :replica_db_role) do - # Functionally the same as yield, except we're highlighting - # that it is action_block being called/yielded. - action_block.call # All queries will connect to the replica + Readyset.route do + action_block.call end end end diff --git a/spec/controller_extension_spec.rb b/spec/controller_extension_spec.rb index 90a59f4..ad2a062 100644 --- a/spec/controller_extension_spec.rb +++ b/spec/controller_extension_spec.rb @@ -39,6 +39,8 @@ def create allow(Post).to receive(:where).and_return([]) allow(Post).to receive(:find).and_return(nil) allow(Post).to receive(:create) + + allow(Readyset).to receive(:route).and_yield end describe '#route_to_readyset' do @@ -50,7 +52,7 @@ def create it 'routes queries to the replica database' do # Make sure it's working within the replica "context" # and it is executing the queries via yield - expect(ActiveRecord::Base).to receive(:connected_to).with(role: :replica_db_role).and_yield + expect(Readyset).to receive(:route).and_yield get :index end end @@ -58,7 +60,7 @@ def create # Check if the options are passing in correctly context 'when accessing the show action with :only option' do it 'routes queries to the replica database' do - expect(ActiveRecord::Base).to receive(:connected_to).with(role: :replica_db_role).and_yield + expect(Readyset).to receive(:route).and_yield get :show, params: { id: 1 } end end @@ -66,7 +68,7 @@ def create # Ensure that non-specified actions aren't getting re-routed context 'when accessing an action not included in :only' do it 'does not route queries to the replica database' do - expect(ActiveRecord::Base).not_to receive(:connected_to).with(role: :replica_db_role) + expect(Readyset).not_to receive(:route) post :create, params: { post: { title: 'New Post' } } end end