0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Add filtering to lists, tables, etc.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

Runtime

>= 3.1
 Project Readme

with_filters

Add filtering to tables, lists, etc.

This project follows Semantic Versioning.

Installation

$ gem install with_filters

If you're using Bundler, add this to your Gemfile:

gem 'with_filters', '~> 0.1.0'

Support

Ruby 1.9
Rails 3.1
Database Framework ActiveRecord

Usage

In your controller:

@data = Data.with_filters(params, fields: {
  email_address: {match: :contains},
  full_name:     ->(value, scope) {
    first_word, second_word = value.strip.split(/\s+/)

    if second_word
      scope.where(['first_name LIKE ? OR last_name LIKE ?', first_word, first_word])
    else
      scope.where(['first_name LIKE ? AND last_name LIKE ?', first_word, second_word])
    end
  }
}).order('created_at DESC').page(params[:page] || 1)

In your view:

<%= filter_form_for(@data) do |f| %>
  <%# pass through variables needed in the form but not associated with filtering %>
  <% if params[:order] %>
    <% f.hidden(:order, name: 'order', value: params[:order]) %>
  <% end %>

  <% f.input(:id) %>
  <% f.input_range(:created_at, label: 'Account Created On') %> <%# creates a filter to search a range %>
  <% f.input(:full_name) %>
  <% f.input(:email_address) %>

  <% f.action(:submit, label: 'Filter') %>
<% end %>