Permalink
Browse files

Validating uniqueness of name on every model where applicable, always…

… returning 'n/a' for sample size and confidence of the control
  • Loading branch information...
1 parent b511cf8 commit ebdaa013d231d42d04147b2018cb3a47c3136ee7 @winton committed Nov 1, 2010
Showing with 27 additions and 9 deletions.
  1. +2 −0 lib/a_b/model/a_b_test.rb
  2. +2 −0 lib/a_b/model/category.rb
  3. +2 −0 lib/a_b/model/env.rb
  4. +2 −0 lib/a_b/model/site.rb
  5. +19 −9 lib/a_b/model/variant.rb
View
2 lib/a_b/model/a_b_test.rb
@@ -8,6 +8,8 @@ class ABTest < ActiveRecord::Base
has_many :variants, :foreign_key => 'test_id', :dependent => :destroy
+ validates_uniqueness_of :name, :scope => :category_id
+
def control
@control ||= self.variants.find_by_control true
end
View
2 lib/a_b/model/category.rb
@@ -5,4 +5,6 @@ class Category < ActiveRecord::Base
has_many :tests, :class_name => 'ABTest', :dependent => :destroy
has_many :variants
+
+ validates_uniqueness_of :name, :scope => :site_id
end
View
2 lib/a_b/model/env.rb
@@ -5,6 +5,8 @@ class Env < ActiveRecord::Base
belongs_to :site
belongs_to :user
+ validates_uniqueness_of :name, :scope => :site_id
+
def domain_match?(referer)
d = (domains || '').split(',')
match =
View
2 lib/a_b/model/site.rb
@@ -8,4 +8,6 @@ class Site < ActiveRecord::Base
has_many :envs, :dependent => :delete_all
has_many :tests, :class_name => 'ABTest'
has_many :variants
+
+ validates_uniqueness_of :name, :scope => :user_id
end
View
28 lib/a_b/model/variant.rb
@@ -9,6 +9,8 @@ class Variant < ActiveRecord::Base
serialize :data
+ validates_uniqueness_of :name, :scope => :test_id
+
attr_reader :condition, :env
attr_accessor :conversions, :visits
attr_accessor :visit_conditions, :conversion_conditions
@@ -109,9 +111,13 @@ def condition=(c)
end
def confidence
- self.test.control.env = @env
- self.test.control.condition = @condition
- cumulative_normal_distribution(z_score(self.test.control))
+ if self.control?
+ 'n/a'
+ else
+ self.test.control.env = @env
+ self.test.control.condition = @condition
+ cumulative_normal_distribution(z_score(self.test.control))
+ end
end
def conversion_rate
@@ -193,13 +199,17 @@ def reset!
end
def suggested_visits
- self.test.control.env = @env
- self.test.control.condition = @condition
- size = sample_size(self.test.control)
- if conversion_rate == 0 || size < 100
- 100
+ if self.control?
+ 'n/a'
else
- commafy size
+ self.test.control.env = @env
+ self.test.control.condition = @condition
+ size = sample_size(self.test.control)
+ if conversion_rate == 0 || size < 100
+ 100
+ else
+ commafy size
+ end
end
end

0 comments on commit ebdaa01

Please sign in to comment.