All Projects → shioyama → wharel

shioyama / wharel

Licence: MIT License
Arel made simple

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to wharel

active record distinct on
Support for `DISTINCT ON` statements when querying with ActiveRecord
Stars: ✭ 23 (-75.79%)
Mutual labels:  activerecord, arel
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-77.89%)
Mutual labels:  activerecord, arel
zen-query
parascope gem for param-based scope generation
Stars: ✭ 25 (-73.68%)
Mutual labels:  activerecord
idy
👓 An ID obfuscator for ActiveRecord
Stars: ✭ 15 (-84.21%)
Mutual labels:  activerecord
filedb
ActiveRecord for static data definitions based on files
Stars: ✭ 72 (-24.21%)
Mutual labels:  activerecord
ar-dynattribute
Provide ActiveRecord dynamic attributes stored into the single field in serialized state
Stars: ✭ 43 (-54.74%)
Mutual labels:  activerecord
active-persistence
Active Persistence is a implementation of Active Record Query Interface for JPA that makes it easy and fun.
Stars: ✭ 14 (-85.26%)
Mutual labels:  activerecord
rails async migrations
Asynchronous support for ActiveRecord::Migration
Stars: ✭ 56 (-41.05%)
Mutual labels:  activerecord
rails-microservices-book
A guide to building distributed Ruby on Rails applications using Protocol Buffers, NATS and RabbitMQ
Stars: ✭ 23 (-75.79%)
Mutual labels:  activerecord
wisper-activerecord
Transparently publish all model changes to subscribers
Stars: ✭ 97 (+2.11%)
Mutual labels:  activerecord
ar-check
Enable PostgreSQL's CHECK constraints on ActiveRecord migrations
Stars: ✭ 17 (-82.11%)
Mutual labels:  activerecord
rails-countries
Integration between Rails and countries gem.
Stars: ✭ 17 (-82.11%)
Mutual labels:  activerecord
models stats
Charts for your rails models with MetricsGraphics.js and NVD3
Stars: ✭ 43 (-54.74%)
Mutual labels:  activerecord
datetime-scopes
A plugin for ActiveRecord which adds very helpful methods for querying records over time ranges (weeks/days/months/years), and it also respects time-zones!
Stars: ✭ 14 (-85.26%)
Mutual labels:  activerecord
mv-core
Migration Validators project core classes
Stars: ✭ 23 (-75.79%)
Mutual labels:  activerecord
LocalSupport
A directory of local support services and volunteer opportunities
Stars: ✭ 60 (-36.84%)
Mutual labels:  activerecord
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (-77.89%)
Mutual labels:  activerecord
computed model
Batch loader with dependency resolution and computed fields
Stars: ✭ 22 (-76.84%)
Mutual labels:  activerecord
SimpleCurd
2个类,实现类ActiveRecord,无需写Mapper, mybatis增强
Stars: ✭ 14 (-85.26%)
Mutual labels:  activerecord
normalize attributes
Sometimes you want to normalize data before saving it to the database like down casing e-mails, removing spaces and so on. This Rails plugin allows you to do so in a simple way.
Stars: ✭ 41 (-56.84%)
Mutual labels:  activerecord

Wharel

Gem Version Build Status

Wharel helps you write concise Arel queries with ActiveRecord using Virtual Rows inspired by Sequel.

Although similar in spirit to gems like Squeel and BabySqueel, which provide sophisticated block-based interfaces for querying with Arel, Wharel is much much smaller. In fact, the core of the gem is only 30 lines long! It uses a single BasicObject as a clean room to evaluate the query block. That's really all there is to it.

For a more detailed explanation of the implementation, see this blog post.

Installation

Add this line to your application's Gemfile:

gem 'wharel', '~> 1.0.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install wharel

Usage

Suppose we have a model Post:

class Post < ApplicationRecord
  has_many :comments
end

And let's assume our Post has columns title and content.

Now, if we wanted to find all the posts with a title which matched the string "foo" and content which matched the string "bar", we'd have to resort to something like this:

title = Post.arel_table[:title]
content = Post.arel_table[:content]
Post.where(title.matches("foo").and(content.matches("bar")))

With Wharel, you can drop the boilerplate and just use a block:

Post.where { title.matches("foo").and(content.matches("bar")) }

Wharel will map title and content in the block to the appropriate Arel attribute for the column.

Wharel also supports most other query methods, e.g. not:

Post.where.not { title.eq("foo") }

... and order:

Post.order { title.lower }

Wharel also supports select, having, pluck, pick, group and or in the same way.

Now suppose we have another model Comment with a column content, and a Post has_many :comments:

class Post < ApplicationRecord
  has_many :comments
end

class Comment < ApplicationRecord
  belongs_to :post
end

Now we want to find all comments which match the title of the comment's post. With standard Arel, you could do this with:

posts = Post.arel_table
comments = Comment.arel_table
Comment.joins(:post).where(comments[:content].matches(posts[:title]))

Using Wharel, you can pass an argument to blocks to handle this case:

Comment.joins(:post).where { |c| Post.where { |p| c.content.matches(p.title) } }

Much better!

Contributing

Notice something wrong or a feature missing? Post an issue or create a pull request.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].