diff --git a/.travis.yml b/.travis.yml index da915d1..191dbff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/lib/baby_squeel/active_record/query_methods.rb b/lib/baby_squeel/active_record/query_methods.rb index ad0c30c..a9c585a 100644 --- a/lib/baby_squeel/active_record/query_methods.rb +++ b/lib/baby_squeel/active_record/query_methods.rb @@ -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 diff --git a/spec/baby_squeel/__snapshots__/association_spec.yaml b/spec/baby_squeel/__snapshots__/association_spec.yaml new file mode 100644 index 0000000..1be18ae --- /dev/null +++ b/spec/baby_squeel/__snapshots__/association_spec.yaml @@ -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" + != ?' diff --git a/spec/baby_squeel/association_spec.rb b/spec/baby_squeel/association_spec.rb index 5e0894e..1e0bc24 100644 --- a/spec/baby_squeel/association_spec.rb +++ b/spec/baby_squeel/association_spec.rb @@ -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 @@ -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 diff --git a/spec/integration/__snapshots__/sifting_spec.yaml b/spec/integration/__snapshots__/sifting_spec.yaml index efcd948..82274c7 100644 --- a/spec/integration/__snapshots__/sifting_spec.yaml +++ b/spec/integration/__snapshots__/sifting_spec.yaml @@ -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 diff --git a/spec/integration/__snapshots__/when_having_spec.yaml b/spec/integration/__snapshots__/when_having_spec.yaml index 83b1c00..ede5e55 100644 --- a/spec/integration/__snapshots__/when_having_spec.yaml +++ b/spec/integration/__snapshots__/when_having_spec.yaml @@ -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 diff --git a/spec/integration/__snapshots__/where_chain_spec.yaml b/spec/integration/__snapshots__/where_chain_spec.yaml index 45f77e4..fe6ff39 100644 --- a/spec/integration/__snapshots__/where_chain_spec.yaml +++ b/spec/integration/__snapshots__/where_chain_spec.yaml @@ -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 diff --git a/spec/integration/joining_spec.rb b/spec/integration/joining_spec.rb index a53c2e4..3a2fd24 100644 --- a/spec/integration/joining_spec.rb +++ b/spec/integration/joining_spec.rb @@ -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 @@ -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 diff --git a/spec/integration/sifting_spec.rb b/spec/integration/sifting_spec.rb index f3c6ac0..8148133 100644 --- a/spec/integration/sifting_spec.rb +++ b/spec/integration/sifting_spec.rb @@ -34,7 +34,7 @@ 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 @@ -42,6 +42,6 @@ sift :author_comments_id, 1 } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end end diff --git a/spec/integration/when_having_spec.rb b/spec/integration/when_having_spec.rb index e44f189..eb21959 100644 --- a/spec/integration/when_having_spec.rb +++ b/spec/integration/when_having_spec.rb @@ -6,7 +6,7 @@ .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 @@ -14,7 +14,7 @@ .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 @@ -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 @@ -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 diff --git a/spec/integration/where_chain_spec.rb b/spec/integration/where_chain_spec.rb index cabf5b5..3ba8344 100644 --- a/spec/integration/where_chain_spec.rb +++ b/spec/integration/where_chain_spec.rb @@ -50,7 +50,7 @@ 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 @@ -58,7 +58,7 @@ 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 @@ -66,10 +66,14 @@ 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' } @@ -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' } @@ -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 @@ -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 @@ -163,7 +171,7 @@ 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 @@ -171,7 +179,7 @@ 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 @@ -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 @@ -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