Project

halumi

0.0
No commit activity in last 3 years
No release in over 3 years
Use query objects as flexible building blocks
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.6.0

Runtime

 Project Readme

Halumi

One of the cool patterns in Rails development are query objects. They are essantialy service objects returning a ActiveRecord Relation. Using them results in less code cluttering your controllers and models. Plus you can easily unit test each of your queries.

This Gem adds simple DSL allowing you to build query objects and than combining them into one query that will be executed against your database

Example usage

Define your 'super' query combining 3 subqueries

class ArticlesQuery < Halumi::Query
  model Article

  merge PaginationQuery
  merge PublishedArticlesQuery
  merge OrderArticlesQuery
end
class PaginationQuery < Halumi::Query
  param :per_page
  param :page

  def execute
    relation.page(page).per(per_page)
  end
end
class PublishedArticlesQuery
  def execute
    relation.where(published: true)
  end
end
class OrderArticlesQuery < Halumi::Query
  def execute
    relation.order(:created_at)
  end
end

Run your query

This will return published articles, paginated and sorted by creation time

params = { page: 2, per_page: 10 }

ArticlesQuery.new(params).call

Optional integration with Dry::Types

Params with dry type specified behave like they would be sanatized by it. If a a type would rise an error, for example in case of strict types, the error will be rescued and query execution will be terminated

class PaginationQuery < Halumi::Query
  param :per_page, Types::Strict::Integer
  param :page, Types::Strict::Integer

  def execute
    relation.page(page).per(per_page)
  end
end