Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin Batch Action Dynamic Form

I am using rails 4 with ActiveAdmin. I created a batch action using a custom form to create a Task with a Build number for every selected Device. Here's what my code looks like:

ActiveAdmin.register Device do

  def get_builds
    builds = []
    Build.all.each do |build|
      builds << [
        "[#{build.resource} - #{build.version}] #{build.file_name}",
        build.id
      ]
    end

    return builds
  end

  batch_action :create_task, form: {
    build: get_builds()
  } do |ids, inputs|

    build = Build.find(inputs[:build])

    Device.where(id: ids).each do |device|
      Task.create({
        device: device,
        build: build
      })
    end

    redirect_to admin_tasks_path
  end

end

My problem is that the list of build in the batch action's form is not refreshing. When I start my app it does have a list of all the available builds but if I add or remove a build, the build list won't be refreshed.

It is of course because the form parameter evaluate my function only once but I can't find any documentation about having a "dynamic" form.

like image 691
Gab Avatar asked Mar 15 '23 18:03

Gab


1 Answers

ActiveAdmin is caching the class in memory on load, so the builds only get calculated once. To re-calculate on each load, pass a lambda as the value of form, e.g:

form_lambda = lambda do
  builds = Build.all.map do |build|
    ["#{ build.resource } - #{ build.version } #{ build.file_name }", build.id]
  end

  { build: builds }
end

batch_action :create_task, form: form_lambda do
  # ...
end
like image 111
ahmacleod Avatar answered Mar 23 '23 03:03

ahmacleod