module BlackAndWhite::Mongoid

Public Instance Methods

ab_participate!(test_name, **options) { |self| ... } click to toggle source
# File lib/black_and_white/mongoid.rb, line 10
def ab_participate!(test_name, **options, &block)
  @options = options
  if (@ab_test = fetch_ab_test(test_name)).present?
    conditions = true
    conditions = yield(self) if block_given?
    update_participant if conditions
  else
    missing_ab_test(test_name)
  end
end
ab_participates?(test_name) click to toggle source
# File lib/black_and_white/mongoid.rb, line 21
def ab_participates?(test_name)
  ab_tests.detect { |ab_test| ab_test.name == test_name }.present?
end
fetch_ab_test(name) click to toggle source
# File lib/black_and_white/mongoid.rb, line 38
def fetch_ab_test(name)
  query = { name: name }
  if options[:join_inactive] == false
    query.merge!(active: true)
  end

  BlackAndWhite::Mongoid::Test.find_by(query)
end
missing_ab_test(test_name) click to toggle source
# File lib/black_and_white/mongoid.rb, line 31
def missing_ab_test(test_name)
  if options[:raise_on_missing]
    raise AbTestError, "no A/B Test with name #{test_name} exists or it is not active"
  end
  false
end
update_participant() click to toggle source
# File lib/black_and_white/mongoid.rb, line 27
def update_participant
  ab_tests << ab_test
end