Skip to content

Commit

Permalink
Merge pull request #87 from rzane/52-support
Browse files Browse the repository at this point in the history
Add support for Active Record 5.2
  • Loading branch information
rzane authored Apr 25, 2018
2 parents b165a50 + 34e07ba commit f6d11f5
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ env:
- AR=4.2.10
- AR=5.0.6
- AR=5.1.5
- AR=5.2.0
- AR=latest
- AR=4.2.10 COMPAT=1
- AR=5.0.6 COMPAT=1
- AR=5.1.5 COMPAT=1
- AR=5.2.0 COMPAT=1
- AR=latest COMPAT=1
12 changes: 9 additions & 3 deletions lib/baby_squeel/active_record/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ def when_having(&block)
# This is a monkey patch, and I'm not happy about it.
# Active Record will call `group_by` on the `joins`. The
# Injector has a custom `group_by` method that handles
# BabySqueel::JoinExpression nodes.
def build_joins(manager, joins)
super manager, BabySqueel::JoinDependency::Injector.new(joins)
# BabySqueel::Join nodes.
if ::ActiveRecord::VERSION::MAJOR >= 5 && ::ActiveRecord::VERSION::MINOR >= 2
def build_joins(manager, joins, aliases)
super manager, BabySqueel::JoinDependency::Injector.new(joins), aliases
end
else
def build_joins(manager, joins)
super manager, BabySqueel::JoinDependency::Injector.new(joins)
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/baby_squeel/__snapshots__/association_spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
BabySqueel::Association#== generates SQL 1: '"posts"."author_id" = 42'
BabySqueel::Association#!= generates SQL 1: ("posts"."author_id" != 42)
'BabySqueel::Association#== generates SQL 1 (Active Record: v5.2)': '"posts"."author_id"
= ?'
'BabySqueel::Association#!= generates SQL 1 (Active Record: v5.2)': '"posts"."author_id"
!= ?'
8 changes: 4 additions & 4 deletions spec/baby_squeel/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
subject(:association) { create_association Post, :author }

if ActiveRecord::VERSION::MAJOR >= 5
it 'returns an wrapped Arel::Nodes::And' do
it 'generates SQL' do
node = association == Author.new(id: 42)
expect(node._arel.to_sql).to eq('"posts"."author_id" = 42')
expect(node._arel.to_sql).to match_sql_snapshot(variants: ['5.2'])
end

it 'throws for an invalid comparison' do
Expand All @@ -59,9 +59,9 @@
subject(:association) { create_association Post, :author }

if ActiveRecord::VERSION::MAJOR >= 5
it 'returns some wrapped arel' do
it 'generates SQL' do
node = association != Author.new(id: 42)
expect(node._arel.to_sql).to eq('("posts"."author_id" != 42)')
expect(node._arel.to_sql).to match_sql_snapshot(variants: ['5.2'])
end

it 'throws for an invalid comparison' do
Expand Down
8 changes: 8 additions & 0 deletions spec/integration/__snapshots__/sifting_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ BabySqueel::ActiveRecord::Base#sifter yield the root table to the block when ari
INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER JOIN "comments"
"author_comments_posts" ON "author_comments_posts"."author_id" = "authors"."id"
WHERE ("author_comments_posts"."id" > 1)
'BabySqueel::ActiveRecord::Base#sifter allows the use of a sifter on an association 1 (Active Record: v5.2)': SELECT
"posts".* FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
WHERE "authors"."name" LIKE 'boogies%'
'BabySqueel::ActiveRecord::Base#sifter yield the root table to the block when arity is given 1 (Active Record: v5.2)': SELECT
"posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER JOIN "comments"
"author_comments_posts" ON "author_comments_posts"."author_id" = "authors"."id"
WHERE "author_comments_posts"."id" > 1
13 changes: 13 additions & 0 deletions spec/integration/__snapshots__/when_having_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@
FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER
JOIN "posts" "posts_authors" ON "posts_authors"."author_id" = "authors"."id" GROUP
BY "posts_authors"."id" HAVING (COUNT("posts_authors"."id") > 5)
"#when_having adds a having clause 1 (Active Record: v5.2)": SELECT COUNT("posts"."id")
FROM "posts" GROUP BY "posts"."author_id" HAVING COUNT("posts"."id") > 5
"#when_having adds a having clause with a calculation 1 (Active Record: v5.2)": SELECT
COUNT("posts"."id") FROM "posts" GROUP BY ("posts"."author_id" + 5) * 3 HAVING COUNT("posts"."id")
> 5
"#when_having adds a having clause with an association 1 (Active Record: v5.2)": SELECT
COUNT("posts"."id") FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
GROUP BY "authors"."id" HAVING COUNT("authors"."id") > 5
"#when_having adds a having clause with an aliased table 1 (Active Record: v5.2)": SELECT
COUNT("posts_authors"."id") FROM "posts" INNER JOIN "authors" ON "authors"."id"
= "posts"."author_id" INNER JOIN "posts" "posts_authors" ON "posts_authors"."author_id"
= "authors"."id" GROUP BY "posts_authors"."id" HAVING COUNT("posts_authors"."id")
> 5
29 changes: 29 additions & 0 deletions spec/integration/__snapshots__/where_chain_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,32 @@
("posts"."author_id" = 42)
"#where.has wheres an association using #!= 1": SELECT "posts".* FROM "posts" WHERE
(("posts"."author_id" != 42))
"#where.has wheres on deep associations 1 (Active Record: v5.2)": SELECT "posts".*
FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER
JOIN "comments" ON "comments"."author_id" = "authors"."id" WHERE "comments"."id"
> 0
"#where.has wheres on an aliased association 1 (Active Record: v5.2)": SELECT "posts".*
FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER
JOIN "posts" "posts_authors" ON "posts_authors"."author_id" = "authors"."id" WHERE
"posts_authors"."id" > 0
"#where.has wheres on an aliased association with through 1 (Active Record: v5.2)": SELECT
"posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER JOIN "comments"
"author_comments_posts" ON "author_comments_posts"."author_id" = "authors"."id"
WHERE "author_comments_posts"."id" > 0
"#where.has wheres on an alias with a function 1 (Active Record: v5.2)": SELECT "posts".*
FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER
JOIN "posts" "posts_authors" ON "posts_authors"."author_id" = "authors"."id" WHERE
coalesce("posts_authors"."id", 1) > 0
"#where.has wheres with a not in subquery 1 (Active Record: v5.2)": SELECT "posts".*
FROM "posts" WHERE "posts"."author_id" NOT IN (SELECT "authors"."id" FROM "authors"
WHERE (1=0))
"#where.has builds an exists query 1 (Active Record: v5.2)": SELECT "posts".* FROM
"posts" WHERE EXISTS(SELECT "posts".* FROM "posts" WHERE "posts"."author_id" = 1)
"#where.has builds a not exists query 1 (Active Record: v5.2)": SELECT "posts".* FROM
"posts" WHERE NOT EXISTS(SELECT "posts".* FROM "posts" WHERE "posts"."author_id"
= 1)
"#where.has wheres an association using #== 1 (Active Record: v5.2)": SELECT "posts".*
FROM "posts" WHERE "posts"."author_id" = 42
"#where.has wheres an association using #!= 1 (Active Record: v5.2)": SELECT "posts".*
FROM "posts" WHERE "posts"."author_id" != 42
12 changes: 12 additions & 0 deletions spec/integration/joining_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,20 @@

describe 'polymorphism' do
it 'inner joins' do
if ActiveRecord::VERSION::STRING >= '5.2.0'
pending "polyamorous's support for polymorphism is broken"
end

relation = Picture.joining { imageable.of(Post) }

expect(relation).to match_sql_snapshot
end

