Skip to content

Instantly share code, notes, and snippets.

@mickey
Created May 7, 2010 15:25
Show Gist options
  • Save mickey/393562 to your computer and use it in GitHub Desktop.
Save mickey/393562 to your computer and use it in GitHub Desktop.
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :target, :polymorphic => true
#validates_presence_of :user, :activity_type, :target
AUDITION_POSTED = 1 #target --> audition
AUDITION_FEEDBACK_UPDATED = 2 #target --> audition
AUDITION_VIEWED = 3 #target --> video
NEW_PART = 4 #target --> part
class << self
def add(user, activity_type, target)
return false if user.blank? or activity_type.blank? or target.blank?
activity = Activity.new(:user => user, :activity_type => activity_type, :target => target)
activity.save!
end
#TODO : maybe do it a a delayed job
def batch_add(users, activity_type, target)
return false if users.blank? or activity_type.blank? or target.blank?
inserts = []
users.each {|user| inserts.push "('#{user.id}', '#{activity_type}', '#{target.id}', '#{target.class}', NOW())" }
sql = "INSERT INTO activities (`user_id`, `activity_type`, `target_id`, `target_type`, `created_at`) VALUES #{inserts.join(", ")}"
ActiveRecord::Base.connection.execute sql
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment