Project

filtron

0.0
No commit activity in last 3 years
No release in over 3 years
Pretty aliases for filtering your data
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

Filtron

Filtron was built to work with Ransack. Ransack uses search predicates to simplify searching and querying your model data. Filtron aims to alias these predicates so your URLs are a little less ugly when filtering your data via GET requests. Take this URL for example:

http://example.com/users?user[first_name_eq]=Lee&user[email_cont]=@gmail

# Controller:
def index
  @users = User.search(params[:user]).result
end

Wouldn't this be nicer?

http://example.com/users?name=Lee&email=@gmail

# Controller:
def index
  @users = User.filter(params)
end

Installation

Add this line to your application's Gemfile:

gem 'filtron'

Usage

You can easily add Filtron to your existing models

class User < ActiveRecord::Base
  filter_with :fname, :first_name_eq
  filter_with :lname, :last_name_eq
  filter_with :country, :address_country_name_eq do |code|
    Country.find_by_code(code).name
  end

  queries :first_name, :email
end

Now in your controller:

def index
  @users = User.filter(params)
end

params might look something like this:

{
  fname: "Lee",
  country: "gb",
  q: "@gmail.com",
  something: "else"
}

This will translate into the following Ransack predicates:

{
  first_name_eq: "Lee",
  address_country_name_eq: "Great Britain",
  first_name_or_email_cont: "@gmail.com"
}

Filtron will then use these predicates to search for and return a result.

By default, queries will use params[:q] for a general query. You can change this in the call to queries:

queries :first_name, :last_name, :email, with: :search

Now, /users?search=foo will use these query predicates.