I'm using a (private) gem which defines a Rails 5.2 controller. I want to override the private params permitting method in the mentioned controller to add some different params.
I tried to reference the Rails Engine Guide but it doesn't actually show how to override a controller method (only a model, and the same approach doesn't seem to work for controllers.)
Update: Here's the decorator pattern I tried based on the aforementioned Rails guide.
- Load the decorators (Is the
/libfolder even loaded by Rails anymore?):
# MyApp/lib/private_gem/engine.rb
module PrivateGem
class Engine < ::Rails::Engine
isolate_namespace PrivateGem
config.to_prepare do
Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
require_dependency(c)
end
end
end
end
- The Decorator:
# MyApp/app/decorators/controllers/private_gem/users_controller_decorator.rb
PrivateGem::UsersController.class_eval do
private
# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:existing_param, :new_param)
end
end
I have a working solution that I don't like. First, I load the existing class and then redefine the private method.
# MyApp/controllers/private_gem/users_controller.rb
load PrivateGem::Engine.root.join('app/controllers/private_gem/users_controller.rb')
PrivateGem::UsersController.class_eval do
private
# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:existing_param, :new_param)
end
end
Is this really the best way to do this? Maybe it belongs in a different place? Should I somehow use ActiveSupport::Concern? It doesn't seem like idiomatic Rails to me but I have a lot to learn about how Rails is initialized and how Engines and Concerns work.
Thanks!