Skip to content

Instantly share code, notes, and snippets.

@dougo-chris
Created August 26, 2010 23:15
Show Gist options
  • Save dougo-chris/552441 to your computer and use it in GitHub Desktop.
Save dougo-chris/552441 to your computer and use it in GitHub Desktop.
# FINDING AD VIEWS
# klass = AdView
# klass = klass.by_campaign_id(@campaign.id)
# klass = klass.order_event_time('DESC')
#
# @ad_views = klass.paginate(:page => params[:page], :per_page => 100 )
#
# CREATING AD VIEWS
# AdView.by_activated_campaign_id(@campaign.id]).create(: ... )
#
####################################################
# CREATE THE ADVIEW CLASS WHEN A CAMPAIGN IS CREATED
# class Campaign < ActiveRecord::Base
#
# after_create :create_supporting_tables
#
# def create_supporting_tables
# AdView.by_activated_campaign_id(self.id).create_table
#
class AdView < ActiveRecord::Base
EVENT_AD_STARTED = 0
EVENT_AD_FINISHED = 1
EVENT_AD_CLICKED = 2
EVENT_AD_CLOSED = 3
belongs_to :content
belongs_to :ad_creative
def self.by_activated_campaign_id(campaign_id)
init_ad_view_table(campaign_id)
end
def self.create_table
ActiveRecord::Migration.create_table(table_name) do |t|
t.integer "content_id"
t.integer "ad_creative_id"
t.datetime "event_time"
t.integer "event_type"
t.string "selector_uuid"
t.string "ip_address"
end
ActiveRecord::Migration.add_index table_name, :event_time
ActiveRecord::Migration.add_index table_name, [:event_type, :event_time]
ActiveRecord::Migration.add_index table_name, :selector_uuid
end
def self.drop_table
ActiveRecord::Migration.drop_table(table_name)
end
private
def self.init_ad_view_table(campaign_id)
klass = Class.new(AdView)
def klass.setup_table_name campaign_id
set_table_name "ad_view_#{campaign_id}".to_sym
end
klass.setup_table_name(campaign_id)
klass
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment