https://github.com/nosolosoftware/active_record_filterable
https://github.com/nosolosoftware/active_record_filterable
accents activerecord filters rails ruby
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/nosolosoftware/active_record_filterable
- Owner: nosolosoftware
- License: mit
- Created: 2018-06-28T10:55:43.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-02-01T07:31:31.000Z (over 4 years ago)
- Last Synced: 2025-02-19T12:52:50.903Z (over 1 year ago)
- Topics: accents, activerecord, filters, rails, ruby
- Language: Ruby
- Size: 23.4 KB
- Stars: 0
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# ActiveRecord::Filterable

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
```ruby
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
```ruby
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:
```ruby
City.filtrate(people_range: [500, 1000]).count # => 1
```
#### Rails controller
```ruby
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.
```ruby
filter_by_normalized :name
```
enables to do sth like:
```ruby
City.filtrate(name_normalized: 'text with accents')
```
It also depends on which adapter you are using.
* Postgresql: It makes use of [`unaccent`](https://www.postgresql.org/docs/9.1/static/unaccent.html). You need to activate in [your project](https://binarapps.com/blog/accent-insensitive-queries-in-rails-with-postgresql).
* Mysql, SqlServer: you only need to select an [accent-insensitive collation](https://binarapps.com/blog/accent-insensitive-queries-in-rails-with-postgresql). In cases where this is not possible you could even write:
```ruby
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