This module will allow users to send and receive NewsFeed notifications to any class object, like a user or a company. It comes default with Create, Update, Delete, Send, event types, but these can be customized. There is also an option to specify a custom message.
message = Message.new
current_user = User.new
recipient = User.new
# Creates a news_feed in the database with text "You have created a Message: Message Title" that is sent to recipient.
# To get a list of message, just call 'recipient.news_feed_events'.
# The text can be customized, as well as the recipients.
message.insertNewsFeed(:Create, recipient, current_user)
NewsFeed is developed with Rails 3.2.3. First, include it in your gemfile:
gem 'news_feed'
Run bundle to install it. Then, you will need to run the generator.
rails generate news_feed
Add this migration to your application:
def change
create_table :news_feed_events, :force => true do |t|
t.text :text
t.integer :event_object_id
t.string :event_object_type
t.string :event_type
t.integer :sender_id
t.string :sender_type
t.integer :recipient_id
t.string :recipient_type
t.timestamps
end
end
Include 'acts_as_news_feedable' in the following models:
- Class that you want a list of news_feed_events.
- Class that that you want to call news_feed_events on.
Pass in the news_feed_object_name as a separate definition when defining acts_as_news_feedable. The object name will be used to generate the name of the subject, like a user's full name or a message title, per instance.
# In both classes you can call either user.news_feed_events or user.insertNewsFeed
class User
acts_as_news_feedable
def news_feed_object_name
"Bob Smith"
end
end
class Message
acts_as_news_feedable
def news_feed_object_name
"Message Title"
end
end
You can get the newsfeed for each acts_as_news_feedable class simply by typing:
@news_feeds = current_user.news_feed_events
After you set the instance variable, you can use group_by methods and css classes to customize the view and layout of the news feed. For example, you can create a separate icon for each event_type, and use the object_id to generate a link to that object of interest.
You can add define and customize additional event messages in the lib/news_feed/news_feed_events.rb. Editing the messages themselves can be done in config/locales/en.news_feed.rb.
-
[Content of latest news_feed_events.rb (0.0.6)] (https://github.com/dleung/NewsFeed/wiki/Latest-news_feed_events)
Simple add the corresponding translation in the locales file.