Giter Club home page Giter Club logo

queryko's Introduction

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

# Pagination
gem 'kaminari'
#or
gem 'will_paginate'

# Query Object
gem 'queryko'

And then execute:

$ bundle

Or install it yourself as:

$ gem install queryko

Usage

Create a query object

class ProductsQuery < Queryko::Base
  feature :created_at, :min
  feature :created_at, :max
  feature :price, :min
  feature :price, :max
  feature :name, :search, as: :name
  feature :vendor, :search, as: :vendor
  feature :id, :after, as: :since_id
  feature :id, :batch, as: :by_ids
end

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.

# Collection
products = ProductsQuery.new(price_min: 100, price_max: 150, name: 'Milk').call
products = ProductsQuery.new(since_id: 26).call

# Count - Counts items on current page. Default page is 1
products = ProductsQuery.new(created_at_min: 'Jan 1, 2019').count
products = ProductsQuery.new(name: 'Bag').count

# Total Count - Counts all items matching defined conditions
products = ProductsQuery.new(created_at_min: 'Jan 1, 2019').total_count
products = 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

...

def upper_limit
  20 # default is 100
end

def lower_limit
  5 # default is 10
end

def default_limit
  10 # default is 50
end

...

Custom Filters

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

class Filters::CoolSearch < Queryko::Filters::Base

  # Optional.
  # Some `options` keys are reserved for basic functionality
  # Use `options` to get data from feature definition
  def initialize(options = {}, feature)
    super options, feature
  end

  # Required. This method is called by query object. Always return the result of
  # the collection
  def perform(collection, token, query_object)
    collection.where("#{table_name}.#{column_name} < ?", "Cool-#{token}")
  end
end

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

class QueryObject < Queryko::Base
  filter_class :cool_search, CustomFilters::CoolSearch
  # or
  filter_class :cool_search, "CustomFilters::CoolSearch"


  feature :name, :cool_search
  # the :name will be scoped using params[:name_cool_search]
end

Other available options

Option description
since_id retrieves records after id
page page 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.

queryko's People

Contributors

denmarkmeralpis avatar dependabot[bot] avatar neume avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

denmarkmeralpis

queryko's Issues

ActiveRecord::StatementInvalid when using feature search

I got this error when using the feature :status, :search, as: :status

ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'query_object.status' in 'where clause': SELECT  `accounts`.* FROM `accounts` WHERE (query_object.status LIKE '%inactive%') LIMIT 11 OFFSET 0)

Here's how I defined it

# accounts_query.rb
...
feature :status, :search, as: :status
...

# accounts_controller.rb
...
def index
  AccountsQuery({ status: 'active' }, Account.all).call
end
...

Test against multiple ruby/rails versions

This project works on rails 5 and 4 but without any written test against these versions.

Using appraisal gem from thoughtbot, test this gem against rails 4, 5 ,6 and, if possible, against rails 3.

You can also use gemika to test it against ruby versions

[Bug] Infer from query object name not working

ProductsQuery.new(created_at_min: 'Jun 10, 2019')

From the example, results in.

NoMethodError (undefined method `paginate' for {:created_at_max=>Mon, 10 Jun 2019 10:18:50 +0000}:Hash)

I am using Rails 5.1.6, Ruby 2.5.0

Thank you gwapo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.