Skip to content

Instantly share code, notes, and snippets.

@croaky
Created June 26, 2012 17:26
Show Gist options
  • Save croaky/2997233 to your computer and use it in GitHub Desktop.
Save croaky/2997233 to your computer and use it in GitHub Desktop.
Activity Feed pattern
%ul
- activities.each do |activity|
%li
.activity{ data: { role: activity.activity_type } }
= render_activity activity
#{user_path(bid.user)} offered a bid of #{bid.amount.format} for your job, #{bid_path(bid)}.
class Activity < ActiveRecord::Base
belongs_to :subject, polymorphic: true
belongs_to :user
end
module ApplicationHelper
def render_activity(activity, user = current_user)
activity_type = activity.activity_type
subject = activity.subject
partial = "activities/#{activity_type}"
subject_type = activity.subject_type.underscore.to_sym
render partial, subject_type => subject
end
end
class BidOfferedJob < Struct.new(:bid_id)
PRIORITY = 1
def self.enqueue(bid)
Delayed::Job.enqueue new(bid.id), priority: PRIORITY
end
def perform
Mailer.bid_offered(owner, bid).deliver
Activity.create activity_type: 'bid_offered', subject: bid, user: owner
end
private
def bid
Bid.find bid_id
end
def owner
bid.job.owner
end
end
class RenameActivityNotifications < ActiveRecord::Migration
def up
create_table :activities do |t|
t.timestamps null: false
t.integer :user_id, null: false
t.integer :subject_id, null: false
t.string :subject_type, null: false
t.string :activity_type, null: false
end
add_index :activities, :user_id
add_index :activities, :subject_id
add_index :activities, :subject_type
end
def down
drop_table :activities
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment