Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveAdmin Access filtered collection

I'm trying to create a collection_action where I'm going to do something with the entire collection of filtered items. My problem is that from within the collection_action I don't seem to have access to the filtered collection. When I access collection it is only the items that are on the first page of records. In my action_item I have access to collection_before_scope which is exactly the filtered record I want, but this is empty when I try to access it from within my collection_action.

Below is my current setup attempting to find the correct collection.

collection_action :dosmth, :method => :get do
  # collection_before_scope will be empty
  puts "collection_before_scope = " + collection_before_scope.to_yaml

  # collection will return only the filtered items on the front page
  puts "collection = " + collection.to_yaml

  redirect_to :back, notice: 'Something happening'
end

action_item :only => :index do
  # collection_before_scope will return the full collection that I'm looking for.
  puts "collection_before_scope = " + collection_before_scope.to_yaml 

  link_to "Export", dosmth_admin_earned_points_path(controller.params)
end

The closest related question I could find was this, ActiveAdmin Collection action on filtered data, which didn't seem to help me out.

Any help would be greatly appreciated.

Thank you,

Update:

I still have the same problem, but I have figured something out. If I try to access the collection before the collection_before_scope then the correct filtered items are in collection_before_scope. I don't want to have to access the collection just to get the correct collection_before_scope though. Not sure why this would happen.

collection_action :dosmth, :method => :get d0
  # collection will return only the filtered items on the front page
  puts "collection = " + collection.to_yaml

  # collection_before_scope will be correct because I accessed the collection first.  why???
  puts "collection_before_scope = " + collection_before_scope.to_yaml

  redirect_to :back, notice: 'Something happening'
end
like image 637
KevinM Avatar asked Feb 11 '23 21:02

KevinM


2 Answers

Try this:

puts "filtered collection = " + apply_filtering(collection).to_yaml (before you call collection)

Why do you reach the correct filtered collection after you accessed the collection first?

The collection method will invoke the find_collection method: https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L32

The find_collection method will invoke the apply_filter method: https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L50

And once the collection method has been called: https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_controller/data_access.rb#L22-L27

like image 165
nistvan Avatar answered Feb 15 '23 10:02

nistvan


I know this is old, but I just ran into this issue when trying to access the filtered collection for a custom CSV download.

Because ActiveAdmin uses Ransack for search, you can grab the filtered collection using their params.

ModelName.ransack(params[:q]).result worked for me.

like image 36
edatrix Avatar answered Feb 15 '23 10:02

edatrix