-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#873] [#839] fix chained scopes and the :include scope. Credit:
Matt Jones
- Loading branch information
1 parent
e0330b3
commit b46beb9
Showing
1 changed file
with
24 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
module ActiveRecord | ||
module NamedScope | ||
class Scope | ||
|
||
delegate :member_class, :to => :proxy_found | ||
|
||
class Scope | ||
delegate :member_class, :to => :proxy_found | ||
include Hobo::Scopes::ApplyScopes | ||
end | ||
|
||
include Hobo::Scopes::ApplyScopes | ||
module ClassMethods | ||
def scopes | ||
hash = read_inheritable_attribute(:scopes) | ||
if hash.nil? | ||
write_inheritable_attribute(:scopes, new_automatic_scoping_hash(self)) | ||
elsif hash.default_proc.nil? | ||
write_inheritable_attribute(:scopes, new_automatic_scoping_hash(self).merge!(hash)) | ||
else | ||
hash | ||
end | ||
|
||
def respond_to_with_hobo_scopes?(method, include_private=false) | ||
scopes.include?(method) || proxy_scope.respond_to?(method, include_private) || respond_to_without_hobo_scopes?(method, include_private) | ||
end | ||
alias_method_chain :respond_to?, :hobo_scopes | ||
|
||
private | ||
|
||
def method_missing_with_hobo_scopes(method, *args, &block) | ||
respond_to?(method) # required for side effects, see LH#839 | ||
method_missing_without_hobo_scopes(method, *args, &block) | ||
def new_automatic_scoping_hash(o) | ||
hash = Hash.new { |hash, key| o.create_automatic_scope(key) && hash[key] } | ||
hash.meta_eval do | ||
define_method :include? do |key, *args| | ||
super(key, *args) || o.create_automatic_scope(key) | ||
end | ||
end | ||
hash | ||
end | ||
alias_method_chain :method_missing, :hobo_scopes | ||
|
||
end | ||
end | ||
end | ||
|