Skip to content

Instantly share code, notes, and snippets.

@jfirebaugh
Created April 14, 2011 02:55
Show Gist options
  • Save jfirebaugh/918818 to your computer and use it in GitHub Desktop.
Save jfirebaugh/918818 to your computer and use it in GitHub Desktop.
From 9af3823ff8ca10ab1c604628ec7f8601e43c9367 Mon Sep 17 00:00:00 2001
From: John Firebaugh <[email protected]>
Date: Wed, 13 Apr 2011 19:54:38 -0700
Subject: [PATCH] Add associated columns later to avoid eager loading callback needing to reselect them.
---
lib/sequel/model/associations.rb | 11 ++++++-----
spec/model/eager_loading_spec.rb | 2 +-
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/lib/sequel/model/associations.rb b/lib/sequel/model/associations.rb
index a334ec8..c454be8 100644
--- a/lib/sequel/model/associations.rb
+++ b/lib/sequel/model/associations.rb
@@ -683,17 +683,18 @@ module Sequel
if opts[:eager_graph]
ds = ds.eager_graph(opts[:eager_graph])
ds = ds.add_graph_aliases(opts.associated_key_alias=>[opts.associated_class.table_name, opts.associated_key_alias, SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column)]) if opts.eager_loading_use_associated_key?
- elsif opts.eager_loading_use_associated_key?
+ end
+ ds = ds.eager(associations) unless Array(associations).empty?
+ ds = opts[:eager_block].call(ds) if opts[:eager_block]
+ ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
+ if !opts[:eager_graph] && opts.eager_loading_use_associated_key?
ds = if opts[:uses_left_composite_keys]
t = opts.associated_key_table
ds.select_more(*opts.associated_key_alias.zip(opts.associated_key_column).map{|a, c| SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(t, c), a)})
else
- ds.select_more(SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column), opts.associated_key_alias))
+ ds.select_more(SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column), opts.associated_key_alias))
end
end
- ds = ds.eager(associations) unless Array(associations).empty?
- ds = opts[:eager_block].call(ds) if opts[:eager_block]
- ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
ds
end
diff --git a/spec/model/eager_loading_spec.rb b/spec/model/eager_loading_spec.rb
index ee1552c..3bc9a0c 100644
--- a/spec/model/eager_loading_spec.rb
+++ b/spec/model/eager_loading_spec.rb
@@ -685,7 +685,7 @@ describe Sequel::Model, "#eager" do
end
it "should eagerly load a many_to_many association with custom eager block" do
- a = EagerAlbum.eager(:genres => proc {|ds| ds.select(:name, ds.opts[:select].last)}).all
+ a = EagerAlbum.eager(:genres => proc {|ds| ds.select(:name)}).all
a.should be_a_kind_of(Array)
a.size.should == 1
a.first.should be_a_kind_of(EagerAlbum)
--
1.7.4.3
From e26bbf3b9a419d79802f712b52d0120125e09ae4 Mon Sep 17 00:00:00 2001
From: John Firebaugh <[email protected]>
Date: Wed, 13 Apr 2011 19:52:22 -0700
Subject: [PATCH] Call eager loading callback earlier to avoid it needing to reselect associated key columns.
---
lib/sequel/model/associations.rb | 4 ++--
spec/model/eager_loading_spec.rb | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/lib/sequel/model/associations.rb b/lib/sequel/model/associations.rb
index a334ec8..83f523b 100644
--- a/lib/sequel/model/associations.rb
+++ b/lib/sequel/model/associations.rb
@@ -680,6 +680,8 @@ module Sequel
ds = ds.order(*opts[:order]) if opts[:order]
ds = ds.eager(opts[:eager]) if opts[:eager]
ds = ds.distinct if opts[:distinct]
+ ds = opts[:eager_block].call(ds) if opts[:eager_block]
+ ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
if opts[:eager_graph]
ds = ds.eager_graph(opts[:eager_graph])
ds = ds.add_graph_aliases(opts.associated_key_alias=>[opts.associated_class.table_name, opts.associated_key_alias, SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column)]) if opts.eager_loading_use_associated_key?
@@ -692,8 +694,6 @@ module Sequel
end
end
ds = ds.eager(associations) unless Array(associations).empty?
- ds = opts[:eager_block].call(ds) if opts[:eager_block]
- ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
ds
end
diff --git a/spec/model/eager_loading_spec.rb b/spec/model/eager_loading_spec.rb
index ee1552c..3bc9a0c 100644
--- a/spec/model/eager_loading_spec.rb
+++ b/spec/model/eager_loading_spec.rb
@@ -685,7 +685,7 @@ describe Sequel::Model, "#eager" do
end
it "should eagerly load a many_to_many association with custom eager block" do
- a = EagerAlbum.eager(:genres => proc {|ds| ds.select(:name, ds.opts[:select].last)}).all
+ a = EagerAlbum.eager(:genres => proc {|ds| ds.select(:name)}).all
a.should be_a_kind_of(Array)
a.size.should == 1
a.first.should be_a_kind_of(EagerAlbum)
--
1.7.4.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment