Skip to content

Commit

Permalink
Rename JoinExpression to Join
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Zane committed Mar 30, 2018
1 parent 103cd9e commit 3291511
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 41 deletions.
4 changes: 2 additions & 2 deletions lib/baby_squeel/active_record/query_methods.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'baby_squeel/dsl'
require 'baby_squeel/join_dependency/injector'
require 'baby_squeel/join_dependency'

module BabySqueel
module ActiveRecord
Expand Down Expand Up @@ -39,7 +39,7 @@ 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.
# 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
Expand Down
2 changes: 1 addition & 1 deletion lib/baby_squeel/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def needs_polyamorous?
_join == Arel::Nodes::OuterJoin || _reflection.polymorphic?
end

# See JoinExpression#add_to_tree.
# See Join#add_to_tree.
def add_to_tree(hash)
polyamorous = Polyamorous::Join.new(
_reflection.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module BabySqueel
# This is the thing that gets added to Active Record's joins_values.
# By including Polyamorous::TreeNode, when this instance is found when
# traversing joins in ActiveRecord::Associations::JoinDependency::walk_tree,
# JoinExpression#add_to_tree will be called.
class JoinExpression
# Join#add_to_tree will be called.
class Join
include Polyamorous::TreeNode

def initialize(associations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@

module BabySqueel
module JoinDependency
# Unfortunately, this is mostly all duplication of
# ActiveRecord::QueryMethods#build_joins
# This class allows BabySqueel to slip custom
# joins_values into Active Record's JoinDependency
class Injector < Array # :nodoc:
# Active Record will call group_by on this object
# in ActiveRecord::QueryMethods#build_joins. This
# allows BabySqueel::Joins to be treated
# like typical join hashes until Polyamorous can
# deal with them.
def group_by
super do |join|
case join
when BabySqueel::Join
:association_join
else
yield join
end
end
end
end

class Builder # :nodoc:
attr_reader :join_dependency

def initialize(relation)
@join_dependency = ::JoinDependency.from_relation(relation) do |join|
:association_join if join.kind_of? BabySqueel::JoinExpression
:association_join if join.kind_of? BabySqueel::Join
end
end

Expand Down
23 changes: 0 additions & 23 deletions lib/baby_squeel/join_dependency/injector.rb

This file was deleted.

8 changes: 4 additions & 4 deletions lib/baby_squeel/table.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'baby_squeel/resolver'
require 'baby_squeel/join_expression'
require 'baby_squeel/join_dependency/builder'
require 'baby_squeel/join'
require 'baby_squeel/join_dependency'

module BabySqueel
class Table
Expand Down Expand Up @@ -96,13 +96,13 @@ def find_alias(associations = [])
# 2. Implicit join without using an outer join. In this case, we'll just
# give a hash to Active Record, and join the normal way.
# 3. Implicit join using an outer join. In this case, we need to use
# Polyamorous to build the join. We'll return a JoinExpression.
# Polyamorous to build the join. We'll return a Join.
#
def _arel(associations = [])
if _on
_join.new(_table, Arel::Nodes::On.new(_on))
elsif associations.any?(&:needs_polyamorous?)
JoinExpression.new(associations)
Join.new(associations)
elsif associations.any?
associations.reverse.inject({}) do |names, assoc|
{ assoc._reflection.name => names }
Expand Down
6 changes: 3 additions & 3 deletions spec/baby_squeel/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def make_tree(tree_node)
expect(assoc._arel).to be_an(Arel::Nodes::InnerJoin)
end

it 'sets an on clause on the JoinExpression' do
it 'sets an on clause on the Join' do
expect(assoc._on).not_to be_nil
end

Expand All @@ -144,8 +144,8 @@ def make_tree(tree_node)
end

context 'when outer joining' do
it 'resolves to a JoinExpression' do
expect(association.outer._arel).to be_a(BabySqueel::JoinExpression)
it 'resolves to a Join' do
expect(association.outer._arel).to be_a(BabySqueel::Join)
end

it 'throws a fit when an alias is attempted' do
Expand Down
2 changes: 1 addition & 1 deletion spec/baby_squeel/join_dependency/injector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe BabySqueel::JoinDependency::Injector do
let(:join_path) {
BabySqueel::JoinExpression.new([])
BabySqueel::Join.new([])
}

let(:joins_values) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe BabySqueel::JoinExpression do
describe BabySqueel::Join do
let(:association1) { create_association Author, :posts }
let(:association2) { create_association Post, :comments }

Expand Down
2 changes: 1 addition & 1 deletion spec/baby_squeel/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

context 'when outer joining' do
let(:association) { create_association(Author, :posts).outer }
specify { is_expected.to be_a(BabySqueel::JoinExpression) }
specify { is_expected.to be_a(BabySqueel::Join) }
end

context 'when not joining' do
Expand Down

0 comments on commit 3291511

Please sign in to comment.