Socket
Book a DemoInstallSign in
Socket

ransack_mongo

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ransack_mongo

1.0.1
bundlerRubygems
Version published
Maintainers
1
Created
Source

Ransack Mongo

Build Status

Ransack Mongo is based on Ransack, but for MongoDB.

With Ransack Mongo you can convert query params into Mongo queries.

Why another gem?

Given that Ransack is built on top of ARel and that ARel only works with relational databases, I don't see how we could add Mongoid support without dramatically changing everything.

Installation

Add this line to your application's Gemfile:

gem 'ransack_mongo'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ransack_mongo

Usage

# GET /customers?q[name_eq]=Pablo&q[middle_name_or_last_name_cont]=Cantero

# params[:q]
# => { name_eq: 'Pablo', middle_name_or_last_name_cont: 'Cantero' }

# RansackMongo::Query.parse(params[:q])
# => { name: 'Pablo', '$or' => { middle_name: /Cantero/i, last_name: /Cantero/i } }

# GET /customers
def index
  selector = RansackMongo::Query.parse(params[:q])

  # Mongo Ruby Driver
  @customers = db['customers'].find(selector)

  # Moped
  @customers = session[:customers].find(selector)

  # Mongoid
  @customers = Customer.where(selector)
end

Available predicates

  • eq
  • not_eq
  • cont
  • in
  • start
  • mstart
  • gt
  • lt
  • gteq
  • lteq

OR operator

You can also combine predicates for OR queries.

query_param = { name_eq: 'Pablo', middle_name_or_last_name_cont: 'Cantero' }

RansackMongo::Query.parse(params[:q])
# => { name: 'Pablo', '$or' => { middle_name: /Cantero/i, last_name: /Cantero/i } }

parse!

You can use parse! for stricter validations. This method will raise an exception if a query cannot be produced.

 # xpto isn't a valid predicate

RansackMongo::Query.parse(name_xpto: 'Pablo')
# => {}

RansackMongo::Query.parse!(name_xpto: 'Pablo')
# => RansackMongo::MatcherNotFound: No matchers found. To allow empty queries use .parse instead

Contributing

  • Fork it ( https://github.com/phstc/ransack_mongo/fork )
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

FAQs

Package last updated on 27 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.