Project

hayrick

0.0
No commit activity in last 3 years
No release in over 3 years
Create Query Objects without a hitch.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
>= 0
~> 10.0
~> 3.0
~> 4.0
 Project Readme

Hayrick

Query Objects without a hitch.


Build Status Gem Version

Hayrick lets you easily create Query Objects, decoupling your data models from querying concerns. It is extremely lightweight, having less than 100 lines of code and no external dependencies. Hayrick currently supports Active Record and Sequel.

Usage

First, add this line to your application's Gemfile:

gem 'hayrick'

Then you have just to include the Hayrick module and implement a #base_scope method:

class AlbumCollection
  include Hayrick

  def base_scope
    Album.all # or Album.unscoped depending on your Rails version
  end
end

And that's it! You're now able to perform queries by calling AlbumCollection#search.

AlbumCollection.new.search(artist: 'Morphine')
=> #<ActiveRecord::Relation [#<Album id: 1, name: "Good", artist: "Morphine", release_date: "1992-09-08">, #<Album id: 2, name: "Cure for Pain", artist: "Morphine", release_date: "1993-09-14">...]>

That seems fine, but we usually want to carry out queries using parameters other than our model fields, right? Hayrick provides a simple DSL for adding such filters:

class AlbumCollection
  include Hayrick

  filter :year do |search_relation, year|
    search_relation.where('extract(year from released_at) = ?', year)
  end

  def base_scope
    Album.all
  end
end

Notice the .filter method receives two parameters:

  • a filter name (in this case, :year)
  • a filter definition which can be either a Proc or a block. This callable object must receive two arguments: the first is the search relation and the second is the argument for the filter itself. Make sure to always return the search relation to prevent the filter chain from breaking.
AlbumCollection.new.search(artist: 'Morphine', year: 1993)
=> #<ActiveRecord::Relation [#<Album id: 2, name: "Cure for Pain", artist: "Morphine", release_date: "1993-09-14">]>