diff --git a/lib/administrate_exportable/exporter.rb b/lib/administrate_exportable/exporter.rb index e8571c4..e023965 100644 --- a/lib/administrate_exportable/exporter.rb +++ b/lib/administrate_exportable/exporter.rb @@ -10,9 +10,12 @@ module Exporter def exportable define_method(:export) do search_term = params[:search].to_s.strip - resources = Administrate::Search.new(scoped_resource, - dashboard, - search_term).run + resources = if respond_to?(:filter_resources, true) + filter_resources(scoped_resource, search_term: search_term) + else + # Administrate < 0.18 has no filter_resources hook + Administrate::Search.new(scoped_resource, dashboard, search_term).run + end resources = apply_collection_includes(resources) resources = order.apply(resources) diff --git a/spec/administrate_exportable/exporter_concern_spec.rb b/spec/administrate_exportable/exporter_concern_spec.rb index f7918f3..ef091ed 100644 --- a/spec/administrate_exportable/exporter_concern_spec.rb +++ b/spec/administrate_exportable/exporter_concern_spec.rb @@ -26,6 +26,20 @@ expect(rows[1][1]).to eq "Jane" end + it "uses the controller's filter_resources to apply custom search" do + User.create(first_name: "John", last_name: "Doe", email: "john@email.com") + User.create(first_name: "Jane", last_name: "Roe", email: "jane@email.com") + allow_any_instance_of(Admin::UsersController).to receive(:filter_resources) + .with(anything, search_term: "custom") + .and_return(User.where(last_name: "Roe")) + + get export_admin_users_url(format: :csv, search: "custom") + + rows = CSV.parse(response.body) + expect(rows.size).to eq 2 + expect(rows[1][2]).to eq "Roe" + end + it "exports only headers when the search has no results" do get export_admin_users_url(format: :csv, search: "nobody")