Module: Mongoid::Criterion::Scoping

Included in:
Mongoid::Criteria
Defined in:
lib/mongoid/criterion/scoping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_scopableObject

Returns the value of attribute default_scopable.



6
7
8
# File 'lib/mongoid/criterion/scoping.rb', line 6

def default_scopable
  @default_scopable
end

Instance Method Details

#apply_default_scopeCriteria

Apply the model’s default scope to this criteria.

Examples:

Apply the default scope.

criteria.apply_default_scope

Returns:

Since:

  • 2.4.0



16
17
18
19
20
21
22
23
# File 'lib/mongoid/criterion/scoping.rb', line 16

def apply_default_scope
  if klass.default_scoping && default_scopable?
    self.default_scopable = false
    fuse(klass.default_scoping)
  else
    self
  end
end

#clear_scopingnil

Remove all scoping from the criteria.

Examples:

Remove the default scope.

criteria.clear_scoping

Returns:

  • (nil)

    No guaranteed return value.

Since:

  • 2.4.0



77
78
79
80
# File 'lib/mongoid/criterion/scoping.rb', line 77

def clear_scoping
  selector.clear
  options.clear
end

#default_scopable?true, false

Is the default scope of the class allowed to be applied?

Examples:

Can the default scope be applied?

criteria.default_scopable?

Returns:

  • (true, false)

    The the default can be applied.

Since:

  • 2.4.0



33
34
35
# File 'lib/mongoid/criterion/scoping.rb', line 33

def default_scopable?
  default_scopable != false
end

#scopedCriteria

Force the default scope to be applied to the criteria.

Examples:

Force default scoping.

criteria.scoped

Returns:

Since:

  • 2.4.0



45
46
47
48
# File 'lib/mongoid/criterion/scoping.rb', line 45

def scoped
  self.default_scopable = true
  apply_default_scope
end

#unscopedCriteria

Note:

This has slightly different behaviour than AR - will remove the default scoping if no other criteria have been chained and tampered with the criterion instead of clearing everything.

Get the criteria with the default scoping removed.

Examples:

Get the criteria unscoped.

criteria.unscoped

Returns:

Since:

  • 2.4.0



62
63
64
65
66
67
# File 'lib/mongoid/criterion/scoping.rb', line 62

def unscoped
  clone.tap do |criteria|
    criteria.clear_scoping
    criteria.default_scopable = false
  end
end