Skip to content

Instantly share code, notes, and snippets.

@DGaffney
Created January 21, 2011 03:07
Show Gist options
  • Select an option

  • Save DGaffney/789182 to your computer and use it in GitHub Desktop.

Select an option

Save DGaffney/789182 to your computer and use it in GitHub Desktop.
##lib/targeting_algorithm_manager.rb
class TargetingAlgorithmManager
def self.method_missing(name, *args, &blk)
debugger
callbacks = ["before_validation","after_validation","before_save","before_create","after_create","after_save"]
allowed_prefixes = callbacks#&other_prefixes&others&still_more
if name.to_s.partially_included_in?(callbacks)
self.send("#{name.to_s}_#{args[0].class.to_s.underscore}", args[0])
elsif !name.to_s.partially_included_in?(allowed_prefixes)
raise NoMethodError
end
end
end
##models/click.rb
class Click < ActiveRecord::Base
db_magic :slaves => DB_READ_SLAVES
after_create :decrement_campaign_quantity_remaining!
after_create :algorithm_after_create
after_create :check_campaign_daily_limit!
validates_numericality_of :ad_id, :allow_nil => false
validates_numericality_of :app_id, :allow_nil => false
validates_numericality_of :impression_id, :allow_nil => false
validates_numericality_of :user_id, :allow_nil => false
validates_length_of :user_agent, :allow_blank => true, :maximum => 255
belongs_to :ad
belongs_to :app
delegate :campaign, :campaign=, :to => :ad
belongs_to :impression
belongs_to :user
# self_serve clicks currently cost $1/each globally
def self.cost(quantity = 1, discount_multiplier = 1)
cost_per_unit = 100
quantity * cost_per_unit * discount_multiplier
end
protected
# Callback to decrement our campaign's clicks counter after create
# Currently only applies to clicks -- impressions are tracked via infrequent cronjobs
def decrement_campaign_quantity_remaining!
campaign.decrement_quantity_remaining!
end
# Let our campaign know we had another click, and thus potentially reached quota
# FIXME redundant with decrement_campaign_quantity_remaining ???
def check_campaign_daily_limit!
campaign.check_daily_limit!
end
# Create ClickTag records for the subset of tags common to both our user & ad
# FIXME this is duplicated in several models: Click, Retweet, Reply, Favorite, Friendship
def algorithm_after_create
TargetingAlgorithmManager.source(impression).after_create(self)
end
end
##lib/targeting_algorithms/alpha_20110110155619.rb
class TargetingAlgorithmManager::Alpha20110110155619 < TargetingAlgorithmManager
def after_create_click(click)
do_stuff
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment