Class: Friendly::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, parameters) ⇒ Scope

Returns a new instance of Scope.



5
6
7
8
# File 'lib/friendly/scope.rb', line 5

def initialize(klass, parameters)
  @klass      = klass
  @parameters = parameters
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Use method_missing to respond to other named scopes on klass.



71
72
73
# File 'lib/friendly/scope.rb', line 71

def method_missing(method_name, *args, &block)
  respond_to?(method_name) ? chain_with(method_name) : super
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/friendly/scope.rb', line 3

def klass
  @klass
end

#parametersObject (readonly)

Returns the value of attribute parameters.



3
4
5
# File 'lib/friendly/scope.rb', line 3

def parameters
  @parameters
end

Instance Method Details

#+(other_scope) ⇒ Object

Create a new Scope that is the combination of self and other, where other takes priority

Parameters:



87
88
89
# File 'lib/friendly/scope.rb', line 87

def +(other_scope)
  self.class.new(klass, parameters.merge(other_scope.parameters))
end

#all(extra_parameters = {}) ⇒ Object

Fetch all documents at this scope.

Parameters:

  • extra_parameters (Hash) (defaults to: {})

    add extra parameters to this query.



14
15
16
# File 'lib/friendly/scope.rb', line 14

def all(extra_parameters = {})
  klass.all(params(extra_parameters))
end

#build(extra_parameters = {}) ⇒ Object

Build an object at this scope.

e.g.
  Post.scope(:name => "James").build.name # => "James"

Parameters:

  • extra_parameters (Hash) (defaults to: {})

    add extra parameters to this query.



42
43
44
# File 'lib/friendly/scope.rb', line 42

def build(extra_parameters = {})
  klass.new(params_without_modifiers(extra_parameters))
end

#chain_with(scope_name) ⇒ Object

Chain with another one of klass’s named_scopes.

Parameters:

  • scope_name (Symbol)

    The name of the scope to chain with.



79
80
81
# File 'lib/friendly/scope.rb', line 79

def chain_with(scope_name)
  self + klass.send(scope_name)
end

#create(extra_parameters = {}) ⇒ Object

Create an object at this scope.

e.g.
  @post = Post.scope(:name => "James").create
  @post.new_record? # => false
  @post.name # => "James"

Parameters:

  • extra_parameters (Hash) (defaults to: {})

    add extra parameters to this query.



55
56
57
# File 'lib/friendly/scope.rb', line 55

def create(extra_parameters = {})
  klass.create(params_without_modifiers(extra_parameters))
end

#first(extra_parameters = {}) ⇒ Object

Fetch the first document at this scope.

Parameters:

  • extra_parameters (Hash) (defaults to: {})

    add extra parameters to this query.



22
23
24
# File 'lib/friendly/scope.rb', line 22

def first(extra_parameters = {})
  klass.first(params(extra_parameters))
end

#paginate(extra_parameters = {}) ⇒ Object

Paginate the documents at this scope.

Parameters:

  • extra_parameters (Hash) (defaults to: {})

    add extra parameters to this query.

Returns:

  • WillPaginate::Collection



31
32
33
# File 'lib/friendly/scope.rb', line 31

def paginate(extra_parameters = {})
  klass.paginate(params(extra_parameters))
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Override #respond_to? so that we can return true when it’s another named_scope.

Returns:



63
64
65
# File 'lib/friendly/scope.rb', line 63

def respond_to?(method_name, include_private = false)
  klass.has_named_scope?(method_name) || super
end