Module: ThinkingSphinx::ActiveRecord::Scopes::ClassMethods

Defined in:
lib/thinking_sphinx/active_record/scopes.rb

Instance Method Summary collapse

Instance Method Details

#default_sphinx_scope(sphinx_scope_name) ⇒ Object

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.



19
20
21
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 19

def default_sphinx_scope(sphinx_scope_name)
  @default_sphinx_scope = sphinx_scope_name
end

#get_default_sphinx_scopeObject

Returns the default_sphinx_scope or nil if none is set.



24
25
26
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 24

def get_default_sphinx_scope
  @default_sphinx_scope
end

#has_default_sphinx_scope?Boolean

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

Returns:

  • (Boolean)


30
31
32
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 30

def has_default_sphinx_scope?
  !@default_sphinx_scope.nil? && sphinx_scopes.include?(@default_sphinx_scope)
end

#remove_sphinx_scopesObject



65
66
67
68
69
70
71
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 65

def remove_sphinx_scopes
  sphinx_scopes.each do |scope|
    singleton_class.send(:undef_method, scope)
  end
  
  sphinx_scopes.clear
end

#sphinx_scope(method, &block) ⇒ Object

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'


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 45

def sphinx_scope(method, &block)
  @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
  end
end

#sphinx_scopesObject

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



61
62
63
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 61

def sphinx_scopes
  @sphinx_scopes || []
end