g_named_scope_filters¶ ↑
github.com/midas/g_named_scope_filters/tree/master
DESCRIPTION:¶ ↑
A UI component that generates an unordered list of filters that use named scopes within an ActiveRecord model to filter a list. It is not tied to a table or list specifically as it simply manipulates the url, resulting in a manipulation of any collection (list, table, etc.) it may be coupled with.
FEATURES¶ ↑
-
Generates unordered list of links that will filter based on defined named scopes in an ActiveRecord model
PLANNED FEATURES¶ ↑
-
Automatically use all named scopes defined in model if :only or :except options are not used
-
Obey the :except option if :only option not present
REQUIREMENTS:¶ ↑
-
Rails >= 2.2.0
-
Guilded >= 1.0.0 (github.com/midas/guilded/tree/master)
INSTALL:¶ ↑
Update your RubyGem sources to include gemcutter if it is not already:
gem sources -a http://gemcutter.org
Install:
sudo gem install g_named_scope_filters
In your Rails environment.rb file:
config.gem 'g_named_scope_filters', :version => '1.0.5', :source => 'http://gems.github.com'
USAGE:¶ ↑
Model:
class Item < ActiveRecord::Base named_scope :recent, lambda { { :conditions => [ "date_of_offense BETWEEN ? AND ?", 31.days.ago.utc, Time.zone.now.end_of_day.utc ] } } named_scope :inactive, :conditions => { :is_active => false } end
View:
<%= g_named_scope_filters Item, :only => [:recent, :inactive], :scoped_by => @account, :include_count => true, :id => 'item_filters' %>
You currently must use the :only option to specify which named scopes to use. It is planned to use all named scopes if :only or :except are not present, however it is not yet implemented.
Results in:
<ul id="doc-filters" class="filters"> <li> <a class="selected" href="/accounts/2/items"> All <span>2</span> </a> </li> <li> <a class="" href="/accounts/2/items?filter=recent"> Recent <span>1</span> </a> </li> <li> <a class="" href="/accounts/2/items?filter=inactive"> Inactive <span>0</span> </a> </li> </ul>
In order to override how the counts are calculated, simply pass a hash into the g_named_scope_filters :record_counts option:
<%= g_named_scope_filters Item, :only => [:recent, :inactive], :scoped_by => @account, :include_count => true, :record_counts => @record_counts, :id => 'item_filters' %>
The hash should contain symbols and integers representing the filter name and the number of available records:
{ :all => 4, :recent => 2, :inactive => 2 }
Check the documentation for the g_named_scope_filter method in ViewHelpers module for more options.
LICENSE:¶ ↑
(The MIT License)
Copyright © 2009 C. Jason Harrelson (midas)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.