app/controllers/madmin/resource_controller.rb in madmin-1.2.0 vs app/controllers/madmin/resource_controller.rb in madmin-1.2.1

- old
+ new

@@ -2,12 +2,25 @@ class ResourceController < ApplicationController include SortHelper before_action :set_record, except: [:index, :new, :create] + # Assign current_user for paper_trail gem + before_action :set_paper_trail_whodunnit, if: -> { respond_to?(:set_paper_trail_whodunnit, true) } + def index @pagy, @records = pagy(scoped_resources) + + respond_to do |format| + format.html + format.json { + render json: @records.map { |r| {name: @resource.display_name(r), id: r.id} } + } + end + rescue Pagy::OverflowError + params[:page] = 1 + retry end def show end @@ -54,11 +67,13 @@ def resource_name "#{controller_path.singularize}_resource".delete_prefix("madmin/").classify end def scoped_resources - resource.model.send(valid_scope).order(sort_column => sort_direction) + resources = resource.model.send(valid_scope) + resources = Madmin::Search.new(resources, resource, search_term).run + resources.reorder(sort_column => sort_direction) end def valid_scope scope = params.fetch(:scope, "all") resource.scopes.include?(scope.to_sym) ? scope : :all @@ -76,8 +91,12 @@ if data[:type] == "polymorphic" GlobalID::Locator.locate(data[:value]) else raise "Unrecognised param data: #{data.inspect}" end + end + + def search_term + @search_term ||= params[:q].to_s.strip end end end