No commit activity in last 3 years
No release in over 3 years
Simply conditions DSL for ActiveRecord
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
 Dependencies

Development

Runtime

>= 3.0.0
>= 3.0.0
 Project Readme

activeecord-blockwhere

Simply conditions DSL for ActiveRecord. It helps using Arel Predications in the block.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-blockwhere'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-blockwhere

Usage

# * person has many entries.
# Person(id: integer, name: string)
# Entry(id: integer, person_id: integer, name: string)

Person.where { id.eq(1) }.to_sql
# => SELECT "people".* FROM "people"  WHERE "people"."id" = 1

Person.where { !id.eq(1) }.to_sql
# => SELECT "people".* FROM "people"  WHERE (NOT ("people"."id" = 1))

Person.where { id.eq(1) & name.matches('%alice%') }.to_sql
# => SELECT "people".* FROM "people"  WHERE ("people"."id" = 1 AND "people"."name" LIKE '%alice%')

Person.where { id.in([1,2,3]) & name.not_eq('bob') }.to_sql
# => SELECT "people".* FROM "people"  WHERE ("people"."id" IN (1, 2, 3) AND "people"."name" != 'bob')

Person.where { name.eq('alice') | name.eq('bob') }.to_sql
# => SELECT "people".* FROM "people"  WHERE (("people"."name" = 'alice' OR "people"."name" = 'bob'))

# join association
Person.where { name.eq('alice') & entries.name.matches('%hello%') }.to_sql
# => SELECT "people".* FROM "people"
#    INNER JOIN "entries" ON "entries"."person_id" = "people"."id"
#    WHERE ("people"."name" = 'alice' AND "entries"."name" LIKE '%hello%')

# in action method
def index
  # you can use controller's method in the block. ex: params,request,etc..
  # but cannot use instance variables.
  @person = Person.where { id.eq(params[:id]) }.first
end

Introductory articles

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request