Last active
December 10, 2015 11:48
-
-
Save litch/4429748 to your computer and use it in GitHub Desktop.
I'm trying to decide what's the best way to build this out. Basically I'm going to observe all kinds of events in the system and create feed items for users based on a number of criteria - "John changed the Status of Job: JUE-923 to active". I'm facing a diliema on how to build the actual FeedItemGenerator part. Below are two ways and how they w…
This file contains hidden or 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
#This works but is maybe not so great. | |
class FeedItemGenerator | |
include ActionView::Helpers | |
attr_accessor :feedable | |
attr_accessor :user | |
attr_accessor :event | |
def initialize(feedable, user, event=nil) | |
@feedable = feedable | |
@user = user | |
@event = event | |
self.extend CommentFeedItem if feedable.class.name == "Comment" | |
self.extend JobFeedItem if feedable.class.name == "Job" | |
self.extend TimesheetFeedItem if feedable.class.name == "Timesheet" | |
end | |
def generate | |
fi = FeedItem.new(glyph: glyph, message: feed_item_message, sender_id: user.id, feedable_url: feedable_url) | |
Feed.all.each do |feed| | |
feed << fi | |
end | |
end | |
end | |
module CommentFeedItem | |
def feedable_url | |
"#{feedable.commentable.class.name.underscore}/#{feedable.commentable.id}" | |
end | |
def glyph | |
"n" | |
end | |
def feed_item_message | |
"#{user.name} commented on #{feedable.commentable.class.name}: #{feedable.commentable.name}" | |
end | |
end | |
module JobFeedItem | |
def feedable_url | |
"#{feedable.class.name.underscore}/#{feedable.id}" | |
end | |
def glyph | |
"k" | |
end | |
def feed_item_message | |
"#{user.name} changed status of #{feedable.class.name}: #{feedable.name} to #{feedable.state.humanize.titleize}" | |
end | |
end | |
module TimesheetFeedItem | |
def feedable_url | |
"jobs/#{feedable.job.id}/#{feedable.class.name.underscore}/#{feedable.id}" | |
end | |
def glyph | |
"i" | |
end | |
def feed_item_message | |
"#{sender.full_name} #{event.to_s}d a new timesheet for #{feedable.employee_full_name}." | |
end | |
end | |
This file contains hidden or 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
class FeedItemGenerator | |
include ActionView::Helpers | |
attr_accessor :feedable | |
attr_accessor :user | |
attr_accessor :event | |
def initialize(feedable, user, event=nil) | |
@feedable = feedable | |
@user = user | |
@event = event | |
end | |
def generate | |
fi = FeedItem.new(glyph: glyph, message: feed_item_message, sender_id: user.id, feedable_url: feedable_url) | |
Feed.all.each do |feed| | |
feed << fi | |
end | |
end | |
end | |
class CommentFeedItem < FeedItemGenerator | |
def feedable_url | |
"#{feedable.commentable.class.name.underscore}/#{feedable.commentable.id}" | |
end | |
def glyph | |
"n" | |
end | |
def feed_item_message | |
"#{user.name} commented on #{feedable.commentable.class.name}: #{feedable.commentable.name}" | |
end | |
end | |
class JobFeedItem < FeedItemGenerator | |
def feedable_url | |
"#{feedable.class.name.underscore}/#{feedable.id}" | |
end | |
def glyph | |
"k" | |
end | |
def feed_item_message | |
"#{user.name} changed status of #{feedable.class.name}: #{feedable.name} to #{feedable.state.humanize.titleize}" | |
end | |
end | |
class TimesheetFeedItem < FeedItemGenerator | |
def feedable_url | |
"jobs/#{feedable.job.id}/#{feedable.class.name.underscore}/#{feedable.id}" | |
end | |
def glyph | |
"i" | |
end | |
def feed_item_message | |
"#{sender.full_name} #{event.to_s}d a new timesheet for #{feedable.employee_full_name}." | |
end | |
end | |
This file contains hidden or 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
#Using the extending modules method, this is then called in, say, an observer like so: | |
class TimesheetObserver < ActiveRecord | |
def after_create(timesheet) | |
FeedItemGenerator.new(timesheet, current_user, :create).generate | |
end | |
end | |
#Or, using the subclassing method: | |
class TimesheetObserver < ActiveRecord | |
def after_create(timesheet) | |
TimesheetFeedItem(timesheet, current_user, :create) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment