-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from kollegorna/feat/or_filter
Add OR filter
- Loading branch information
Showing
8 changed files
with
193 additions
and
32 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
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
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
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
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,3 +1,3 @@ | ||
module ActiveHashRelation | ||
VERSION = "1.1.0" | ||
VERSION = "1.2.0" | ||
end |
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
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,98 @@ | ||
describe ActiveHashRelation do | ||
include Helpers | ||
|
||
if ActiveRecord::VERSION::MAJOR < 5 | ||
context 'OR filter' do | ||
it "one OR clause" do | ||
logger = double('logger') | ||
allow(logger).to receive(:warn) | ||
allow(Rails).to receive(:logger).and_return(logger) | ||
|
||
hash = {or: [{name: 'Filippos'}, {name: 'Vasilis'}]} | ||
|
||
HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expect(logger).to have_received(:warn) | ||
end | ||
end | ||
|
||
else | ||
context 'OR filter' do | ||
it "one OR clause" do | ||
hash = {or: [{name: 'Filippos'}, {name: 'Vasilis'}]} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users", | ||
"WHERE ((users.name = 'Filippos') OR (users.name = 'Vasilis'))" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
it "multiple OR clauses" do | ||
hash = {or: [{or: [{name: 'Filippos'}, {name: 'Vasilis'}]}, {or: [{id: 1}, {id: 2}]}]} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users", | ||
"WHERE", | ||
"(", | ||
"((users.name = 'Filippos') OR (users.name = 'Vasilis'))", | ||
"OR", | ||
"((users.id = 1) OR (users.id = 2))", | ||
")" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
it "one complex OR clause" do | ||
hash = {or: [{name: 'Filippos', token: '123'}, {name: 'Vasilis'}]} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users", | ||
"WHERE", | ||
"(", | ||
"(users.name = 'Filippos') AND (users.token = '123')", | ||
"OR", | ||
"(users.name = 'Vasilis')", | ||
")" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
it "nested OR clause" do | ||
hash = {or: [{or: [{name: 'Filippos'}, {token: '123'}]}, {name: 'Vasilis'}]} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users", | ||
"WHERE", | ||
"(", | ||
"((users.name = 'Filippos') OR (users.token = '123'))", | ||
"OR", | ||
"(users.name = 'Vasilis')", | ||
")" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
it "OR clause on associations" do | ||
hash = {microposts: {or: [{content: 'Sveavägen 4'}, {id: 1}]}} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users", | ||
"INNER JOIN microposts ON microposts.user_id = users.id", | ||
"WHERE ((microposts.content = 'Sveavägen 4') OR (microposts.id = 1))" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
end | ||
end | ||
end | ||
|
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,29 @@ | ||
describe ActiveHashRelation do | ||
include Helpers | ||
|
||
context 'primary_key' do | ||
it "one key" do | ||
hash = {id: 1} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users WHERE (users.id = 1)" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
it "multiple keys" do | ||
hash = {id: [1,2,3,4]} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users WHERE (users.id IN (1, 2, 3, 4))" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
end | ||
end | ||
|
||
|