Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/inaki-ibarra/qry_filter
A Rails add-on for handling ActiveRecord::Relation filters
https://github.com/inaki-ibarra/qry_filter
activerecord ruby-gem ruby-on-rails
Last synced: about 1 month ago
JSON representation
A Rails add-on for handling ActiveRecord::Relation filters
- Host: GitHub
- URL: https://github.com/inaki-ibarra/qry_filter
- Owner: inaki-ibarra
- License: mit
- Created: 2019-09-05T07:31:30.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-31T13:47:46.000Z (over 1 year ago)
- Last Synced: 2024-04-24T08:27:33.183Z (7 months ago)
- Topics: activerecord, ruby-gem, ruby-on-rails
- Language: Ruby
- Homepage: https://rubygems.org/gems/qry_filter
- Size: 33.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
[![Gem Version](https://badge.fury.io/rb/qry_filter.svg)](https://badge.fury.io/rb/qry_filter)
![](https://github.com/inaki-ibarra/qry_filter/workflows/CI/badge.svg)
![](https://github.com/inaki-ibarra/qry_filter/workflows/CD/badge.svg)
[![Maintainability](https://api.codeclimate.com/v1/badges/de0189f5068cc9cec4ab/maintainability)](https://codeclimate.com/github/inaki-ibarra/qry_filter/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/de0189f5068cc9cec4ab/test_coverage)](https://codeclimate.com/github/inaki-ibarra/qry_filter/test_coverage)# QryFilter
QryFilter aka "QueryFilter" is a simple Rails gem that provides a pattern and helper when dealing with lots of filter clauses in your ActiveRecord query.
## Usage
**Filter Class**
```ruby
# app/filters/user_filter.rb
class UserFilter < ApplicationFilter
def by_id
@scope = @scope.where(id: @filter_hash[:id])
enddef by_age
@scope = @scope.where(age: @filter_hash[:age])
end
end
```**In Controller**
```ruby
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
users = filter User, params
# ...
end
end
```**Other options**
Class Method:
```ruby
params = {id: [1, 2, 3], age: [18, 20]}
users = User.where(happy: true)QryFilter.compose(users, params, filter_by: [:id, :age], filter_class: UserFilter)
```Helper:
```ruby
filter User, params, filter_by: [:id, :age], filter_class: UserFilter
```- The first argument accepts ActiveRecord::Relation or model class name.
- The second is for key-value pair of data you want to pass to your filter class.
- The last argument is an optional hash and allows you to set `filter_by` and `filter_class`
- `filter_by` maps with your filter class methods e.g. `[:id]` will only trigger `by_id` method. If empty, all filters will be triggered.
- `filter_class` allows you to set a specific class when needed.## Installation
Add this line to your application's Gemfile:
```ruby
gem 'qry_filter'
```And then execute:
```bash
$ bundle
```Or install it yourself as:
```bash
$ gem install qry_filter
```Generate app/filters/application_filter.rb:
```bash
$ rails g qry_filter:install
```Include QryFilter in ApplicationController
```ruby
class ApplicationController < ActionController::Base
include QryFilter
end
```## Contributing
Fork the repo and submit a pull request.
Please follow this [Rails style guide](https://github.com/rubocop-hq/rails-style-guide).## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).