Module: Searchlogic::NamedScopes::AliasScope
- Included in:
- ActiveRecord::Base
- Defined in:
- lib/searchlogic/named_scopes/alias_scope.rb
Overview
Adds the ability to create alias scopes that allow you to alias a named scope or create a named scope procedure. See the alias_scope method for a more detailed explanation.
Instance Method Summary collapse
-
#alias_scope(name, options = nil) ⇒ Object
(also: #scope_procedure)
In some instances you might create a class method that essentially aliases a named scope or represents a named scope procedure.
-
#alias_scope?(name) ⇒ Boolean
:nodoc:.
-
#alias_scopes ⇒ Object
:nodoc:.
-
#condition?(name) ⇒ Boolean
:nodoc:.
-
#named_scope_options(name) ⇒ Object
:nodoc:.
Instance Method Details
#alias_scope(name, options = nil) ⇒ Object Also known as: scope_procedure
In some instances you might create a class method that essentially aliases a named scope or represents a named scope procedure. Ex:
class User
def teenager
age_gte(13).age_lte(19)
end
end
This is obviously a very basic example, but notice how we are utilizing already existing named scopes so that we do not have to repeat ourself. This method makes a lot more sense when you are dealing with complicated named scope.
There is a problem though. What if you want to use this in your controller’s via the ‘search’ method:
User.search(:teenager => true)
You would expect that to work, but how does Searchlogic::Search tell the difference between your ‘teenager’ method and the ‘destroy_all’ method. It can’t, there is no way to tell unless we actually call the method, which we obviously can not do.
The being said, we need a way to tell searchlogic that this is method is safe. Here’s how you do that:
User.alias_scope :teenager, lambda { age_gte(13).age_lte(19) }
This feels better, it feels like our other scopes, and it provides a way to tell Searchlogic that this is a safe method.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/searchlogic/named_scopes/alias_scope.rb', line 34 def alias_scope(name, = nil) alias_scopes[name.to_sym] = (class << self; self end).instance_eval do define_method name do |*args| case when Symbol send() else .call(*args) end end end end |
#alias_scope?(name) ⇒ Boolean
:nodoc:
53 54 55 56 |
# File 'lib/searchlogic/named_scopes/alias_scope.rb', line 53 def alias_scope?(name) # :nodoc: return false if name.blank? alias_scopes.key?(name.to_sym) end |
#alias_scopes ⇒ Object
:nodoc:
49 50 51 |
# File 'lib/searchlogic/named_scopes/alias_scope.rb', line 49 def alias_scopes # :nodoc: @alias_scopes ||= {} end |
#condition?(name) ⇒ Boolean
:nodoc:
58 59 60 |
# File 'lib/searchlogic/named_scopes/alias_scope.rb', line 58 def condition?(name) # :nodoc: super || alias_scope?(name) end |
#named_scope_options(name) ⇒ Object
:nodoc:
62 63 64 |
# File 'lib/searchlogic/named_scopes/alias_scope.rb', line 62 def (name) # :nodoc: super || alias_scopes[name.to_sym] end |