Sha256: cf6c02c9655efad2291e53da840ffcc67fff0445b218ff697c11c90d3270f9cc

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

# Filtering

Gem for comfort filtering of ActiveRecord queries.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'filtering'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install filtering

## Usage
All you need to do is inherit custom filter service from `Filtering::Base` and define params for filtering.

### Example filter service

```
class Filters::UsersFilter < Filtering::Base
  def initialize(params, args)
    super(params, args)
  end

  private

  # Required methods

  def relation
    User.all
  end

  def plain_acessible_params
    %i[city age]
  end

  def complex_acessible_params
    %i[name]
  end

  # Complex filters

  def filter_by_name(name)
    results.where('name ILIKE ? or name_auto ILIKE ?', "%#{name}%", "%#{name}%")
  end
end

```
There are private methods:

`relation` is an ActiveRecord initial relation which must be filtered

`plain_acessible_params` array of params for an auto-filtering by `where`, so if you have `%i[city age]` will be called `User.where(city: city).where(age: age)`

`complex_acessible_params` array of params for filters with custom logic. If you have some params in that method you have to create methods for those custom filters with format: `filter_by_{param}`

### Calling
Controller usage example:

```
def index
  render json: Filters::UsersFilter.new(params, page: params[:page]).call
end
```
If you don't use Kaminari just delete page from initializer

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the Filtering project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/filtering/blob/master/CODE_OF_CONDUCT.md).

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
filtering-0.1.2 README.md