Permalink
Browse files

Removing more remnants of cached find and collaborations, adding opti…

…mistic locking on variants
  • Loading branch information...
1 parent 55ffef5 commit d58f32249956aef33e9c2718dbf9de996788d080 @winton committed Nov 7, 2010
View
9 db/migrate/014_variants_lock.rb
@@ -0,0 +1,9 @@
+class VariantsLock < ActiveRecord::Migration
+ def self.up
+ add_column :variants, :lock_version, :integer, :default => 0
+ end
+
+ def self.down
+ remove_column :variants, :lock_version
+ end
+end
View
2 lib/a_b/model/env.rb
@@ -1,7 +1,5 @@
class Env < ActiveRecord::Base
- extend CachedFind
-
belongs_to :site
belongs_to :user
View
2 lib/a_b/model/site.rb
@@ -3,8 +3,6 @@ class Site < ActiveRecord::Base
belongs_to :user
has_many :categories, :dependent => :destroy
- has_many :collaborations
- has_many :collab_users, :class_name => 'User', :source => :user, :through => :collaborations
has_many :envs, :dependent => :delete_all
has_many :tests, :class_name => 'ABTest'
has_many :variants
View
2 lib/a_b/model/user.rb
@@ -1,8 +1,6 @@
class User < ActiveRecord::Base
has_many :categories
- has_many :collaborations
- has_many :collab_sites, :class_name => 'Site', :source => :site, :through => :collaborations
has_many :envs
has_many :sites, :dependent => :destroy
has_many :tests, :class_name => 'ABTest'
View
62 lib/a_b/model/variant.rb
@@ -1,7 +1,5 @@
class Variant < ActiveRecord::Base
- extend CachedFind
-
belongs_to :category
belongs_to :site
belongs_to :test, :class_name => 'ABTest', :foreign_key => 'test_id'
@@ -44,9 +42,7 @@ def self.record(options)
ids = data['c'] + data['v']
ids = ids.compact.uniq
- variants = ids.collect do |id|
- Variant.find(id, :include => :site)
- end
+ variants = Variant.find_all_by_id(ids, :include => :site)
env = Env.find(:first, :conditions => {
:name => env,
@@ -55,39 +51,45 @@ def self.record(options)
return [ [], [] ] if variants.empty? || !env
- visit = []
- convert = []
+ begin
+ visit = []
+ convert = []
- variants.each do |variant|
- next unless env.domain_match?(options[:referer])
- variant.env = env.name
- visit.push(variant) if data['v'].include?(variant.id)
- convert.push(variant) if data['c'].include?(variant.id)
- end
+ variants.each do |variant|
+ next unless env.domain_match?(options[:referer])
+ variant.env = env.name
+ visit.push(variant) if data['v'].include?(variant.id)
+ convert.push(variant) if data['c'].include?(variant.id)
+ end
- visit.each do |v|
- v.visits += 1
- if data['e'] && !data['e'].empty?
- v.visit_conditions ||= {}
- (data['e'] || {}).each do |key, value|
- v.visit_conditions[key] ||= 0
- v.visit_conditions[key] += 1 if value
+ visit.each do |v|
+ v.visits += 1
+ if data['e'] && !data['e'].empty?
+ v.visit_conditions ||= {}
+ (data['e'] || {}).each do |key, value|
+ v.visit_conditions[key] ||= 0
+ v.visit_conditions[key] += 1 if value
+ end
end
end
- end
- convert.each do |c|
- c.conversions += 1
- if data['e'] && !data['e'].empty?
- c.conversion_conditions ||= {}
- (data['e'] || {}).each do |key, value|
- c.conversion_conditions[key] ||= 0
- c.conversion_conditions[key] += 1 if value
+ convert.each do |c|
+ c.conversions += 1
+ if data['e'] && !data['e'].empty?
+ c.conversion_conditions ||= {}
+ (data['e'] || {}).each do |key, value|
+ c.conversion_conditions[key] ||= 0
+ c.conversion_conditions[key] += 1 if value
+ end
end
end
- end
- variants.each(&:save)
+ variants.each(&:save)
+
+ rescue ActiveRecord::StaleObjectError => e
+ variants.each(&:reload)
+ retry
+ end
[ visit.collect(&:id), convert.collect(&:id) ]
end
View
10 spec/a_b/model/site_spec.rb
@@ -24,16 +24,6 @@
Category.count(:conditions => { :user_id => @site.id }).should == 0
end
- it "should have many collaborations" do
- @site.collaborations.empty?.should == false
- @site.collaborations.should == [ @collaboration ]
- end
-
- it "should have many collaborative users" do
- @site.collab_users.empty?.should == false
- @site.collab_users.should == [ @user ]
- end
-
it "should have many envs" do
@site.envs.empty?.should == false
@site.envs.should == [ @env ]
View
10 spec/a_b/model/user_spec.rb
@@ -14,16 +14,6 @@
@user.categories.should == [ @category ]
end
- it "should have many collaborations" do
- @user.collaborations.empty?.should == false
- @user.collaborations.should == [ @collaboration ]
- end
-
- it "should have many collaborative sites" do
- @user.collab_sites.empty?.should == false
- @user.collab_sites.should == [ @site ]
- end
-
it "should have many envs" do
@user.envs.empty?.should == false
@user.envs.should == [ @env ]
View
7 spec/spec_helper.rb
@@ -32,11 +32,6 @@ def setup_tests
:user_id => @user.id
)
@site.id = 1
- @collaboration = Collaboration.new(
- :site_id => @site.id,
- :user_id => @user.id
- )
- @collaboration.id = 1
@category = Category.new(
:name => 'category',
:site_id => @site.id,
@@ -66,7 +61,6 @@ def setup_tests
)
@variant.id = 1
@category.save
- @collaboration.save
@env.save
@site.save
@test.save
@@ -78,7 +72,6 @@ def teardown_tests
ABRequest.delete_all
ABTest.delete_all
Category.delete_all
- Collaboration.delete_all
Env.delete_all
IP.delete_all
Site.delete_all

0 comments on commit d58f322

Please sign in to comment.