module ThinkingSphinx::ActiveRecord::Scopes::ClassMethods

Public Instance Methods

add_sphinx_scopes_support_to_has_many_associations() click to toggle source
# File lib/thinking_sphinx/active_record/scopes.rb, line 83
def add_sphinx_scopes_support_to_has_many_associations
  mixin = sphinx_scopes_support_mixin
  sphinx_scopes_support_classes.each do |klass|
    klass.send(:include, mixin) unless klass.ancestors.include?(mixin)
  end
end
default_sphinx_scope(sphinx_scope_name) click to toggle source

Similar to ActiveRecord’s default_scope method Thinking Sphinx supports a default_sphinx_scope. For example:

default_sphinx_scope :some_sphinx_named_scope

The scope is automatically applied when the search method is called. It will only be applied if it is an existing sphinx_scope.

# File lib/thinking_sphinx/active_record/scopes.rb, line 19
def default_sphinx_scope(sphinx_scope_name)
  add_sphinx_scopes_support_to_has_many_associations
  @default_sphinx_scope = sphinx_scope_name
end
get_default_sphinx_scope() click to toggle source

Returns the #default_sphinx_scope or nil if none is set.

# File lib/thinking_sphinx/active_record/scopes.rb, line 25
def get_default_sphinx_scope
  @default_sphinx_scope
end
has_default_sphinx_scope?() click to toggle source

Returns true if the current Model has a default_sphinx_scope. Also checks if the #default_sphinx_scope actually is a scope.

# File lib/thinking_sphinx/active_record/scopes.rb, line 31
def has_default_sphinx_scope?
  !@default_sphinx_scope.nil? && sphinx_scopes.include?(@default_sphinx_scope)
end
remove_sphinx_scopes() click to toggle source
# File lib/thinking_sphinx/active_record/scopes.rb, line 75
def remove_sphinx_scopes
  sphinx_scopes.each do |scope|
    singleton_class.send(:undef_method, scope)
  end
  
  sphinx_scopes.clear
end
sphinx_scope(method, &block) click to toggle source

Similar to ActiveRecord’s named_scope method Thinking Sphinx supports scopes. For example:

sphinx_scope(:latest_first) { 
    {:order => 'created_at DESC, @relevance DESC'}
  }

Usage:

@articles =  Article.latest_first.search 'pancakes'
# File lib/thinking_sphinx/active_record/scopes.rb, line 46
def sphinx_scope(method, &block)
  add_sphinx_scopes_support_to_has_many_associations

  @sphinx_scopes ||= []
  @sphinx_scopes << method
  
  singleton_class.instance_eval do
    define_method(method) do |*args|
      options = {:classes => classes_option}
      options.merge! block.call(*args)
      
      ThinkingSphinx::Search.new(options)
    end
    
    define_method("#{method}_without_default".to_sym) do |*args|
      options = {:classes => classes_option, :ignore_default => true}
      options.merge! block.call(*args)
      
      ThinkingSphinx::Search.new(options)
    end
  end
end
sphinx_scopes() click to toggle source

This returns an Array of all defined scopes. The default scope shows as :default.

# File lib/thinking_sphinx/active_record/scopes.rb, line 71
def sphinx_scopes
  @sphinx_scopes || []
end
sphinx_scopes_support_classes() click to toggle source
# File lib/thinking_sphinx/active_record/scopes.rb, line 90
def sphinx_scopes_support_classes
  if ThinkingSphinx.rails_3_1?
    [::ActiveRecord::Associations::CollectionProxy]
  else
    [::ActiveRecord::Associations::HasManyAssociation,
     ::ActiveRecord::Associations::HasManyThroughAssociation]
  end
end
sphinx_scopes_support_mixin() click to toggle source
# File lib/thinking_sphinx/active_record/scopes.rb, line 99
def sphinx_scopes_support_mixin
  if ThinkingSphinx.rails_3_1?
    ::ThinkingSphinx::ActiveRecord::CollectionProxyWithScopes
  else
    ::ThinkingSphinx::ActiveRecord::HasManyAssociationWithScopes
  end
end