Coder Social home page Coder Social logo

active_record_filterable's Introduction

ActiveRecord::Filterable

Build Status

Let you add scopes to active_record document for filters.

Installation

Add this line to your application's Gemfile:

gem 'active_record_filterable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_record_filterable

Usage

Model

class City < ActiveRecord::Base
  include ActiveRecord::Filterable

  filter_by(:name)
  filter_by(:people, ->(value) { where(City.arel_table[:people].gt(value)) })
  filter_by(:people_range, (lambda do |range_start, range_end|
    where(people: range_start..range_end)
  end))
end

City.create(name: 'city1', people: 100)
City.create(name: 'city2', people: 1000)
City.filtrate({name: 'city'}).count # => 2
City.filtrate({name: 'city1'}).count # => 1
City.filtrate({name: ''}).count # => 0
City.filtrate({people: 500}) # => 1
City.filtrate({invalid: 'x'}) # => 2 (is ignored)

Operator

You can specify selector operator:

  • and (default operator)
  • or
City.filtrate({name: 'city1', people: 1000}, 'and').count # => 0
City.filtrate({name: 'city1', people: 1000}, 'or').count # => 1

Range

Searches with more than one param is also available:

City.filtrate(people_range: [500, 1000]).count # => 1

Rails controller

class CitiesController
  def index
    respond_with City.filtrate(filter_params)
  end

  private

  def filter_params
    params.slice(:name, :people)
  end
end

Normalized values

Searches without considering accents are also supported.

  filter_by_normalized :name

enables to do sth like:

City.filtrate(name_normalized: 'text with accents')

It also depends on which adapter you are using.

  • Postgresql: It makes use of unaccent. You need to activate in your project.
  • Mysql, SqlServer: you only need to select an accent-insensitive collation. In cases where this is not possible you could even write:
    filter_by :name, (lambda do |value|
      where("name LIKE ? COLLATE utf8_general_ci", "%#{value}%")
    end)

Contributing

  1. Fork it ( http://github.com/nosolosoftware/active_record_filterable/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

active_record_filterable's People

Contributors

javierav avatar pacop avatar rjurado01 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.