-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rails-7' into 'master'
ADD rails-7 support See merge request oelmekki/activerecord_any_of!44
- Loading branch information
Showing
18 changed files
with
301 additions
and
478 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.bundle/ | ||
log/*.log | ||
pkg/ | ||
Gemfile.lock | ||
gemfiles/*.gemfile.lock | ||
spec/debug.log | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
image: "ruby:latest" | ||
|
||
stages: | ||
- test | ||
|
||
before_script: | ||
- gem install bundler | ||
- bundle install | ||
|
||
rspec: | ||
stage: test | ||
script: | ||
- bundle exec rspec | ||
artifacts: | ||
when: on_failure | ||
paths: | ||
- coverage | ||
|
||
rubocop: | ||
stage: test | ||
script: | ||
- bundle exec rubocop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require: | ||
- rubocop-rspec | ||
- rubocop-rake | ||
|
||
AllCops: | ||
NewCops: enable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,15 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec :path => File.expand_path('../', __FILE__) | ||
|
||
gem 'combustion', :github => 'pat/combustion', :branch => 'master' | ||
# frozen_string_literal: true | ||
|
||
platforms :jruby do | ||
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.0.beta2' | ||
gem 'activerecord-jdbcmysql-adapter', '>= 1.3.0.beta2' | ||
gem 'jdbc-mysql' | ||
gem 'activerecord-jdbcpostgresql-adapter', '>= 1.3.0.beta2' | ||
gem 'jruby-openssl' | ||
end | ||
|
||
platforms :ruby do | ||
gem 'sqlite3' | ||
gem 'mysql2', (MYSQL2_VERSION if defined? MYSQL2_VERSION) | ||
gem 'pg' | ||
end | ||
|
||
platforms :rbx do | ||
gem 'rubysl', '~> 2.0' | ||
gem 'rubysl-test-unit' | ||
end | ||
source 'https://rubygems.org' | ||
|
||
# Add Oracle Adapters | ||
# gem 'ruby-oci8' | ||
# gem 'activerecord-oracle_enhanced-adapter' | ||
gemspec path: File.expand_path(__dir__) | ||
|
||
# Debuggers | ||
gem 'database_cleaner' | ||
gem 'pry' | ||
gem 'pry-nav' | ||
gem 'rake' | ||
gem 'rspec-rails' | ||
gem 'rubocop' | ||
gem 'rubocop-rake' | ||
gem 'rubocop-rspec' | ||
gem 'simplecov' | ||
gem 'sqlite3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,46 +6,67 @@ This gem provides `#any_of` and `#none_of` on ActiveRecord. | |
|
||
`#any_of` is inspired by [any_of from mongoid](http://two.mongoid.org/docs/querying/criteria.html#any_of). | ||
|
||
Its main purpose is to both : | ||
It was released before `#or` was implemented in ActiveRecord. Its main purpose was to both : | ||
|
||
* remove the need to write a sql string when we want an `OR` | ||
* allows to write dynamic `OR` queries, which would be a pain with a string | ||
|
||
It can still be useful today given the various ways you can call it. While | ||
ActiveRecord's `#or` only accepts relations, you can pass to `#any_of` and | ||
`#none_of` the same kind of conditions you would pass to `#where`: | ||
|
||
|
||
```ruby | ||
User.where.any_of({ active: true }, ['offline = ?', required_status], 'posts_count > 0') | ||
``` | ||
|
||
And you can still use relations, like AR's `#or`: | ||
|
||
```ruby | ||
inactive_users = User.not_activated | ||
offline_users = User.offline | ||
|
||
User.where.any_of(inactive_users, offline) | ||
``` | ||
|
||
## Installation | ||
|
||
In your Gemfile : | ||
|
||
``` | ||
gem 'activerecord_any_of' | ||
``` | ||
|
||
## Usage | ||
|
||
### `#any_of` | ||
|
||
It allows to compute an `OR` like query that leverages AR's `#where` syntax: | ||
|
||
|
||
#### basics | ||
|
||
```ruby | ||
User.where.any_of(first_name: 'Joe', last_name: 'Joe') | ||
# => SELECT * FROM users WHERE first_name = 'Joe' OR last_name = 'Joe' | ||
``` | ||
|
||
|
||
#### grouped conditions | ||
|
||
You can separate sets of hash condition by explicitly group them as hashes : | ||
|
||
```ruby | ||
User.where.any_of({first_name: 'John', last_name: 'Joe'}, {first_name: 'Simon', last_name: 'Joe'}) | ||
User.where.any_of({ first_name: 'John', last_name: 'Joe' }, { first_name: 'Simon', last_name: 'Joe' }) | ||
# => SELECT * FROM users WHERE ( first_name = 'John' AND last_name = 'Joe' ) OR ( first_name = 'Simon' AND last_name = 'Joe' ) | ||
``` | ||
|
||
|
||
#### it's plain #where syntax | ||
|
||
Each `#any_of` set is the same kind you would have passed to #where : | ||
|
||
```ruby | ||
Client.where.any_of("orders_count = '2'", ["name = ?", 'Joe'], {email: '[email protected]'}) | ||
Client.where.any_of("orders_count = '2'", ["name = ?", 'Joe'], { email: '[email protected]' }) | ||
``` | ||
|
||
|
||
#### with relations | ||
|
||
You can as well pass `#any_of` to other relations : | ||
|
@@ -54,16 +75,14 @@ You can as well pass `#any_of` to other relations : | |
Client.where("orders_count = '2'").where.any_of({ email: '[email protected]' }, { email: '[email protected]' }) | ||
``` | ||
|
||
|
||
#### with associations | ||
|
||
And with associations : | ||
|
||
```ruby | ||
User.find(1).posts.where.any_of({published: false}, "user_id IS NULL") | ||
User.find(1).posts.where.any_of({ published: false }, 'user_id IS NULL') | ||
``` | ||
|
||
|
||
#### dynamic OR queries | ||
|
||
The best part is that `#any_of` accepts other relations as parameter, to help compute | ||
|
@@ -81,64 +100,6 @@ inactive_users = User.where.any_of(banned_users, unconfirmed_users) | |
|
||
```ruby | ||
banned_users = User.where(banned: true) | ||
unconfirmed_users = User.where("confirmed_at IS NULL") | ||
unconfirmed_users = User.where('confirmed_at IS NULL') | ||
active_users = User.where.none_of(banned_users, unconfirmed_users) | ||
``` | ||
|
||
## Rails-3 | ||
|
||
`activerecord_any_of` uses WhereChain, which has been introduced in rails-4. In | ||
rails-3, simply call `#any_of` and `#none_of` directly, without using `#where` : | ||
|
||
```ruby | ||
manual_removal = User.where(id: params[:users][:destroy_ids]) | ||
User.any_of(manual_removal, "email like '%@example.com'", {banned: true}) | ||
@company.users.any_of(manual_removal, "email like '%@example.com'", {banned: true}) | ||
User.where(offline: false).any_of( manual_removal, "email like '%@example.com'", {banned: true}) | ||
``` | ||
|
||
## Installation | ||
|
||
In your Gemfile : | ||
|
||
``` | ||
gem 'activerecord_any_of' | ||
``` | ||
|
||
Activerecord_any_of supports rails >= 3.2.13 and rails-4 (let me know if tests | ||
pass for rails < 3.2.13, I may edit gem dependencies). | ||
|
||
|
||
## Why not an `#or` method instead ? | ||
|
||
```ruby | ||
User.where( "email LIKE '%@example.com" ).where( active: true ).or( offline: true ) | ||
``` | ||
|
||
What does this query do ? `where (email LIKE '%@example.com' AND active = '1' ) | ||
OR offline = '1'` ? Or `where email LIKE '%@example.com' AND ( active = '1' OR | ||
offline = '1' )` ? This can quickly get messy and counter intuitive. | ||
|
||
The MongoId solution is quite elegant. Using `#any_of`, it is made clear which | ||
conditions are grouped through `OR` and which are grouped through `AND` : | ||
|
||
* `User.where( "email LIKE '%@example.com" ).where.any_of({ active: true }, { offline: true })` | ||
* `fakes = User.where( "email LIKE '%@example.com'" ).where( active: true ); User.where.any_of( fakes, { offline: true })` | ||
|
||
|
||
## Running test | ||
|
||
Testing is done using TravisCI. You can use the wonderful [wwtd gem](https://github.com/grosser/wwtd) to run all tests locally. By default, the task to run is `bundle exec rake spec`, and will run against `sqlite3` in memory. You can change the database like so: `DB=postgresql bundle exec rake spec`. Please note that you may need to change the credentials for your database in the `database.yml` file. *Do not commit those changes.* | ||
|
||
## Pull requests | ||
|
||
This gem is extracted from a pull request made to activerecord core, and | ||
still hope to be merged. So, any pull request here should respects usual | ||
[Rails contributing rules](http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-code) | ||
when it makes sense (especially : coding conventions) to make integration | ||
in source pull request easy. | ||
|
||
|
||
## Licence | ||
|
||
MIT-LICENSE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
require "bundler/gem_tasks" | ||
task :default => :spec | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/gem_tasks' | ||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
$:.push File.expand_path("../lib", __FILE__) | ||
# frozen_string_literal: true | ||
|
||
$LOAD_PATH.push File.expand_path('lib', __dir__) | ||
|
||
# Maintain your gem's version: | ||
require "activerecord_any_of/version" | ||
require 'activerecord_any_of/version' | ||
|
||
# Describe your gem and declare its dependencies: | ||
Gem::Specification.new do |s| | ||
s.name = "activerecord_any_of" | ||
s.name = 'activerecord_any_of' | ||
s.version = ActiverecordAnyOf::VERSION | ||
s.authors = ["Olivier El Mekki"] | ||
s.email = ["[email protected]"] | ||
s.homepage = "https://github.com/oelmekki/activerecord_any_of" | ||
s.authors = ['Olivier El Mekki'] | ||
s.email = ['[email protected]'] | ||
s.homepage = 'https://github.com/oelmekki/activerecord_any_of' | ||
s.summary = "Mongoid's any_of like implementation for activerecord" | ||
s.description = "Any_of allows to compute dynamic OR queries." | ||
s.description = 'Any_of allows to compute dynamic OR queries.' | ||
s.license = 'MIT' | ||
s.required_ruby_version = '>= 2.7.0' | ||
|
||
s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"] | ||
s.test_files = Dir["spec/**/*"] | ||
s.files = Dir['{app,config,db,lib}/**/*'] + ['MIT-LICENSE', 'Rakefile', 'README.md'] | ||
|
||
s.add_dependency "activerecord", ">= 3.2.13", '< 6' | ||
s.add_dependency 'activerecord', '>= 7', '< 8' | ||
|
||
s.add_development_dependency 'rspec-rails', '~> 2.12' | ||
s.add_development_dependency 'rake', '~> 10' | ||
s.add_development_dependency 'combustion', '>= 0.5.1' | ||
s.add_development_dependency 'database_cleaner' | ||
s.metadata['rubygems_mfa_required'] = 'true' | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.