class Tenancy::Shoulda::Matchers::HaveScopeToMatcher

Attributes

scope_name[R]

Public Class Methods

new(scope_name) click to toggle source
# File lib/tenancy/matchers.rb, line 18
def initialize(scope_name)
  @scope_name             = scope_name
  if defined?(ActiveRecord)
    @ar_presence_matcher    = ::Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher.new(@scope_name)
    @ar_belong_to_matcher   = ::Shoulda::Matchers::ActiveRecord::AssociationMatcher.new(:belongs_to, @scope_name)
  end
  if defined?(Mongoid)
    @mid_presence_matcher   = ::Mongoid::Matchers::Validations::HaveValidationMatcher.new(@scope_name, :presence)
    @mid_belong_to_matcher  = ::Mongoid::Matchers::Associations::HaveAssociationMatcher.new(@scope_name, ::Mongoid::Matchers::Associations::BELONGS_TO)
  end
end

Public Instance Methods

description() click to toggle source
# File lib/tenancy/matchers.rb, line 47
def description
  "require to have scope_to :#{@scope_name}"
end
failure_message() click to toggle source
# File lib/tenancy/matchers.rb, line 41
def failure_message
  @presence_matcher.failure_message                   unless @presence_matches
  @belong_to_matcher.failure_message                  unless @belong_to_matches
  "Expected to have default_scope on :#{@scope_name}" unless @default_scope_matches
end
matches?(subject) click to toggle source
# File lib/tenancy/matchers.rb, line 30
def matches?(subject)
  if defined?(ActiveRecord) && subject.class <= ::ActiveRecord::Base
    @ar_presence_matcher.matches?(subject) &&
    @ar_belong_to_matcher.matches?(subject) &&
    ar_default_scope_matches?(subject)
  elsif defined?(Mongoid) && subject.class <= ::Mongoid::Document
    @mid_presence_matcher.matches?(subject) &&
    @mid_belong_to_matcher.matches?(subject)
  end
end

Private Instance Methods

ar_default_scope_matches?(subject) click to toggle source
# File lib/tenancy/matchers.rb, line 53
def ar_default_scope_matches?(subject)
  actual_class = subject.class
  reflection   = actual_class.reflect_on_association(@scope_name.to_sym)
  scoped_class = reflection.class_name.constantize

  if scoped_class.current_id
    actual_class.where(nil).to_sql.include? %Q{#{actual_class.quoted_table_name}.#{scoped_class.connection.quote_column_name(reflection.foreign_key)} = #{scoped_class.connection.quote(scoped_class.current_id)}}
  else
    true
  end
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/tenancy/matchers.rb, line 65
def method_missing(method, *args, &block)
  if @ar_belong_to_matcher && @ar_belong_to_matcher.respond_to?(method)
    @ar_belong_to_matcher.send(method, *args, &block)
  elsif @mid_belong_to_matcher && @mid_belong_to_matcher.respond_to?(method)
    @mid_belong_to_matcher.send(method, *args, &block)
  else
    super
  end
end