Permalink
Browse files

Added support for a custom persistence class

  • Loading branch information...
1 parent cc551d8 commit abe752fdc09f33cc6d42ce29609a8d3dc14a8d45 @czarneckid czarneckid committed Sep 15, 2011
Showing with 103 additions and 10 deletions.
  1. +23 −0 README.markdown
  2. +7 −8 lib/activity_feed.rb
  3. +32 −0 spec/activity_feed/custom/item.rb
  4. +24 −0 spec/activity_feed_spec.rb
  5. +14 −2 spec/feed_spec.rb
  6. +3 −0 spec/spec_helper.rb
View
@@ -167,6 +167,29 @@ ruby-1.9.2-p290 :012 > feed.page(1)
ruby-1.9.2-p290 :013 >
```
+### Custom persistence
+
+ActivityFeed can also use a custom class to do more customization. You can set this using:
+
+```ruby
+ActivityFeed.persistence = :custom
+```
+
+This will try to load the following class:
+
+```ruby
+ActivityFeed::Custom::Item""
+```
+
+If you set persistence to be `:foo`, it would try to load the following class:
+
+```ruby
+ActivityFeed::Foo::Item
+```
+
+The custom class should implement a find(item_or_item_id) method that does "the right thing".
+Consult the specs to see this working if you have questions.
+
## Contributing to Activity Feed
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
View
@@ -1,6 +1,7 @@
require 'activity_feed/version'
require 'activity_feed/feed'
require 'active_support/core_ext/module/attribute_accessors'
+require 'active_support/inflector'
require 'redis'
@@ -24,9 +25,7 @@ def self.persistence=(type = :memory)
require 'activity_feed/active_record/item'
klazz = ActivityFeed::ActiveRecord::Item
else
- require 'activity_feed/memory/item'
- klazz = ActivityFeed::Memory::Item
- @@persistence_type = :memory
+ klazz = "ActivityFeed::#{type.to_s.classify}::Item".constantize
end
@@persistence = klazz
@@ -38,16 +37,16 @@ def self.create_item(attributes)
item
end
- def self.load_item(item)
+ def self.load_item(item_or_item_id)
case @@persistence_type
when :memory
- JSON.parse(item)
+ JSON.parse(item_or_item_id)
when :mongo_mapper
- ActivityFeed::MongoMapper::Item.find(item)
+ ActivityFeed::MongoMapper::Item.find(item_or_item_id)
when :active_record
- ActivityFeed::ActiveRecord::Item.find(item)
+ ActivityFeed::ActiveRecord::Item.find(item_or_item_id)
else
- item
+ @@persistence.find(item_or_item_id)
end
end
@@ -0,0 +1,32 @@
+module ActivityFeed
+ module Custom
+ class Item
+ attr_accessor :user_id
+ attr_accessor :nickname
+ attr_accessor :type
+ attr_accessor :title
+ attr_accessor :text
+ attr_accessor :url
+ attr_accessor :icon
+ attr_accessor :sticky
+
+ def initialize(attributes = {})
+ @attributes = attributes
+
+ attributes.each do |key,value|
+ self.send("#{key}=", value)
+ end
+ end
+
+ def self.find(item)
+ JSON.parse(item)
+ end
+
+ def save
+ raise 'user_id MUST be defined in the attributes' if user_id.blank?
+
+ ActivityFeed.redis.zadd("#{ActivityFeed.namespace}:#{ActivityFeed.key}:#{self.user_id}", DateTime.now.to_i, @attributes.to_json)
+ end
+ end
+ end
+end
View
@@ -69,4 +69,28 @@
loaded_item.should == item
end
end
+
+ describe 'custom persistence' do
+ it 'should allow you to define a custom persistence handler class' do
+ ActivityFeed.persistence = :custom
+ end
+
+ it 'should allow you to create a new item using a custom persistence handler class' do
+ user_id = 1
+ ActivityFeed.persistence = :custom
+
+ ActivityFeed.redis.zcard("#{ActivityFeed.namespace}:#{ActivityFeed.key}:#{user_id}").should be(0)
+ ActivityFeed.create_item(:user_id => user_id, :text => 'This is text for my activity feed')
+ ActivityFeed.redis.zcard("#{ActivityFeed.namespace}:#{ActivityFeed.key}:#{user_id}").should be(1)
+ end
+
+ it 'should allow you to load an item using a custom persistence handler class' do
+ user_id = 1
+ ActivityFeed.persistence = :custom
+
+ item = ActivityFeed.create_item(:user_id => user_id, :text => 'This is text for my activity feed')
+ loaded_item = ActivityFeed.load_item(item.to_json)
+ loaded_item.should == JSON.parse(item.to_json)
+ end
+ end
end
View
@@ -1,7 +1,7 @@
require 'spec_helper'
describe ActivityFeed::Feed do
- it 'should pull up the correct list of ActivityFeed:Items when calling #page using :memory' do
+ it 'should pull up the correct list of ActivityFeed::Memory::Item when calling #page using :memory' do
1.upto(5) do |index|
item = ActivityFeed.create_item(:user_id => 1, :nickname => 'nickname_1', :text => "text_#{index}")
end
@@ -10,7 +10,7 @@
feed.page(1).size.should be(5)
end
- it 'should pull up the correct list of ActivityFeed:Items when calling #page using :mongo_mapper' do
+ it 'should pull up the correct list of ActivityFeed::MongoMapper::Item when calling #page using :mongo_mapper' do
ActivityFeed.persistence = :mongo_mapper
ActivityFeed::MongoMapper::Item.count.should be(0)
1.upto(5) do |index|
@@ -39,4 +39,16 @@
feed = ActivityFeed::Feed.new(1)
feed.total_pages.should be(2)
end
+
+ describe 'custom persistence' do
+ it 'should allow you to pull up the correct list of ActivityFeed::Custom::Item when calling #page using :custom' do
+ ActivityFeed.persistence = :custom
+ 1.upto(5) do |index|
+ item = ActivityFeed.create_item(:user_id => 1, :nickname => 'nickname_1', :text => "text_#{index}")
+ end
+
+ feed = ActivityFeed::Feed.new(1)
+ feed.page(1).size.should be(5)
+ end
+ end
end
View
@@ -4,6 +4,7 @@
require 'mongo_mapper'
require 'database_cleaner'
require 'fabrication'
+require 'json'
$redis = Redis.new(:host => '127.0.0.1', :port => 6379)
@@ -45,6 +46,8 @@
ActivityFeed.redis = $redis
+require 'activity_feed/custom/item'
+
RSpec.configure do |config|
config.mock_with :rspec

0 comments on commit abe752f

Please sign in to comment.