Module: Friendly::Document::Scoping::ClassMethods

Defined in:
lib/friendly/document/scoping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scope_proxyObject



11
12
13
# File 'lib/friendly/document/scoping.rb', line 11

def scope_proxy
  @scope_proxy ||= ScopeProxy.new(self)
end

Instance Method Details

#has_named_scope?(name) ⇒ Boolean

Returns boolean based on whether the Document has a scope by a particular name.

Parameters:

  • name (Symbol)

    The name of the scope in question.

Returns:



47
48
49
# File 'lib/friendly/document/scoping.rb', line 47

def has_named_scope?(name)
  scope_proxy.has_named_scope?(name)
end

#named_scope(name, parameters) ⇒ Object

Add a named scope to this Document.

e.g.

class Post
  indexes     :created_at
  named_scope :recent, :order! => :created_at.desc
end

Then, you can access the recent posts with:

Post.recent.all

…or…

Post.recent.first

Both #all and #first also take additional parameters:

Post.recent.all(:author_id => @author.id)

Scopes are also chainable. See the README or Friendly::Scope docs for details.

Parameters:

  • name (Symbol)

    the name of the scope.

  • parameters (Hash)

    the query that this named scope will perform.



39
40
41
# File 'lib/friendly/document/scoping.rb', line 39

def named_scope(name, parameters)
  scope_proxy.add_named(name, parameters)
end

#scope(parameters) ⇒ Object

Create an ad hoc scope on this Document.

e.g.

scope = Post.scope(:order! => :created_at)
scope.all # => [#<Post>, #<Post>]

Parameters:

  • parameters (Hash)

    the query parameters to create the scope with.



60
61
62
# File 'lib/friendly/document/scoping.rb', line 60

def scope(parameters)
  scope_proxy.ad_hoc(parameters)
end