Module: ThinkingSphinx::ActiveRecord::Scopes::ClassMethods
- Defined in:
- lib/thinking_sphinx/active_record/scopes.rb
Instance Method Summary collapse
- #add_sphinx_scopes_support_to_has_many_associations ⇒ Object
-
#default_sphinx_scope(sphinx_scope_name) ⇒ Object
Similar to ActiveRecord’s default_scope method Thinking Sphinx supports a default_sphinx_scope.
-
#get_default_sphinx_scope ⇒ Object
Returns the default_sphinx_scope or nil if none is set.
-
#has_default_sphinx_scope? ⇒ Boolean
Returns true if the current Model has a default_sphinx_scope.
- #remove_sphinx_scopes ⇒ Object
-
#sphinx_scope(method, &block) ⇒ Object
Similar to ActiveRecord’s named_scope method Thinking Sphinx supports scopes.
-
#sphinx_scopes ⇒ Object
This returns an Array of all defined scopes.
Instance Method Details
#add_sphinx_scopes_support_to_has_many_associations ⇒ Object
83 84 85 86 87 88 |
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 83 def add_sphinx_scopes_support_to_has_many_associations scope_mixin = ::ThinkingSphinx::ActiveRecord::HasManyAssociationWithScopes ::ActiveRecord::Associations::HasManyAssociation.send(:include, scope_mixin) ::ActiveRecord::Associations::HasManyThroughAssociation.send(:include, scope_mixin) end |
#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 22 |
# 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 ⇒ Object
Returns the default_sphinx_scope or nil if none is set.
25 26 27 |
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 25 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.
31 32 33 |
# 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 ⇒ Object
75 76 77 78 79 80 81 |
# 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) ⇒ 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'
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# 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| = {:classes => classes_option} .merge! block.call(*args) ThinkingSphinx::Search.new() end define_method("#{method}_without_default".to_sym) do |*args| = {:classes => classes_option, :ignore_default => true} .merge! block.call(*args) ThinkingSphinx::Search.new() end end end |
#sphinx_scopes ⇒ Object
This returns an Array of all defined scopes. The default scope shows as :default.
71 72 73 |
# File 'lib/thinking_sphinx/active_record/scopes.rb', line 71 def sphinx_scopes @sphinx_scopes || [] end |