Skip to content

Commit

Permalink
refactor: accounted for Readyset.route
Browse files Browse the repository at this point in the history
This method will handle the actual routing for us. So we'll mock it
assuming it'll be implemented later.
  • Loading branch information
helpotters committed Dec 3, 2023
1 parent a867539 commit 4dc5493
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 2 additions & 5 deletions lib/ready_set/controller_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions spec/controller_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,23 +52,23 @@ 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

# 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

# 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
Expand Down

0 comments on commit 4dc5493

Please sign in to comment.