Skip to content

add current_user to importer.rb#203

Closed
adrian-kinda-bot wants to merge 1 commit intoactiveadmin-plugins:masterfrom
adrian-kinda-bot:add_current_user_to_importer_object
Closed

add current_user to importer.rb#203
adrian-kinda-bot wants to merge 1 commit intoactiveadmin-plugins:masterfrom
adrian-kinda-bot:add_current_user_to_importer_object

Conversation

@adrian-kinda-bot
Copy link
Copy Markdown

this PR would allow us something like

active_admin_import(
  after_batch_import->(importer) do
    ...
    # possible some logging for current_user
    Log.create!(user: importer.current_user)
    ...
  end
)

@adrian-kinda-bot adrian-kinda-bot changed the title add_current_user_to_importer_object add current_user to importer.rb Sep 19, 2024
@Fivell
Copy link
Copy Markdown
Member

Fivell commented Apr 8, 2026

Hi @adrian-kinda-bot — thanks for the PR.

I've opened #211 which covers this use case with a more general mechanism: a convention method active_admin_import_context that the controller can define to provide extra attributes for the import model. Your example becomes:

ActiveAdmin.register Post do
  controller do
    def active_admin_import_context
      { imported_by_id: current_user.id }
    end
  end

  active_admin_import(
    template_object: ActiveAdminImport::Model.new,
    after_batch_import: ->(importer) {
      Log.create!(user_id: importer.model.imported_by_id)
    }
  )
end

Advantages over hardcoding current_user into the Importer:

  • Works with any auth setup (current_admin, current_staff, Pundit scopes, etc.) — you pick what to expose
  • Not limited to current_user; you can pass request.remote_ip, session[:account_id], parent, etc.
  • No API change on Importer#initialize
  • Context wins over form params, so values the controller is authoritative about can't be overridden by tampered fields

If that approach works for you I'll close this PR in favor of #211.

@Fivell Fivell closed this in #211 Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants