• Stars
    star
    42
  • Rank 656,625 (Top 13 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

โ” Gives your queries a home and avoid tucking scopes inside your models

Queryable

Gem Version Build Status Test Coverage Code Climate Inline docs License

Queryable is a mixin that allows you to easily define query objects with chainable scopes.

Scopes

Scopes serve to encapsulate reusable business rules, a method is defined with the selected name and block (or proc)

class CustomersQuery
  include Queryable

  scope(:recent) { desc(:logged_in_at) }

  scope :active, ->{ where(status: 'active') }

  scope :favourite_brand do |product, brand|
    where("favourites.#{product}": brand)
  end

  def current
    recent.active
  end

  def miller_fans
    favourite_brand(:beer, :Miller)
  end
end


CustomerQuery.new(shop.customers).miller_fans

Delegation

By default most Array methods are delegated to the internal query. It's possible to delegate extra methods to the query by calling delegate.

class CustomersQuery
  include Queryable

  delegate :update_all, :destroy_all, :exists?
end

Delegate and Chain

Sometimes you want to delegate a method to the internal query, but continue working with the query object like if you were calling scopes.

You can achieve that using delegate_and_chain, which will delegate the method call, assign the return value as the internal query, and return the query object.

class CustomersQuery
  include Queryable

  delegate_and_chain :where, :order_by
end

Advantages

  • Query objects are easy to understand.
  • You can inherit, mixin, and chain queries in a very natural way.
  • Increased testability, pretty close to being ORM/ODM agnostic.

Basic Usage

If you are using Mongoid or ActiveRecord, you might want to try the Queryable::Mongoid and Queryable::ActiveRecord modules that already take care of delegating and chaining most of the methods in the underlying queries.

class CustomersQuery
  include Queryable::Mongoid
end

CustomersQuery.new.where(:amount_purchased.gt => 2).active.asc(:logged_in_at)

This modules also include all the optional modules. If you would like to opt-out of the other modules you can follow the approach in the Notes section.

Advanced Usage

There are three opt-in modules that can help you when creating query objects. These modules would need to be manually required during app initialization or wherever necessary (in Rails, config/initializers).

DefaultQuery

Provides default initialization for query objects, by attempting to infer the class name of the default collection for the query, and it also provides a queryable method to specify it.

require 'queryable/default_query'

def CustomersQuery
  include Queryable
  include Queryable::DefaultQuery
end

def OldCustomersQuery < CustomersQuery
  queryable ArchivedCustomers
end

CustomersQuery.new.queryable == Customer.all
OldCustomersQuery.new.queryable == ArchivedCustomers.all

If you want to use common base objects for your queries, you may want want to delay the automatic inference:

class BaseQuery
  include Queryable
  include Queryable::DefaultQuery

  queryable false
end

class CustomersQuery < BaseQuery
end

CustomersQuery.new.queryable == Customer.all

DefaultScope

Allows to define default scopes in query objects, and inherit them in query object subclasses.

require 'queryable/default_scope'

def CustomersQuery
  include Queryable
  include Queryable::DefaultScope
  include Queryable::DefaultQuery

  default_scope :active
  scope :active, -> { where(:last_purchase.gt => 7.days.ago) }
end

def BigCustomersQuery < CustomersQuery
  default_scope :big_spender
  scope :big_spender, -> { where(:total_expense.gt => 9999999) }
end

CustomersQuery.new.queryable == Customer.where(:last_purchase.gt => 7.days.ago)

BigCustomersQuery.new.queryable ==
Customer.where(:last_purchase.gt => 7.days.ago, :total_expense.gt => 9999999)

Chainable

While scopes are great because of their terseness, they can be limiting because the block executes in the context of the internal query, so methods, constants, and variables of the Queryable are not accessible.

For those cases, you can use a normal method, and then chain it. Chainable will take care of setting the return value of the method as the internal query, and return self at the end to make the method chainable.

class CustomersQuery
  include Queryable
  include Queryable::Chainable

  chain :active, :recent

  def active
    where(status: 'active')
  end

  def recent
    queryable.desc(:logged_in_at)
  end

  chain def search(field_values)
    field_values.inject(queryable) { |query, (field, value)|
      query.where(field => /#{value}/i)
    }
  end

  def search_in_active(field_values)
    search(field_values).active
  end
end


CustomerQuery.new(shop.customers).miller_fans.search_in_current(last_name: 'M')

Notes

To avoid repetition, it's a good idea to create a BaseQuery object to contain both the modules inclusion, and common scopes you may reuse.

require 'queryable/chainable'
require 'queryable/default_scope'
require 'queryable/default_query'

def BaseQuery
  include Queryable
  include Queryable::Chainable
  include Queryable::DefaultScope
  include Queryable::DefaultQuery

  # If you want to be concise:
  include Queryable::DefaultQuery, Queryable::DefaultScope, Queryable::Chainable, Queryable

  queryable false

  scope :recent, ->{ where(:created_at.gt => 1.week.ago) }
end

def CustomersQuery < BaseQuery
...
end

Testing

You can check the specs of the project to check how to test query objects without even having to require the ORM/ODM, or you can test by requiring your ORM/ODM and executing queries as usual.

RDocs

You can view the Queryable documentation in RDoc format here:

http://rubydoc.info/github/ElMassimo/queryable/master/frames

License

Copyright (c) 2014 Mรกximo Mussini

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

vite_ruby

โšก๏ธ Vite.js in Ruby, bringing joy to your JavaScript experience
Ruby
1,270
star
2

iles

๐Ÿ The joyful site generator
TypeScript
1,074
star
3

vite-plugin-image-presets

๐Ÿ–ผ Image Presets for Vite.js apps
TypeScript
246
star
4

vite-plugin-environment

Easily expose environment variables in Vite.js
TypeScript
144
star
5

vite-plugin-full-reload

โ™ป๏ธ Automatically reload the page when files are modified
JavaScript
134
star
6

oj_serializers

โšก๏ธ Faster JSON serialization for Ruby on Rails. Easily migrate away from Active Model Serializers.
Ruby
113
star
7

js_from_routes

๐Ÿ›ฃ๏ธ Generate path helpers and API methods from your Rails routes
Ruby
98
star
8

types_from_serializers

โœ… Generate TypeScript interfaces from your JSON serializers
Ruby
92
star
9

request_store_rails

๐Ÿ“ฆ Per-request global storage for Rails prepared for multi-threaded apps
Ruby
85
star
10

vuex-stores

๐Ÿ—„ Store objects for Vuex, a simple and more fluid API for state-management.
JavaScript
62
star
11

vue-custom-element-example

An example on how to define custom elements using Vue 3
TypeScript
59
star
12

jekyll-vite

โšก๏ธ๐Ÿฉธ Use Vite.js in Jekyll as your assets pipeline
Ruby
50
star
13

vite-plugin-stimulus-hmr

โšก๏ธ HMR for Stimulus controllers in Vite.js
TypeScript
50
star
14

mongoid_includes

๐ŸŒฟ Improves eager loading support for Mongoid
Ruby
46
star
15

stimulus-vite-helpers

Helpers to easily load all your Stimulus controllers when using Vite.js
TypeScript
43
star
16

capybara-compose

โœ… Easily write fluent integration tests with Capybara in Ruby
Ruby
32
star
17

better_settings

โš™ Settings for Ruby apps โ€“ fast, immutable, better.
Ruby
20
star
18

vite-plugin-bugsnag

Report builds and upload source maps to Bugsnag
TypeScript
18
star
19

i18n_multitenant

๐ŸŒŽ Provides a convenient way to use tenant-specific translations
Ruby
18
star
20

vite-plugin-manifest-sri

Subresource Integrity for Vite.js manifest files
JavaScript
15
star
21

resourcerer

โœจ Works like magic to dry up your controllers
Ruby
10
star
22

sublime-toggle-dark-mode

๐ŸŒš๐ŸŒž Toggle between dark and light mode in Sublime Text 4
JavaScript
10
star
23

pakiderm

๐Ÿ˜ Pakiderm will never forget the return value
Ruby
7
star
24

presenter_rails

๐Ÿ”ญ Expose your view models in a convenient way
Ruby
6
star
25

vite-plugin-erb

Use ERB files in Vite.js projects with a Ruby backend
TypeScript
5
star
26

journeyman

Let your factories use your business logic, making them flexible and easier to update.
Ruby
5
star
27

jekyll-vite-minima

โšก๏ธ๐Ÿฉธ Use Vite.js in Jekyll minima theme as your assets pipeline
Ruby
3
star
28

automatic-music-transcription

Automatically exported from code.google.com
C
2
star
29

vite-plugin-xdm

Use XDM in VIte.js
JavaScript
2
star
30

fast-food-mvc

Automatically exported from code.google.com
C#
1
star
31

ElMassimo

1
star
32

vite-vue-router-hmr-repro

Vue
1
star
33

crouton

๐Ÿž Context sensitive notifications for Rails
Ruby
1
star