-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactored association code to be much simpler and rely on recursio…
…n. This allows the underlying class to do most of the work. This also allows calling any named scopes through any level of associations.
- Loading branch information
1 parent
8a21abe
commit 74d5631
Showing
17 changed files
with
191 additions
and
210 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Searchlogic | ||
module ActiveRecord | ||
# Active Record is pretty inconsistent with how their SQL is constructed. This | ||
# method attempts to close the gap between the various inconsistencies. | ||
module Consistency | ||
def self.included(klass) | ||
klass.class_eval do | ||
alias_method_chain :merge_joins, :searchlogic | ||
end | ||
end | ||
|
||
# In AR multiple joins are sometimes in a single join query, and other times they | ||
# are not. The merge_joins method in AR should account for this, but it doesn't. | ||
# This fixes that problem. This way there is one join per string, which allows | ||
# the merge_joins method to delete duplicates. | ||
def merge_joins_with_searchlogic(*args) | ||
joins = merge_joins_without_searchlogic(*args) | ||
joins.collect { |j| j.is_a?(String) ? j.split(" ") : j }.flatten.uniq | ||
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,51 @@ | ||
module Searchlogic | ||
module ActiveRecord | ||
# Adds methods that give extra information about a classes named scopes. | ||
module NamedScopes | ||
# Retrieves the options passed when creating the respective named scope. Ex: | ||
# | ||
# named_scope :whatever, :conditions => {:column => value} | ||
# | ||
# This method will return: | ||
# | ||
# :conditions => {:column => value} | ||
# | ||
# ActiveRecord hides this internally in a Proc, so we have to try and pull it out with this | ||
# method. | ||
def named_scope_options(name) | ||
key = scopes.key?(name.to_sym) ? name.to_sym : primary_condition_name(name) | ||
|
||
if key | ||
eval("options", scopes[key].binding) | ||
else | ||
nil | ||
end | ||
end | ||
|
||
# The arity for a named scope's proc is important, because we use the arity | ||
# to determine if the condition should be ignored when calling the search method. | ||
# If the condition is false and the arity is 0, then we skip it all together. Ex: | ||
# | ||
# User.named_scope :age_is_4, :conditions => {:age => 4} | ||
# User.search(:age_is_4 => false) == User.all | ||
# User.search(:age_is_4 => true) == User.all(:conditions => {:age => 4}) | ||
# | ||
# We also use it when trying to "copy" the underlying named scope for association | ||
# conditions. This way our aliased scope accepts the same number of parameters for | ||
# the underlying scope. | ||
def named_scope_arity(name) | ||
options = named_scope_options(name) | ||
options.respond_to?(:arity) ? options.arity : nil | ||
end | ||
|
||
# A convenience method for creating inner join sql to that your inner joins | ||
# are consistent with how Active Record creates them. Basically a tool for | ||
# you to use when writing your own named scopes. This way you know for sure | ||
# that duplicate joins will be removed when chaining scopes together that | ||
# use the same join. | ||
def inner_joins(association_name) | ||
ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, association_name, nil).join_associations.collect { |assoc| assoc.association_join } | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.