class PostsController < ApplicationController
around_action :set_reading_role, only: %i[index show]
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
@post.touch
end
private
def set_reading_role
ActiveRecord::Base.connected_to(role: :reading) do
yield
end
end
end
class PostsController < ApplicationController
set_default_connect_role :reading, only: %i[index show]
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
@post.touch
end
end
class ApplicationController < ActionController::Base
include DefaultConnectRole
end
module DefaultConnectRole
extend ActiveSupport::Concern
class_methods do
def set_default_connect_role(role, only: nil, except: nil)
if only.nil? && except.nil?
around_action wrap_connected_to(role)
elsif only.present?
around_action wrap_connected_to(role), only: only
elsif except.present?
around_action wrap_connected_to(role), except: except
else
raise ArgumentError.new("need any options")
end
end
private
def wrap_connected_to(role)
lambda do |controller, block|
with_connected_to(role, &block)
end
end
end
private
def with_connected_to(role, &block)
ActiveRecord::Base.connected_to(role: role) do
yield
end
end
end