Disable caching on specific model in active admin

Viewed 293

Basically I want to be able to have a count value in the active admin navigation bar, that updates according to a query. But it seems like the registered models are cached, and thereby also the navigation label provided by the registered model.

Is it possible to change this default caching behaviour?

ActiveAdmin.register ChangeRequest do
  config.sort_order = "created_at_desc"

  actions :all, except: [:show]

  menu label: "Change Requests (#{ChangeRequest.pending.count})"

  ....
end

If possible, the "menu label:" should be updated on every reload...

2 Answers

You can try using ajax for this purpose.

Assuming you have a model name Change Request

Add a collection action in admin/change_request controller

  collection_action :total_pending do
    render json: { total: ChangeRequest.pending.count }
  end

in your active_admin.js.coffee file add this ajax call

  $.ajax
    url: '/en/admin/change_requests/total_pending'
    success: (data) ->
      $('#change_requests a').text("Custom text #{data.total}")

NOTE: HTML element id and URL can be different as per your model & controller names

Maybe proc will work here?

menu label: proc{"Change Requests (#{ChangeRequest.pending.count})"}
Related