-
Notifications
You must be signed in to change notification settings - Fork 21.7k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,5 +75,9 @@ def calculate(operation, _column_name) | |
def exists?(_id = false) | ||
false | ||
end | ||
|
||
def or(other) | ||
other.spawn | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require "cases/helper" | ||
require 'models/post' | ||
|
||
module ActiveRecord | ||
class OrTest < ActiveRecord::TestCase | ||
fixtures :posts | ||
|
||
def test_or_with_relation | ||
expected = Post.where('id = 1 or id = 2').to_a | ||
assert_equal expected, Post.where('id = 1').or(Post.where('id = 2')).to_a | ||
end | ||
|
||
def test_or_identity | ||
expected = Post.where('id = 1').to_a | ||
assert_equal expected, Post.where('id = 1').or(Post.where('id = 1')).to_a | ||
end | ||
|
||
def test_or_with_null_left | ||
expected = Post.where('id = 1').to_a | ||
assert_equal expected, Post.none.or(Post.where('id = 1')).to_a | ||
end | ||
|
||
def test_or_with_null_right | ||
expected = Post.where('id = 1').to_a | ||
assert_equal expected, Post.where('id = 1').or(Post.none).to_a | ||
end | ||
|
||
def test_or_with_bind_params | ||
assert_equal Post.find([1, 2]), Post.where(id: 1).or(Post.where(id: 2)).to_a | ||
end | ||
|
||
def test_or_with_null_both | ||
expected = Post.none.to_a | ||
assert_equal expected, Post.none.or(Post.none).to_a | ||
end | ||
|
||
def test_or_without_left_where | ||
expected = Post.where('id = 1') | ||
assert_equal expected, Post.or(Post.where('id = 1')).to_a | ||
end | ||
|
||
def test_or_without_right_where | ||
expected = Post.where('id = 1') | ||
assert_equal expected, Post.where('id = 1').or(Post.all).to_a | ||
end | ||
|
||
def test_or_preserves_other_querying_methods | ||
expected = Post.where('id = 1 or id = 2 or id = 3').order('body asc').to_a | ||
partial = Post.order('body asc') | ||
assert_equal expected, partial.where('id = 1').or(partial.where(:id => [2, 3])).to_a | ||
assert_equal expected, Post.order('body asc').where('id = 1').or(Post.order('body asc').where(:id => [2, 3])).to_a | ||
end | ||
|
||
def test_or_with_incompatible_relations | ||
assert_raises ArgumentError do | ||
Post.order('body asc').where('id = 1').or(Post.order('id desc').where(:id => [2, 3])).to_a | ||
end | ||
end | ||
|
||
def test_or_when_grouping | ||
groups = Post.where('id < 10').group('body').select('body, COUNT(*) AS c') | ||
expected = groups.having("COUNT(*) > 1 OR body like 'Such%'").to_a.map {|o| [o.body, o.c] } | ||
assert_equal expected, groups.having('COUNT(*) > 1').or(groups.having("body like 'Such%'")).to_a.map {|o| [o.body, o.c] } | ||
end | ||
|
||
def test_or_with_named_scope | ||
expected = Post.where("id = 1 or body LIKE '\%a\%'").to_a | ||
assert_equal expected, Post.where('id = 1').or(Post.containing_the_letter_a) | ||
end | ||
|
||
def test_or_inside_named_scope | ||
expected = Post.where("body LIKE '\%a\%' OR title LIKE ?", "%'%").order('id DESC').to_a | ||
assert_equal expected, Post.order(id: :desc).typographically_interesting | ||
end | ||
|
||
def test_or_on_loaded_relation | ||
expected = Post.where('id = 1 or id = 2').to_a | ||
p = Post.where('id = 1') | ||
p.load | ||
assert_equal p.loaded?, true | ||
assert_equal expected, p.or(Post.where('id = 2')).to_a | ||
end | ||
end | ||
end |
34 comments
on commit 9e42cf0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Gael, Olivier, Matthew and Sean for your work on this π looking forward to giving it a try with scopes/class methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am surprised at what a small commit this is!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because I've been refactoring to make changes like this trivial for a few weeks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π π Been waiting for this for a few years. Thanks guys!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome π One of the most required features in active_record :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about combining and
and or
? How should one do that? Can we nest or's and and's togheter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
completely awesome! π π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JanStevens Yes, you can combine as many operations as you'd like, just like all the other methods on Relation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π π π π π π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome - excited to use this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm newb, please someboy tell me how can i use this feature? shall i update smthing (im newb to Rails). thanks! p.s. i was just astonished that Rails didnt have OR operator for ORM/ and that's a really great commit!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@classyPimp This will be part of Rails 5.0, which will ship in late 2015.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The definition of scope :typographically_interesting
using two other scopes is really interesting. Is there a way to combine two scopes using AND? If not, an .and
method would be useful, tooβ¦
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@korny Post.typographcially_interesting.containing_the_letter_a
would combine them with AND. Or to be more specific, where
adds conditions using AND.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I was missing the obvious π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π definitely gonna use this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, I've backported this to Rails 4.2.3 and am sharing since it was non-trivial https://gist.github.com/bf4/84cff9cc6ac8489d769e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to π this commit, folks -- everyone who has commented or who has starred this repo gets a notification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not cool bro π’