class Object

Public Instance Methods

all_scope_categories_satisfied(&block) click to toggle source
# File lib/required_scopes/active_record/relation.rb, line 61
def all_scope_categories_satisfied(&block)
  scope_categories_satisfied(required_scope_categories, &block)
end
all_scope_categories_satisfied!() click to toggle source

Tells this Relation that all categories have been satisfied.

# File lib/required_scopes/active_record/relation.rb, line 57
def all_scope_categories_satisfied!
  scope_categories_satisfied!(required_scope_categories)
end
build_scope_with_required_scopes_ignored() click to toggle source
# File lib/required_scopes/active_record/base.rb, line 228
def build_scope_with_required_scopes_ignored
  out = build_scope_without_required_scopes_ignored
  out.all_scope_categories_satisfied!
  out
end
ensure_categories_satisfied!(triggering_method) click to toggle source

Raises an exception if there is at least one required scope category that has not yet been satisfied. triggering_method is the name of the method called that triggered this check; we include this in the error we raise.

# File lib/required_scopes/active_record/relation.rb, line 82
def ensure_categories_satisfied!(triggering_method)
  required_categories = required_scope_categories
  missing_categories = required_categories - satisfied_scope_categories

  if missing_categories.length > 0
    # We return a special exception for the category +:base+, because we want to give a simpler, cleaner error
    # message for users who are just using the #base_scope_required! syntactic sugar instead of the full categories
    # system.
    # $stderr.puts "RAISING AT: #{caller.join("\n    ")}"
    if missing_categories == [ :base ]
      raise RequiredScopes::Errors::BaseScopeNotSatisfiedError.new(klass, self, triggering_method)
    else
      raise RequiredScopes::Errors::RequiredScopeCategoriesNotSatisfiedError.new(
        klass, self, triggering_method, required_categories, satisfied_scope_categories)
    end
  end
end
merge(other_relation) click to toggle source

Overrides merge to merge the information about which scope categories have been satisfied, too.

Calls superclass method
# File lib/required_scopes/active_record/relation.rb, line 71
def merge(other_relation)
  super.scope_categories_satisfied(satisfied_scope_categories | other_relation.satisfied_scope_categories)
end
satisfied_scope_categories() click to toggle source

Returns the set of scope categories that have been satisfied.

# File lib/required_scopes/active_record/relation.rb, line 66
def satisfied_scope_categories
  @satisfied_scope_categories ||= [ ]
end
scope_categories_satisfied(*categories, &block) click to toggle source

Call this method inline, exactly as you would any class-defined scope, to indicate that a particular category or categories have been satisfied. It’s really intended for use in a class method, but both of these will work:

class User < ActiveRecord::Base
  must_scope_by :client, :deleted

  class << self
    def active_for_client_named(client_name)
      client_id = CLIENT_MAP[client_name]
      where(:client_id => client_id).where(:deleted => false).scope_categories_satisfied(:client)
    end
  end
end

User.active_for_client_named('foo').first
User.where(:client_id => client_id).where(:deleted => false).scope_categories_satisfied(:client, :deleted).first
# File lib/required_scopes/active_record/relation.rb, line 25
def scope_categories_satisfied(*categories, &block)
  categories = categories.flatten

  new_scope = if categories.length == 0
    self
  else
    out = clone
    out.scope_categories_satisfied!(categories)
    out
  end

  if block
    new_scope.scoping(&block)
  else
    new_scope
  end
end
scope_categories_satisfied!(categories) click to toggle source

Tells this Relation that one or more categories have been satisfied.

# File lib/required_scopes/active_record/relation.rb, line 49
def scope_categories_satisfied!(categories)
  categories = categories.flatten

  @satisfied_scope_categories ||= [ ]
  @satisfied_scope_categories |= categories
end
scope_category_satisfied(category, &block) click to toggle source

Alias for scope_categories_satisfied.

# File lib/required_scopes/active_record/relation.rb, line 44
def scope_category_satisfied(category, &block)
  scope_categories_satisfied(category, &block)
end