Skip to content

Instantly share code, notes, and snippets.

@Fivell
Forked from tinynumbers/active_admin.rb
Last active August 29, 2015 14:12
Show Gist options
  • Save Fivell/111a0034835012ad92fe to your computer and use it in GitHub Desktop.
Save Fivell/111a0034835012ad92fe to your computer and use it in GitHub Desktop.
# this stuff goes in config/initializers/active_admin.rb
ActiveAdmin.setup do |config|
config.after_filter do
save_search_filters if respond_to?(:save_filters?) and save_filters?
end
config.before_filter :only => [:index, :show, :edit, :new, :create] do
restore_search_filters if respond_to?(:save_filters?) and save_filters?
end
# put the following lines below the main ActiveAdmin.setup block (also in config/initializers/active_admin.rb)
require 'active_admin/filter_saver/controller'
ActiveAdmin.before_load do |app|
# Add our Extensions
ActiveAdmin::BaseController.send :include, ActiveAdmin::FilterSaver::Controller
end
# -*- encoding : utf-8 -*-
# put this in lib/active_admin/filter_saver/controller.rb
module ActiveAdmin
module FilterSaver
# Extends the ActiveAdmin controller to persist resource index filters between requests.
#
# @author David Daniell / тιηуηυмвєяѕ <[email protected]>
module Controller
def save_filters?
false
end
private
SAVED_FILTER_KEY = :last_search_filter
def restore_search_filters
filter_storage = session[SAVED_FILTER_KEY]
if params[:clear_filters].present?
params.delete :clear_filters
if filter_storage
logger.info{"clearing filter storage for #{controller_key}" }
filter_storage.delete controller_key
end
if request.xhr?
# we were requested via an ajax post from our custom JS
# this render will abort the request, which is ok, since a GET request will immediately follow
render json: { filters_cleared: true } and return
end
elsif filter_storage && params[:action].to_sym == :index && params[:q].blank? && params[:commit].blank?
saved_filters = filter_storage[controller_key]
unless saved_filters.blank?
flash.now[:notice] = 'Filters were restored to previous values'
params[:q] = saved_filters
end
end
end
def save_search_filters
if params[:action].to_sym == :index
session[SAVED_FILTER_KEY] ||= Hash.new
session[SAVED_FILTER_KEY][controller_key] = params[:q]
end
end
# Get a symbol for keying the current controller in the saved-filter session storage.
def controller_key
#params[:controller].gsub(/\//, '_').to_sym
current_path = request.env['PATH_INFO']
current_route = Rails.application.routes.recognize_path(current_path)
current_route.sort.flatten.join('-').gsub(/\//, '_').to_sym
end
end
end
end
ActiveAdmin.before_load do |app|
# Add our Extensions
ActiveAdmin::BaseController.send :include, ActiveAdmin::FilterSaver::Controller
end
$ ->
# Extend the clear-filters button to clear saved filters
$('.clear_filters_btn').click (evt) ->
# This will send a synchronous post with clear_filters set to true -
# our AA FilterSaver controller extension looks for this parameter to
# know when to clear session-stored filters for a resource - and then
# the default AA clear-filters button behavior will issue a get request
# to actually re-render the page.
$.ajax this.href, {
async: false,
data: { clear_filters: true },
type: 'GET'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment