Browse files

a_b.js working in all IE versions, adding way to specify which varian…

…t gets visited
  • Loading branch information...
1 parent a12b925 commit 17abee721cd308a5511570904b1750226a37415a @winton committed Oct 28, 2010
Showing with 20 additions and 7 deletions.
  1. +18 −7 public/js/a_b.js
  2. +2 −0 vendor/a_b_plugin/lib/a_b_plugin/test.rb
View
25 public/js/a_b.js
@@ -119,6 +119,14 @@ window.A_B = new function() {
return (data[key] || (key == 'e' ? {} : []));
}
+ function inArray(arr, obj) {
+ for (var i = 0; i < arr.length; i++) {
+ if (arr[i] == obj)
+ return i;
+ }
+ return -1;
+ };
+
function objEmpty(obj) {
for(var i in obj)
return false;
@@ -137,7 +145,7 @@ window.A_B = new function() {
else if (value.constructor == Array)
data[key] = data[key].concat(value);
// Other value
- else if (data[key].indexOf(value) == -1)
+ else if (inArray(data[key], value) == -1)
data[key].push(value);
if (data.constructor == Array)
data[key] = uniqArray(data[key]);
@@ -296,6 +304,8 @@ window.A_B = new function() {
if (!visit && test.variants.length) {
if (window.testing)
visit = test.variants[0];
+ else if (variant)
+ visit = variant;
else
visit = test.variants[
Math.floor(Math.random() * test.variants.length)
@@ -317,12 +327,13 @@ window.A_B = new function() {
// Private
function findCategory(name) {
- return grep(categories, function(c) {
- return (
- c.name == name ||
- symbolizeName(c.name) == name
- );
- })[0];
+ if (categories)
+ return grep(categories, function(c) {
+ return (
+ c.name == name ||
+ symbolizeName(c.name) == name
+ );
+ })[0];
}
function findTest(name) {
View
2 vendor/a_b_plugin/lib/a_b_plugin/test.rb
@@ -60,6 +60,8 @@ def visit(name=nil, &block)
if !visit && !@test['variants'].empty?
if $testing
visit = @test['variants'][0]
+ elsif variant
+ visit = variant
else
visit = @test['variants'][rand(@test['variants'].size)]
end

0 comments on commit 17abee7

Please sign in to comment.