Class: Friendly::Scope
- Inherits:
-
Object
- Object
- Friendly::Scope
- Defined in:
- lib/friendly/scope.rb
Instance Attribute Summary
- - (Object) klass readonly Returns the value of attribute klass.
- - (Object) parameters readonly Returns the value of attribute parameters.
Instance Method Summary
- - (Object) +(other_scope) Create a new Scope that is the combination of self and other, where other takes priority.
- - (Object) all(extra_parameters = {}) Fetch all documents at this scope.
- - (Object) build(extra_parameters = {}) Build an object at this scope.
- - (Object) chain_with(scope_name) Chain with another one of klass’s named_scopes.
- - (Object) create(extra_parameters = {}) Create an object at this scope.
- - (Object) first(extra_parameters = {}) Fetch the first document at this scope.
- - (Scope) initialize(klass, parameters) constructor A new instance of Scope.
- - (Object) method_missing(method_name, *args, &block) Use method_missing to respond to other named scopes on klass.
- - (Object) paginate(extra_parameters = {}) Paginate the documents at this scope.
- - (Boolean) respond_to?(method_name, include_private = false) Override #respond_to? so that we can return true when it’s another named_scope.
Constructor Details
- (Scope) initialize(klass, parameters)
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
- (Object) method_missing(method_name, *args, &block)
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
- (Object) klass (readonly)
Returns the value of attribute klass
3 4 5 |
# File 'lib/friendly/scope.rb', line 3 def klass @klass end |
- (Object) parameters (readonly)
Returns the value of attribute parameters
3 4 5 |
# File 'lib/friendly/scope.rb', line 3 def parameters @parameters end |
Instance Method Details
- (Object) +(other_scope)
Create a new Scope that is the combination of self and other, where other takes priority
87 88 89 |
# File 'lib/friendly/scope.rb', line 87 def +(other_scope) self.class.new(klass, parameters.merge(other_scope.parameters)) end |
- (Object) all(extra_parameters = {})
Fetch all documents at this scope.
14 15 16 |
# File 'lib/friendly/scope.rb', line 14 def all(extra_parameters = {}) klass.all(params(extra_parameters)) end |
- (Object) build(extra_parameters = {})
Build an object at this scope.
e.g. Post.scope(:name => "James").build.name # => "James"
42 43 44 |
# File 'lib/friendly/scope.rb', line 42 def build(extra_parameters = {}) klass.new(params_without_modifiers(extra_parameters)) end |
- (Object) chain_with(scope_name)
Chain with another one of klass’s named_scopes.
79 80 81 |
# File 'lib/friendly/scope.rb', line 79 def chain_with(scope_name) self + klass.send(scope_name) end |
- (Object) create(extra_parameters = {})
Create an object at this scope.
e.g.
@post = Post.scope(:name => "James").create
@post.new_record? # => false
@post.name # => "James"
55 56 57 |
# File 'lib/friendly/scope.rb', line 55 def create(extra_parameters = {}) klass.create(params_without_modifiers(extra_parameters)) end |
- (Object) first(extra_parameters = {})
Fetch the first document at this scope.
22 23 24 |
# File 'lib/friendly/scope.rb', line 22 def first(extra_parameters = {}) klass.first(params(extra_parameters)) end |
- (Object) paginate(extra_parameters = {})
Paginate the documents at this scope.
31 32 33 |
# File 'lib/friendly/scope.rb', line 31 def paginate(extra_parameters = {}) klass.paginate(params(extra_parameters)) end |
- (Boolean) respond_to?(method_name, include_private = false)
Override #respond_to? so that we can return true when it’s another named_scope.
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 |