it 'outer joins' do
if ActiveRecord::VERSION::STRING >= '5.2.0'
pending "polyamorous's support for polymorphism is broken"
end

relation = Picture.joining { imageable.of(Post).outer }

expect(relation).to match_sql_snapshot
Expand Down Expand Up @@ -156,6 +164,10 @@
end

it 'handles polymorphism' do
if ActiveRecord::VERSION::STRING >= '5.2.0'
pending "polyamorous's support for polymorphism is broken"
end

relation = Picture.joining { imageable.of(Post).comments }

expect(relation).to match_sql_snapshot
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/sifting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
author.sift :boogers
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'yield the root table to the block when arity is given' do
relation = Post.joins(:comments, :author_comments).where.has {
sift :author_comments_id, 1
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end
end
8 changes: 4 additions & 4 deletions spec/integration/when_having_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
.grouping { author_id }
.when_having { id.count > 5 }

expect(relation).to match_sql_snapshot(variants: ['4.2'])
expect(relation).to match_sql_snapshot(variants: ['4.2', '5.2'])
end

it 'adds a having clause with a calculation' do
relation = Post.selecting { id.count }
.grouping { (author_id + 5 ) * 3 }
.when_having { id.count > 5 }

expect(relation).to match_sql_snapshot(variants: ['4.2'])
expect(relation).to match_sql_snapshot(variants: ['4.2', '5.2'])
end

it 'adds a having clause with an association' do
Expand All @@ -23,7 +23,7 @@
.grouping { author.id }
.when_having { author.id.count > 5 }

expect(relation).to match_sql_snapshot(variants: ['4.2'])
expect(relation).to match_sql_snapshot(variants: ['4.2', '5.2'])
end

it 'adds a having clause with an aliased table' do
Expand All @@ -32,6 +32,6 @@
.grouping { author.posts.id }
.when_having { author.posts.id.count > 5 }

expect(relation).to match_sql_snapshot(variants: ['4.2'])
expect(relation).to match_sql_snapshot(variants: ['4.2', '5.2'])
end
end
26 changes: 17 additions & 9 deletions spec/integration/where_chain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,30 @@
author.comments.id > 0
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'wheres on an aliased association' do
relation = Post.joins(author: :posts).where.has {
author.posts.id > 0
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'wheres on an aliased association with through' do
relation = Post.joins(:comments, :author_comments).where.has {
author_comments.id > 0
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'wheres on polymorphic associations' do
if ActiveRecord::VERSION::STRING >= '5.2.0'
pending "polyamorous's support for polymorphism is broken"
end

relation = Picture.joining { imageable.of(Post) }.where.has {
imageable.of(Post).title =~ 'meatloaf'
}
Expand All @@ -78,6 +82,10 @@
end

it 'wheres on polymorphic associations outer join' do
if ActiveRecord::VERSION::STRING >= '5.2.0'
pending "polyamorous's support for polymorphism is broken"
end

relation = Picture.joining { imageable.of(Post).outer }.where.has {
imageable.of(Post).title =~ 'meatloaf'
}
Expand Down Expand Up @@ -106,7 +114,7 @@
coalesce(author.posts.id, 1) > 0
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'wheres with a subquery' do
Expand Down Expand Up @@ -141,7 +149,7 @@
author_id.not_in Author.none.select(:id)
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'wheres using a simple table' do
Expand All @@ -163,15 +171,15 @@
exists Post.where.has { author_id == 1 }
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'builds a not exists query' do
relation = Post.where.has {
not_exists Post.where.has { author_id == 1 }
}

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'wheres an association using #==' do
Expand All @@ -184,7 +192,7 @@
post.author == author
end

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'wheres an association using #!=' do
Expand All @@ -197,7 +205,7 @@
post.author != author
end

expect(relation).to match_sql_snapshot
expect(relation).to match_sql_snapshot(variants: ['5.2'])
end

it 'handles a hash' do
Expand Down

0 comments on commit f6d11f5

Please sign in to comment.