Skip to content

Instantly share code, notes, and snippets.

@tinynumbers
Last active August 15, 2024 16:11
Show Gist options
  • Select an option

  • Save tinynumbers/5896537 to your computer and use it in GitHub Desktop.

Select an option

Save tinynumbers/5896537 to your computer and use it in GitHub Desktop.

Revisions

  1. tinynumbers revised this gist Jun 30, 2013. 1 changed file with 0 additions and 19 deletions.
    19 changes: 0 additions & 19 deletions persistent ActiveAdmin index filters
    Original file line number Diff line number Diff line change
    @@ -1,19 +0,0 @@
    This works by calling a before_filter to restore any saved search
    filters, and an after_filter to save any filters from the current
    request.

    Filters are only restored if the current index page request does
    not contain a :q param (i.e., new filter values).

    There's a smelly hack in here to ajax-post a "clear filters" request
    just before the GET request attached to the filter form "clear filters"
    link. This is because the "clear filters" button just sends a request
    for the index page, without any :q parameter. So the ajax request
    clears the saved filters before this :q-less request comes through
    (otherwise the :q-less request would trigger restoring of the saved
    filters).

    In a real implementation of this, the clear-filters button would send
    a special value for :q which would cause the filters to be cleared.
    If I make a pull request for this feature, that's how it will be
    implemented.
  2. tinynumbers renamed this gist Jun 30, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. tinynumbers revised this gist Jun 30, 2013. 1 changed file with 16 additions and 4 deletions.
    20 changes: 16 additions & 4 deletions Description
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,19 @@
    This works by calling a before_filter to restore any saved search filters, and an after_filter to save any filters from the current request.
    This works by calling a before_filter to restore any saved search
    filters, and an after_filter to save any filters from the current
    request.

    Filters are only restored if the current index page request does not contain a :q param (i.e., new filter values).
    Filters are only restored if the current index page request does
    not contain a :q param (i.e., new filter values).

    There's a smelly hack in here to ajax-post a "clear filters" request just before the GET request attached to the filter form "clear filters" link. This is because the "clear filters" button just sends a request for the index page, without any :q parameter. So the ajax request clears the saved filters before this :q-less request comes through (otherwise the :q-less request would trigger restoring of the saved filters).
    There's a smelly hack in here to ajax-post a "clear filters" request
    just before the GET request attached to the filter form "clear filters"
    link. This is because the "clear filters" button just sends a request
    for the index page, without any :q parameter. So the ajax request
    clears the saved filters before this :q-less request comes through
    (otherwise the :q-less request would trigger restoring of the saved
    filters).

    In a real implementation of this, the clear-filters button would send a special value for :q which would cause the filters to be cleared. If I make a pull request for this feature, that's how it will be implemented.
    In a real implementation of this, the clear-filters button would send
    a special value for :q which would cause the filters to be cleared.
    If I make a pull request for this feature, that's how it will be
    implemented.
  4. tinynumbers revised this gist Jun 30, 2013. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Description
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    This works by calling a before_filter to restore any saved search filters, and an after_filter to save any filters from the current request.

    Filters are only restored if the current index page request does not contain a :q param (i.e., new filter values).

    There's a smelly hack in here to ajax-post a "clear filters" request just before the GET request attached to the filter form "clear filters" link. This is because the "clear filters" button just sends a request for the index page, without any :q parameter. So the ajax request clears the saved filters before this :q-less request comes through (otherwise the :q-less request would trigger restoring of the saved filters).

    In a real implementation of this, the clear-filters button would send a special value for :q which would cause the filters to be cleared. If I make a pull request for this feature, that's how it will be implemented.
  5. tinynumbers revised this gist Jun 30, 2013. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion active_admin.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,20 @@
    # put the following lines below the main ActiveAdmin.setup block in config/initializers/active_admin.rb
    # this stuff goes in config/initializers/active_admin.rb

    ActiveAdmin.setup do |config|

    # ...

    # put these lines in the "Controller Filters" section of the ActiveAdmin.setup block

    # These two are defined in ActiveAdmin::FilterSaver::Controller, which is loaded below.
    config.before_filter :restore_search_filters
    config.after_filter :save_search_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'

  6. tinynumbers revised this gist Jun 30, 2013. 3 changed files with 26 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions active_admin.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # put the following lines below the main ActiveAdmin.setup block 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
    1 change: 1 addition & 0 deletions controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # -*- encoding : utf-8 -*-
    # put this in lib/active_admin/filter_saver/controller.rb

    module ActiveAdmin
    module FilterSaver
    17 changes: 17 additions & 0 deletions index-filters.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # put this in e.g. app/assets/javascripts/admin/index-filters.coffee
    # and include this in app/assets/javascripts/active_admin.js

    $ ->

    # 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: 'POST'
    }
  7. tinynumbers created this gist Jun 30, 2013.
    54 changes: 54 additions & 0 deletions controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    # -*- encoding : utf-8 -*-

    module ActiveAdmin
    module FilterSaver

    # Extends the ActiveAdmin controller to persist resource index filters between requests.
    #
    # @author David Daniell / тιηуηυмвєяѕ <info@tinynumbers.com>
    module Controller

    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.post?
    # 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 }
    end
    elsif filter_storage && params[:action].to_sym == :index && params[:q].blank?
    saved_filters = filter_storage[controller_key]
    unless saved_filters.blank?
    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