This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
Make Squeel compatible with Rails 4.1 and Rails 4.2.0.alpha #317
Merged
ernie
merged 7 commits into
activerecord-hackery:master
from
bigxiang:ruby-2-rails-edge
Jul 15, 2014
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d4cba87
Add compatibility to Ruby 2.0+ with Rails 4.1 and 4.2
bigxiang 72c153b
Remove shared examples, it should be clearer to review
bigxiang 7b79bbd
Enable using subqueries in joins, a better way to fix #309
bigxiang 2ed5698
Remove things should be placed into Polyamorous.
bigxiang 5fdff20
Remove all join dependency stuff
bigxiang d325c56
Squeel can bind params for CollectionProxy in subquery correctly afte…
bigxiang f6017fa
Add a spec to prove original AR behavior not be broken. #313
bigxiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
Gemfile.lock | ||
pkg/* | ||
.rvmrc | ||
.ruby-version | ||
.ruby-gemset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--color | ||
--format documentation | ||
--backtrace | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
lib/squeel/adapters/active_record/3.0/join_dependency_extensions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'squeel/adapters/active_record/join_dependency_extensions' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
lib/squeel/adapters/active_record/3.1/join_dependency_extensions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'squeel/adapters/active_record/join_dependency_extensions' |
1 change: 1 addition & 0 deletions
1
lib/squeel/adapters/active_record/3.2/join_dependency_extensions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'squeel/adapters/active_record/join_dependency_extensions' |
1 change: 1 addition & 0 deletions
1
lib/squeel/adapters/active_record/4.0/join_dependency_extensions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'squeel/adapters/active_record/join_dependency_extensions' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'squeel/adapters/active_record/compat' | ||
|
||
module ActiveRecord | ||
module Associations | ||
class AssociationScope | ||
def eval_scope(klass, scope, owner) | ||
if scope.is_a?(Relation) | ||
scope | ||
else | ||
klass.unscoped.instance_exec(owner, &scope).visited | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require 'squeel/adapters/active_record/context' | ||
|
||
module Squeel | ||
module Adapters | ||
module ActiveRecord | ||
class Context < ::Squeel::Context | ||
class NoParentFoundError < RuntimeError; end | ||
|
||
def initialize(object) | ||
super | ||
@base = object.join_root | ||
@engine = @base.base_klass.arel_engine | ||
@arel_visitor = get_arel_visitor | ||
@default_table = Arel::Table.new(@base.table_name, :as => @base.aliased_table_name, :engine => @engine) | ||
end | ||
|
||
def find(object, parent = @base) | ||
if ::ActiveRecord::Associations::JoinDependency::JoinPart === parent | ||
case object | ||
when String, Symbol, Nodes::Stub | ||
assoc_name = object.to_s | ||
find_string_symbol_stub_association(@base.children, @base, assoc_name, parent) | ||
when Nodes::Join | ||
find_node_join_association(@base.children, @base, object, parent) | ||
else | ||
find_other_association(@base.children, @base, object, parent) | ||
end | ||
end | ||
end | ||
|
||
def find!(object, parent = @base) | ||
if ::ActiveRecord::Associations::JoinDependency::JoinPart === parent | ||
result = | ||
case object | ||
when String, Symbol, Nodes::Stub | ||
assoc_name = object.to_s | ||
find_string_symbol_stub_association(@base.children, @base, assoc_name, parent) | ||
when Nodes::Join | ||
find_node_join_association(@base.children, @base, object, parent) | ||
else | ||
find_other_association(@base.children, @base, object, parent) | ||
end | ||
|
||
result || raise(NoParentFoundError, "can't find #{object} in #{parent}") | ||
else | ||
raise NoParentFoundError, "can't find #{object} in #{parent}" | ||
end | ||
end | ||
|
||
def traverse!(keypath, parent = @base, include_endpoint = false) | ||
parent = @base if keypath.absolute? | ||
keypath.path_without_endpoint.each do |key| | ||
parent = find!(key, parent) | ||
end | ||
parent = find!(keypath.endpoint, parent) if include_endpoint | ||
|
||
parent | ||
end | ||
|
||
private | ||
def find_string_symbol_stub_association(join_associations, current_parent, assoc_name, target_parent) | ||
join_associations.each do |assoc| | ||
return assoc if assoc.reflection.name.to_s == assoc_name && current_parent.equal?(target_parent) | ||
child_assoc = find_string_symbol_stub_association(assoc.children, assoc, assoc_name, target_parent) | ||
return child_assoc if child_assoc | ||
end && false | ||
end | ||
|
||
def find_node_join_association(join_associations, current_parent, object, target_parent) | ||
join_associations.each do |assoc| | ||
return assoc if assoc.reflection.name == object._name && current_parent.equal?(target_parent) && | ||
(object.polymorphic? ? assoc.reflection.klass == object._klass : true) | ||
child_assoc = find_node_join_association(assoc.children, assoc, object, target_parent) | ||
return child_assoc if child_assoc | ||
end && false | ||
end | ||
|
||
def find_other_association(join_associations, current_parent, object, target_parent) | ||
join_associations.each do |assoc| | ||
return assoc if assoc.reflection == object && current_parent.equal?(target_parent) | ||
child_assoc = find_other_association(assoc.children, assoc, object, target_parent) | ||
return child_assoc if child_assoc | ||
end && false | ||
end | ||
end | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
lib/squeel/adapters/active_record/4.1/preloader_extensions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Squeel | ||
module Adapters | ||
module ActiveRecord | ||
module PreloaderExtensions | ||
|
||
def self.included(base) | ||
base.class_eval do | ||
alias_method_chain :preload, :squeel | ||
end | ||
end | ||
|
||
def preload_with_squeel(records, associations, preload_scope = nil) | ||
records = Array.wrap(records).compact.uniq | ||
associations = Array.wrap(associations) | ||
preload_scope = preload_scope || ::ActiveRecord::Associations::Preloader::NULL_RELATION | ||
|
||
if records.empty? | ||
[] | ||
else | ||
Visitors::PreloadVisitor.new.accept(associations).each do |association| | ||
preloaders_on(association, records, preload_scope) | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Associations::Preloader.send :include, Squeel::Adapters::ActiveRecord::PreloaderExtensions |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this file be in version control?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... I didn't notice it. I just want to let the output of rspec more readable. Does it make any troubles?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't care much, @ernie?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's OK by me, or I wouldn't have merged :D