Skip to content

Repository files navigation

Gem Version

Queryko

This gem provides additional functionality on your query objects. It will filter and paginate your query by supplying arguments directly from controller params

Installation

# Paginationgem'kaminari'#orgem'will_paginate'# Query Objectgem'queryko'

And then execute:

$ bundle

Or install it yourself as:

$ gem install queryko

Usage

Create a query object

classProductsQuery < Queryko::Basefeature:created_at,:minfeature:created_at,:maxfeature:price,:minfeature:price,:maxfeature:name,:search,as: :namefeature:vendor,:search,as: :vendorfeature:id,:after,as: :since_idfeature:id,:batch,as: :by_idsend

Using your query object

Filter your query by appending _min or _max on your defined attributes. You can also filter results by attribute with your defined feature.

# Collectionproducts=ProductsQuery.new(price_min: 100,price_max: 150,name: 'Milk').callproducts=ProductsQuery.new(since_id: 26).call# Count - Counts items on current page. Default page is 1products=ProductsQuery.new(created_at_min: 'Jan 1, 2019').countproducts=ProductsQuery.new(name: 'Bag').count# Total Count - Counts all items matching defined conditionsproducts=ProductsQuery.new(created_at_min: 'Jan 1, 2019').total_countproducts=ProductsQuery.new(name: 'Bag').total_count

Object Methods

  • count - Returns the filtered count including pagination filter or the size of return object.
  • total_count - Returns the overall count of the filtered total records.

Example:

Products.count # 21 rows
query = ProductsQuery.new(page: 5, limit: 5)
query.count # 1
query.total_count # 21

Pagination

Override these methods to customize pagination limits

...
defupper_limit20# default is 100enddeflower_limit5# default is 10enddefdefault_limit10# default is 50end
...

Custom Filters

Create a custom filter class using Queryko::Filters::Base

classFilters::CoolSearch < Queryko::Filters::Base# Optional.# Some `options` keys are reserved for basic functionality# Use `options` to get data from feature definitiondefinitialize(options={},feature)superoptions,featureend# Required. This method is called by query object. Always return the result of# the collectiondefperform(collection,token,query_object)collection.where("#{table_name}.#{column_name} < ?","Cool-#{token}")endend

Then add the filter class to your Queryko::Base object

classQueryObject < Queryko::Basefilter_class:cool_search,CustomFilters::CoolSearch# orfilter_class:cool_search,"CustomFilters::CoolSearch"feature:name,:cool_search# the :name will be scoped using params[:name_cool_search]end

Other available options

Optiondescription
since_idretrieves records after id
pagepage to retrieve

File structure

You are free to structure your query objects and custom filters. Or group them in one folder

app/
└─ queries/
├─ filters/
│ └─ cool_search.rb
├─ products_query.rb
└─ query_base.rb

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/neume/queryko. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

A gem that simplifies query objects

Topics

Resources

Code of conduct

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages