No commit activity in last 3 years
No release in over 3 years
Easy way to add scopes to your models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0

Runtime

>= 0
>= 3.0, < 8.0
 Project Readme

Mongoid::Filterable

Build Status

Let you add scopes to mongoid document for filters.

Installation

Add this line to your application's Gemfile:

gem 'mongoid-filterable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid-filterable

Compatibility

Mongoid Filterable supports Mongoid versions 3, 4, 5, 6 and now 7.

Usage

Model

class City
  include Mongoid::Document
  include Mongoid::Filterable

  field :name
  field :people

  filter_by(:name)
  filter_by(:people, ->(value) { where(:people.gt => value) })
  filter_by(:people_range, (lambda do |range_start, range_end|
    where(:people.lte => range_end,
          :people.gte => range_start)
  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

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
class City
  include Mongoid::Document
  include Mongoid::Filterable
  include Mongoid::NormalizeStrings

  field :name
  normalize :name

  filter_by_normalized(:name)
end

City.create(name: 'Cíty1')
City.create(name: 'Cíty2')
City.filtrate({name: 'city1'}).count # => 1

Contributing

  1. Fork it ( http://github.com//mongoid-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