-
👋 I've been running into this odd issue that I can't figure out. I came up with a work-around but I'm wondering if there's a better way. We're migrating to Avo from another admin dashboard. One capability that we had added in the past is to allow admin users to impersonate others. We use the Pretender gem for this. Playing around with Avo, it seemed like defining an Action would be the move here, but that's when I started experiencing issues with accessing methods defined in the base controller within Actions. A simple example is as follows: # Gemfile
gem "pretender", "~> 0.6.0"
# app/controllers/concerns/admin/impersonation.rb
module Admin::Impersonation
extend ActiveSupport::Concern
included do
impersonates :user
end
end
# config/initializers/avo.rb
Rails.configuration.to_prepare do
Avo::ApplicationController.include Admin::Impersonation
end
# app/avo/actions/impersonate_user.rb
class Avo::Actions::ImpersonateUser < Avo::BaseAction
self.name = "Impersonate User"
def handle(query:, fields:, current_user:, resource:, **args)
# This fails with NoMethodError
impersonate_user(resource.record)
succeed "Impersonating #{resource.record.name}"
end
end The error I get is:
After debugging, I discovered that Avo actions don't have access to controller-specific methods defined through these concerns. My workaround worked out since the pretender gem is simply setting a key on the session: class Avo::Actions::ImpersonateUser < Avo::BaseAction
self.name = "Impersonate User"
def handle(query:, fields:, current_user:, resource:, **args)
impersonate_user(resource.record)
succeed "Now impersonating #{resource.record.name}"
end
private
def impersonate_user(user)
Avo::Current.request.session[:pretender_user_id] = user.id
end
end My question here is: Am I missing something with the way actions are architected? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @its-fern, thank you for the detailed explanation.
No, you interpreted the actions architecture correctly. The We received feedback about the need for the Your workaround works and seems legit. I'd take a different approach that I consider more time-proof, since your workaround dips into session internals a bit. Here's how I set up
Final result: https://audit-logging.avodemo.com/avo/resources/users I implemented this a while ago, so it's possible I missed a step. Let me know if it doesn't work for you right away. I also added a guide idea for "how to use Pretender" to our guide ideas list here: avo-hq/docs.avohq.io#373.
I'm curious, how it's going beside this? Any rough edges or surprises so far? |
Beta Was this translation helpful? Give feedback.
Hi @its-fern, thank you for the detailed explanation.
No, you interpreted the actions architecture correctly. The
handle
method is heavily based on a pre-built DSL and doesn't run in the controller's context. This was an early architectural decision.We received feedback about the need for the
handle
method to run within the controller context. That's why, while working on the Avo 4 forms feature, we made sure thehandle
method there is executed in the controller context. We haven't built much DSL around it yet, since at the controller level, each developer has full control over business logic and the resp…