Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add includes, preload, and eager_load to compat #75

Merged
merged 1 commit into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lib/baby_squeel/compat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@ def self.enable!
::ActiveRecord::Relation.prepend QueryMethods
end

class KeyPath
def self.evaluate(&block)
if block.arity.zero?
unwrap new.instance_eval(&block)
else
unwrap yield(new)
end
end

def self.unwrap(path)
if path.kind_of? self
path.value
elsif path.respond_to? :map
path.map(&method(:unwrap))
else
[]
end
end

def initialize(*path)
@path = path
end

def value
@path.reverse.reduce({}) do |acc, name|
{ name => acc }
end
end

private

def respond_to_missing?(*)
true
end

def method_missing(name, *)
self.class.new(*@path, name)
end
end

module DSL
# An alias for BabySqueel::DSL#sql
def `(str)
Expand Down Expand Up @@ -36,6 +76,33 @@ def joins(*args, &block)
end
end

# Overrides ActiveRecord::QueryMethods#includes
def includes(*args, &block)
if block_given? && args.empty?
super KeyPath.evaluate(&block)
else
super
end
end

# Overrides ActiveRecord::QueryMethods#eager_load
def eager_load(*args, &block)
if block_given? && args.empty?
super KeyPath.evaluate(&block)
else
super
end
end

# Overrides ActiveRecord::QueryMethods#preload
def preload(*args, &block)
if block_given? && args.empty?
super KeyPath.evaluate(&block)
else
super
end
end

# Heads up, Array#select conflicts with
# ActiveRecord::QueryMethods#select. So, if arity
# is given to the block, we'll use Array#select.
Expand Down
33 changes: 33 additions & 0 deletions spec/baby_squeel/compat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,36 @@
end
end
end

describe 'BabySqueel::Compat::QueryMethods', :compat do
describe '#includes' do
it 'accepts a block' do
posts = Post.includes { author.posts }
expect(posts.includes_values).to eq([ author: { posts: {} } ])
end

it 'handles arrays' do
posts = Post.includes { [comments, author.posts] }
expect(posts.includes_values).to eq([ { comments: {} }, author: { posts: {} } ])
end

it 'handles nil' do
posts = Post.includes { nil }
expect(posts.includes_values).to eq([])
end
end

describe '#eager_load' do
it 'accepts a block' do
posts = Post.eager_load { author.posts }
expect(posts.eager_load_values).to eq([ author: { posts: {} } ])
end
end

describe '#preload' do
it 'accepts a block' do
posts = Post.preload { author.posts }
expect(posts.preload_values).to eq([ author: { posts: {} } ])
end
end
end