README.markdown in activity_feed-2.3.0 vs README.markdown in activity_feed-3.0.0
- old
+ new
@@ -40,12 +40,14 @@
* `aggregate_key`: Further isolates the aggregate ActivityFeed data.
* `page_size`: Number of activity feed items to be retrieved per-page.
### Advanced configuration options
-* `item_loader`: ActivityFeed supports loading items from your ORM (e.g. ActiveRecord) or your ODM (e.g. Mongoid) when a page for a user's activity feed is requested. This option should be set to a Proc that will be called passing the item ID as its only argument.
+* `items_loader`: ActivityFeed supports loading items from your ORM (e.g. ActiveRecord) or your ODM (e.g. Mongoid) when a page for a user's activity feed is requested. This option should be set to a Proc that will be called passing the item IDs as its only argument.
+NOTE: The following examples developing an activity feed with Mongoid using Mongoid 3.x.
+
For example:
Assume you have defined a class for storing your activity feed items in Mongoid as follows:
```ruby
@@ -66,28 +68,33 @@
field :text, type: String
field :url, type: String
field :icon, type: String
field :sticky, type: Boolean
- index :user_id
+ index({ user_id: 1 }
after_save :update_item_in_activity_feed
+ after_destroy :remove_item_from_activity_feed
private
def update_item_in_activity_feed
ActivityFeed.update_item(self.user_id, self.id, self.updated_at.to_i)
end
+
+ def remove_item_from_activity_feed
+ ActivityFeed.remove_item(self.user_id, self.id)
+ end
end
end
end
```
You would add the following option where you are configuring ActivityFeed as follows:
```ruby
-ActivityFeed.item_loader = Proc.new { |id| ActivityFeed::Mongoid::Item.find(id) }
+ActivityFeed.items_loader = Proc.new { |ids| ActivityFeed::Mongoid::Item.where(:id.in => ids).order_by(updated_at: :desc).to_a }
```
If you need to handle any exceptions when loading activity feed items, please do this in the Proc.
## Usage
@@ -101,13 +108,11 @@
```ruby
# Configure Mongoid
require 'mongoid'
-Mongoid.configure do |config|
- config.master = Mongo::Connection.new.db("activity_feed_gem_test")
-end
+Mongoid.load!("/path/to/your/mongoid.yml", :production)
# Create a class for activity feed items
module ActivityFeed
module Mongoid
class Item
@@ -123,11 +128,11 @@
field :text, type: String
field :url, type: String
field :icon, type: String
field :sticky, type: Boolean
- index :user_id
+ index({ user_id: 1 }
after_save :update_item_in_activity_feed
after_destroy :remove_item_from_activity_feed
private
@@ -150,11 +155,11 @@
configuration.redis = Redis.new(:host => '127.0.0.1', :port => 6379)
configuration.namespace = 'activity_feed'
configuration.aggregate = false
configuration.aggregate_key = 'aggregate'
configuration.page_size = 25
- configuration.item_loader = Proc.new { |id| ActivityFeed::Mongoid::Item.find(id) }
+ configuration.items_loader = Proc.new { |ids| ActivityFeed::Mongoid::Item.where(:id.in => ids).order_by(updated_at: :desc).to_a }
end
# Create a couple of activity feed items
activity_item_1 = ActivityFeed::Mongoid::Item.create(
:user_id => 'david',
@@ -191,13 +196,11 @@
```ruby
# Configure Mongoid
require 'mongoid'
-Mongoid.configure do |config|
- config.master = Mongo::Connection.new.db("activity_feed_gem_test")
-end
+Mongoid.load!("/path/to/your/mongoid.yml", :production)
# Create a class for activity feed items
module ActivityFeed
module Mongoid
class Item
@@ -232,11 +235,11 @@
configuration.redis = Redis.new(:host => '127.0.0.1', :port => 6379)
configuration.namespace = 'activity_feed'
configuration.aggregate = true
configuration.aggregate_key = 'aggregate'
configuration.page_size = 25
- configuration.item_loader = Proc.new { |id| ActivityFeed::Mongoid::Item.find(id) }
+ configuration.items_loader = Proc.new { |ids| ActivityFeed::Mongoid::Item.where(:id.in => ids).order_by(updated_at: :desc).to_a }
end
# Create activity feed items for a couple of users and aggregate the activity feed items from the second user in the first user's activity feed
1.upto(5) do |index|
ActivityFeed::Mongoid::Item.create(
@@ -298,9 +301,10 @@
ActivityFeed.total_pages(user_id, aggregate = ActivityFeed.aggregate, page_size = ActivityFeed.page_size)
ActivityFeed.total_items_in_feed(user_id, aggregate = ActivityFeed.aggregate)
ActivityFeed.total_items(user_id, aggregate = ActivityFeed.aggregate)
ActivityFeed.trim_feed(user_id, starting_timestamp, ending_timestamp, aggregate = ActivityFeed.aggregate)
ActivityFeed.expire_feed(user_id, seconds, aggregate = ActivityFeed.aggregate)
+ActivityFeed.expire_feed_in(user_id, seconds, aggregate = ActivityFeed.aggregate)
ActivityFeed.expire_feed_at(user_id, timestamp, aggregate = ActivityFeed.aggregate)
ActivityFeed.remove_feeds(user_id)
```
## Contributing to ActivityFeed
\ No newline at end of file