Permalink
Browse files

make sure named scopes recognize dynamic scopes

  • Loading branch information...
1 parent fe519d8 commit fdd8e3e4ee4392048165ac7d533e71e79cefb476 @binarylogic committed Mar 7, 2011
Showing with 29 additions and 0 deletions.
  1. +2 −0 lib/searchlogic.rb
  2. +27 −0 lib/searchlogic/active_record/scope.rb
View
2 lib/searchlogic.rb
@@ -2,6 +2,7 @@
require "searchlogic/core_ext/object"
require "searchlogic/active_record/consistency"
require "searchlogic/active_record/named_scope_tools"
+require "searchlogic/active_record/scope"
require "searchlogic/named_scopes/base"
require "searchlogic/named_scopes/column_conditions"
require "searchlogic/named_scopes/ordering"
@@ -29,6 +30,7 @@ class << self; include Searchlogic::ActiveRecord::Consistency; end
end
end
+ActiveRecord::NamedScope::Scope.send(:include, Searchlogic::ActiveRecord::Scope)
ActiveRecord::Base.extend(Searchlogic::ActiveRecord::NamedScopeTools)
ActiveRecord::Base.extend(Searchlogic::NamedScopes::Base)
ActiveRecord::Base.extend(Searchlogic::NamedScopes::ColumnConditions)
View
27 lib/searchlogic/active_record/scope.rb
@@ -0,0 +1,27 @@
+module Searchlogic
+ module ActiveRecord
+ # Makes sure chained scopes work correctly. ActiveRecord calls scopes.include?(name) in the ActiveRecord::NamedScopes::Scope class
+ # This presented a problem because searchlogic scopes are called on the fly. So we need to try and create the scope before they
+ # check for this. Thats what this module is all about.
+ module Scope
+ def self.included(klass)
+ klass.class_eval do
+ alias_method_chain :method_missing, :searchlogic
+ end
+ end
+
+ private
+ def method_missing_with_searchlogic(*args)
+ method = args.first
+ if !scopes.include?(method)
+ begin
+ proxy_scope.proxy_reflection.klass.respond_to?(method)
+ rescue NoMethodError => e
+ proxy_scope.respond_to?(method)
+ end
+ end
+ method_missing_without_searchlogic(*args)
+ end
+ end
+ end
+end

0 comments on commit fdd8e3e

Please sign in to comment.