Last active
August 15, 2024 16:11
-
-
Save tinynumbers/5896537 to your computer and use it in GitHub Desktop.
Revisions
-
tinynumbers revised this gist
Jun 30, 2013 . 1 changed file with 0 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,19 +0,0 @@ -
tinynumbers renamed this gist
Jun 30, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tinynumbers revised this gist
Jun 30, 2013 . 1 changed file with 16 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. 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. -
tinynumbers revised this gist
Jun 30, 2013 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
tinynumbers revised this gist
Jun 30, 2013 . 1 changed file with 17 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,20 @@ # 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' -
tinynumbers revised this gist
Jun 30, 2013 . 3 changed files with 26 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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' } -
tinynumbers created this gist
Jun 30, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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