Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
90 lines (78 sloc) 4.38 KB
Copyright (c) 2008 Tys von Gaza (
MIT License use as you wish, see MIT-LICENSE file
Version 0.31
[ Preview]
Add a filter menu at the top of the ActiveScaffold List view. The filter can be whatever you dream up. Code inspiration: activescaffoldexport plugin.
==Todo/Know Bugs (Please Help!):==
* If you have activescaffoldexport installed, please see this kb issue:
* Have the reset link not close the filter, but actually reset it.
* Ability to save filters (and table orderings!) to the database, this will be a separate plugin.
* Association filter may not work with has_and_belongs_to_many associations.
* Tests need to be created! I don't have much experience here, help or advice welcome.
* Ability to get a count of rows in the _current_ list for each association in the association filter. Ie color the options different if there is results or not. Tricky, need to update the filter along with the list. Thoughts?
* Doesn't javascript degrade... not an issue in our project so won't fix.
==Ideas for filters:==
* Map, 100km's from point (
* iTunes style browser (update categories as you filter)
* Dates (On a year, month, day or between two dates)
# Copy into plugin directory
# Add to your application.rb default action list or to an individual controller:
class ApplicationController < ActionController::Base
ActiveScaffold.set_defaults do |config|
config.actions.add :list_filter
or an individual controller
active_scaffold "Model" do |config|
config.actions.add :list_filter
# Add a filter
The form is:
* config.list_filter.add(:filter_type, :filter_name, {:label => "Filter Label", :other => "options"})
* :filter_type is the type of filter
* :filter_name is the name you want to use for the filter, should be unique in your scaffold
* {} is the options hash, :label is on the base filters, but you can set whatever you like to be used by your filter
ie: Add an Association (checkboxes) filter, named district, with the label Districts and method chain of model.unit.district:
active_scaffold "Model" do |config|
config.list_filter.add(:association, :district, {:label => "District", :association => [ :unit, :district ] })
==Creating your own filters:==
It is simple to create your own filter.
# Choose a name to use, ie "association".
# Create a new view:
in active_scaffold_list_filters/frontends/default/views/, ie:
# Use this to create a way for your user to interact with your filter. You have access to a ListFilter object (see lib/data_structures/list_filter.rb), which you'll later extend with your own methods. This object allows you get your filters type, name, and options set in the controller.
Your filter view must set form fields. Use the list_filter_input_name(filter) to get a base name to use. If you're setting more then one field then use form array's, ie: list_filter_input_name(filter)['field2'], or if you're using checkboxes or multi select menu's you can use an array like so:
# Create a new List Filter in active_scaffold_list_filters/list_filters, ie: active_scaffold_list_filters/list_filters/association.rb
This file must have the following layout:
class Association < ActiveScaffold::DataStructures::ListFilter
# Return a list of conditions based on the params
def conditions(params)
association = association_tree[association_tree.size - 1]
column = [association.active_record.table_name, association.primary_key_name].join('.')
return ["#{column} IN (?)", params]
You must then define the method conditions(params) to return a list of filter conditions. These will be used to filter your table's data.
You have access to your the ListFilter that you're extending again with the same properties as above. You also have a list of parameters from your view.
This file can also contain custom methods used in your view or conditions, sky is the limit!
# Upload your filter object and view for others to use and improve!