FilterMe

Build Status Code Climate Gem Version

A Rails/ActiveRecord filtering gem

FilterMe provids helpers and classes that provides filtering using Ruby classes and object oriented development

Installation

gem "filter_me", "0.1.0"

Filter:

* Highly subject to change as the API moves closer to 1.0

class AccountsFilter < FilterMe::ActiveRecordFilter
  model Account

  field :type, [:matches, :eq, :not_eq] # Uses arel, so any Arel::Predications method should work
  field :cost, [:lt, :gt, :lteq, :gteq, :eq] # Uses arel, so any Arel::Predications method should work
end

class AccountsController < ApplicationController
  include FilterMe

  def index
    @accounts = filter_me(Account.all)
  end
end

Given a controller that recieves params like the following:

params # => {filters: {type: {eq: "admin"} } }

The following SQL would be performed (Using ActiveRecord):

SELECT "accounts".* FROM "accounts" WHERE ("accounts"."type" = "admin")

Nested Filtering:

class UsersFilter < FilterMe::ActiveRecordFilter
  model User

  association :account, :filter_class => AccountsFilter
  field :username, [:matches, :eq, :not_eq] # Uses arel, so any Arel::Predications method should work
end

class UsersController < ApplicationController
  include FilterMe

  def index
    @users = filter_me(User.all)
  end
end

With the following params:

params # => {:filters => {:email => {:matches => "%test.com"}, :account => {:cost => {:lt => 100000} } } }

Performs:

SELECT "users".* FROM "users" INNER JOIN "accounts" ON "accounts"."user_id" = "users"."id"
    WHERE ("users"."email" LIKE '%test.com') AND ("accounts"."cost" < 100000)

License

Copyright (c) 2014, Filter Me is developed and maintained by Sam Clopton, and is released under the open MIT Licence.