/active_scaffold_list_filters

A GitHub version of AS List Filters (see homepage URL) that works with the official Rails 2.1 AS project that can be found on GitHub

Primary LanguageRubyMIT LicenseMIT

=ActiveScaffoldListFilters=
----
 Copyright (c) 2008 Tys von Gaza (tys@gotoybox.com)
 
 MIT License use as you wish, see MIT-LICENSE file
 
 Version 0.31

[http://activescaffoldlistfilters.googlecode.com/files/Picture%201.png 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: http://code.google.com/p/activescaffoldexport/issues/detail?id=4
  * 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 (http://en.wikipedia.org/wiki/Haversine_formula)
  * iTunes style browser (update categories as you filter)
  * Dates (On a year, month, day or between two dates)

==Installing:==

  # Copy into plugin directory
  # Add to your application.rb default action list or to an individual controller:
    app/controllers/application.rb
    {{{
class ApplicationController < ActionController::Base
  ActiveScaffold.set_defaults do |config|
    config.actions.add :list_filter
  end
end
    }}}
    or an individual controller
    {{{
active_scaffold "Model" do |config|
  config.actions.add :list_filter
end
    }}}
  # 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 ] })
end
  }}}

----

==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:
active_scaffold_list_filters/default/views/_association.rhtml
  }}}
  # 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:
    {{{
list_filter_input_name(filter)[]
    }}}
  # 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]
	end
end
    }}}
    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!