Created
February 10, 2009 10:07
-
-
Save juliocesar/61334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Maybe soon to be a feeds plugin. So you go like | |
# | |
# class Message < ActiveRecord::Base | |
# generates_feed do | |
# after :create, :like => ":sender has sent a message to :recipient" | |
# end | |
# end | |
# | |
# Capisce? Pure beauty. | |
# ps: Props to thinking_sphinx | |
class FeedEntry < ActiveRecord::Base | |
belongs_to :owner, :polymorphic => true | |
# t.text :body | |
# t.integer :owner_id | |
# t.string :owner_type | |
end | |
module Feed | |
def self.included(base) | |
base.extend ClassMethods | |
base.class_eval do | |
has_many :feed_entries, :as => :owner | |
end | |
end | |
module ClassMethods | |
def generates_feed(&block) | |
feeder = Feeder.new(&block) | |
feeder.callbacks.each do |method, proc| | |
send(method, &proc) | |
end | |
end | |
end | |
class Feeder | |
attr_accessor :callbacks | |
def initialize(&block) | |
@callbacks = {} | |
self.instance_eval &block | |
end | |
def after(*args) | |
options = args.last | |
@callbacks["after_#{args.first}".to_sym] = Proc.new do |record| | |
body = in_ur_colonz(options[:like], record) | |
FeedEntry.create :body => body, :owner => record | |
end | |
end | |
def in_ur_colonz(text, something) | |
text.gsub(/\:(\w+)/) { something.send($1) } # :-O | |
end | |
end | |
end | |
ActiveRecord::Base.send(:include, Feed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment