Permalink
Browse files

Added Redis store and refactored further to make this possible

  • Loading branch information...
cmer committed Jun 6, 2012
1 parent 280f940 commit 2794f65b7651bc2ecd3fd1a3252127cd23e926b9
Showing with 1,152 additions and 133 deletions.
  1. +1 −1 lib/generators/socialization/socialization_generator.rb
  2. +1 −0 lib/socialization.rb
  3. +4 −4 lib/socialization/actors/follower.rb
  4. +4 −4 lib/socialization/actors/liker.rb
  5. +4 −4 lib/socialization/actors/mentioner.rb
  6. +45 −0 lib/socialization/config.rb
  7. +4 −0 lib/socialization/helpers/string.rb
  8. +3 −3 lib/socialization/stores/active_record/follow_store.rb
  9. +3 −3 lib/socialization/stores/active_record/like_store.rb
  10. +3 −3 lib/socialization/stores/active_record/mention_store.rb
  11. +8 −0 lib/socialization/stores/redis/base.rb
  12. +11 −0 lib/socialization/stores/redis/config.rb
  13. +126 −0 lib/socialization/stores/redis/follow_store.rb
  14. +126 −0 lib/socialization/stores/redis/like_store.rb
  15. +126 −0 lib/socialization/stores/redis/mention_store.rb
  16. +2 −2 lib/socialization/victims/followable.rb
  17. +2 −2 lib/socialization/victims/likeable.rb
  18. +2 −2 lib/socialization/victims/mentionable.rb
  19. +1 −0 socialization.gemspec
  20. +8 −8 test/actors/follower_test.rb
  21. +8 −8 test/actors/liker_test.rb
  22. +8 −8 test/actors/mentioner_test.rb
  23. +6 −0 test/stores/active_record/follow_store_test.rb
  24. +6 −0 test/stores/active_record/like_store_test.rb
  25. +6 −0 test/stores/active_record/mention_store_test.rb
  26. +30 −0 test/stores/redis/config_test.rb
  27. +153 −0 test/stores/redis/follow_store_test.rb
  28. +153 −0 test/stores/redis/like_store_test.rb
  29. +153 −0 test/stores/redis/mention_store_test.rb
  30. +52 −3 test/test_helper.rb
  31. +4 −4 test/victims/followable_test.rb
  32. +4 −4 test/victims/likeable_test.rb
  33. +4 −4 test/victims/mentionable_test.rb
  34. +81 −66 test/world_test.rb
@@ -1,7 +1,7 @@
require 'rails/generators'
require 'rails/generators/migration'
-STORES = %w(active_record)
+STORES = %w(active_record redis)
class SocializationGenerator < Rails::Generators::Base
include Rails::Generators::Migration
View
@@ -1,3 +1,4 @@
+require 'socialization/config'
require 'socialization/acts_as_helpers'
ActiveRecord::Base.send :include, Socialization::ActsAsHelpers
@@ -24,7 +24,7 @@ def is_follower?
# @return [Boolean]
def follow!(followable)
raise ArgumentError, "#{followable} is not followable!" unless followable.respond_to?(:is_followable?) && followable.is_followable?
- Follow.follow!(self, followable)
+ Socialization.follow_model.follow!(self, followable)
end
# Delete a {FollowStores follow} relationship.
@@ -33,7 +33,7 @@ def follow!(followable)
# @return [Boolean]
def unfollow!(followable)
raise ArgumentError, "#{followable} is not followable!" unless followable.respond_to?(:is_followable?) && followable.is_followable?
- Follow.unfollow!(self, followable)
+ Socialization.follow_model.unfollow!(self, followable)
end
# Toggles a {FollowStores follow} relationship.
@@ -57,7 +57,7 @@ def toggle_follow!(followable)
# @return [Boolean]
def follows?(followable)
raise ArgumentError, "#{followable} is not followable!" unless followable.respond_to?(:is_followable?) && followable.is_followable?
- Follow.follows?(self, followable)
+ Socialization.follow_model.follows?(self, followable)
end
# Returns all the followables of a certain type that are followed by self
@@ -66,7 +66,7 @@ def follows?(followable)
# @params [Hash] opts a hash of options
# @return [Array<Followable, Numeric>] An array of Followable objects or IDs
def followables(klass, opts = {})
- Follow.followables(self, klass, opts)
+ Socialization.follow_model.followables(self, klass, opts)
end
end
@@ -24,7 +24,7 @@ def is_liker?
# @return [Boolean]
def like!(likeable)
raise ArgumentError, "#{likeable} is not likeable!" unless likeable.respond_to?(:is_likeable?) && likeable.is_likeable?
- Like.like!(self, likeable)
+ Socialization.like_model.like!(self, likeable)
end
# Delete a {LikeStores like} relationship.
@@ -33,7 +33,7 @@ def like!(likeable)
# @return [Boolean]
def unlike!(likeable)
raise ArgumentError, "#{likeable} is not likeable!" unless likeable.respond_to?(:is_likeable?) && likeable.is_likeable?
- Like.unlike!(self, likeable)
+ Socialization.like_model.unlike!(self, likeable)
end
# Toggles a {LikeStores like} relationship.
@@ -57,7 +57,7 @@ def toggle_like!(likeable)
# @return [Boolean]
def likes?(likeable)
raise ArgumentError, "#{likeable} is not likeable!" unless likeable.respond_to?(:is_likeable?) && likeable.is_likeable?
- Like.likes?(self, likeable)
+ Socialization.like_model.likes?(self, likeable)
end
# Returns all the likeables of a certain type that are liked by self
@@ -66,7 +66,7 @@ def likes?(likeable)
# @params [Hash] opts a hash of options
# @return [Array<Likeable, Numeric>] An array of Likeable objects or IDs
def likeables(klass, opts = {})
- Like.likeables(self, klass, opts)
+ Socialization.like_model.likeables(self, klass, opts)
end
end
@@ -24,7 +24,7 @@ def is_mentioner?
# @return [Boolean]
def mention!(mentionable)
raise ArgumentError, "#{mentionable} is not mentionable!" unless mentionable.respond_to?(:is_mentionable?) && mentionable.is_mentionable?
- Mention.mention!(self, mentionable)
+ Socialization.mention_model.mention!(self, mentionable)
end
# Delete a {MentionStores mention} relationship.
@@ -33,7 +33,7 @@ def mention!(mentionable)
# @return [Boolean]
def unmention!(mentionable)
raise ArgumentError, "#{mentionable} is not mentionable!" unless mentionable.respond_to?(:is_mentionable?) && mentionable.is_mentionable?
- Mention.unmention!(self, mentionable)
+ Socialization.mention_model.unmention!(self, mentionable)
end
# Toggles a {MentionStores mention} relationship.
@@ -57,7 +57,7 @@ def toggle_mention!(mentionable)
# @return [Boolean]
def mentions?(mentionable)
raise ArgumentError, "#{mentionable} is not mentionable!" unless mentionable.respond_to?(:is_mentionable?) && mentionable.is_mentionable?
- Mention.mentions?(self, mentionable)
+ Socialization.mention_model.mentions?(self, mentionable)
end
# Returns all the mentionables of a certain type that are mentioned by self
@@ -66,7 +66,7 @@ def mentions?(mentionable)
# @params [Hash] opts a hash of options
# @return [Array<Mentionable, Numeric>] An array of Mentionable objects or IDs
def mentionables(klass, opts = {})
- Mention.mentionables(self, klass, opts)
+ Socialization.mention_model.mentionables(self, klass, opts)
end
end
@@ -0,0 +1,45 @@
+module Socialization
+ class << self
+ def follow_model
+ if @follow_model
+ @follow_model
+ elsif defined?(::Follow)
+ ::Follow
+ else
+ raise RuntimeError.new("No Follow model has been defined.")
+ end
+ end
+
+ def follow_model=(klass)
+ @follow_model = klass
+ end
+
+ def like_model
+ if @like_model
+ @like_model
+ elsif defined?(::Like)
+ ::Like
+ else
+ raise RuntimeError.new("No Like model has been defined.")
+ end
+ end
+
+ def like_model=(klass)
+ @like_model = klass
+ end
+
+ def mention_model
+ if @mention_model
+ @mention_model
+ elsif defined?(::Mention)
+ ::Mention
+ else
+ raise RuntimeError.new("No Mention model has been defined.")
+ end
+ end
+
+ def mention_model=(klass)
+ @mention_model = klass
+ end
+ end
+end
@@ -10,4 +10,8 @@ def deep_const_get
result
end
+ def is_integer?
+ self.to_i.to_s == self
+ end
+
end
@@ -24,7 +24,7 @@ def table_name
def follow!(follower, followable)
unless follows?(follower, followable)
- Follow.create! do |follow|
+ self.create! do |follow|
follow.follower = follower
follow.followable = followable
end
@@ -56,7 +56,7 @@ def follows?(follower, followable)
# Returns an ActiveRecord::Relation of all the followers of a certain type that are following followable
def followers_relation(followable, klass, opts = {})
rel = klass.where(:id =>
- Follow.select(:follower_id).
+ self.select(:follower_id).
where(:follower_type => klass.table_name.classify).
where(:followable_type => followable.class.to_s).
where(:followable_id => followable.id)
@@ -82,7 +82,7 @@ def followers(followable, klass, opts = {})
# Returns an ActiveRecord::Relation of all the followables of a certain type that are followed by follower
def followables_relation(follower, klass, opts = {})
rel = klass.where(:id =>
- Follow.select(:followable_id).
+ self.select(:followable_id).
where(:followable_type => klass.table_name.classify).
where(:follower_type => follower.class.to_s).
where(:follower_id => follower.id)
@@ -24,7 +24,7 @@ def table_name
def like!(liker, likeable)
unless likes?(liker, likeable)
- Like.create! do |like|
+ self.create! do |like|
like.liker = liker
like.likeable = likeable
end
@@ -56,7 +56,7 @@ def likes?(liker, likeable)
# Returns an ActiveRecord::Relation of all the likers of a certain type that are liking likeable
def likers_relation(likeable, klass, opts = {})
rel = klass.where(:id =>
- Like.select(:liker_id).
+ self.select(:liker_id).
where(:liker_type => klass.table_name.classify).
where(:likeable_type => likeable.class.to_s).
where(:likeable_id => likeable.id)
@@ -82,7 +82,7 @@ def likers(likeable, klass, opts = {})
# Returns an ActiveRecord::Relation of all the likeables of a certain type that are liked by liker
def likeables_relation(liker, klass, opts = {})
rel = klass.where(:id =>
- Like.select(:likeable_id).
+ self.select(:likeable_id).
where(:likeable_type => klass.table_name.classify).
where(:liker_type => liker.class.to_s).
where(:liker_id => liker.id)
@@ -24,7 +24,7 @@ def table_name
def mention!(mentioner, mentionable)
unless mentions?(mentioner, mentionable)
- Mention.create! do |mention|
+ self.create! do |mention|
mention.mentioner = mentioner
mention.mentionable = mentionable
end
@@ -56,7 +56,7 @@ def mentions?(mentioner, mentionable)
# Returns an ActiveRecord::Relation of all the mentioners of a certain type that are mentioning mentionable
def mentioners_relation(mentionable, klass, opts = {})
rel = klass.where(:id =>
- Mention.select(:mentioner_id).
+ self.select(:mentioner_id).
where(:mentioner_type => klass.table_name.classify).
where(:mentionable_type => mentionable.class.to_s).
where(:mentionable_id => mentionable.id)
@@ -82,7 +82,7 @@ def mentioners(mentionable, klass, opts = {})
# Returns an ActiveRecord::Relation of all the mentionables of a certain type that are mentioned by mentioner
def mentionables_relation(mentioner, klass, opts = {})
rel = klass.where(:id =>
- Mention.select(:mentionable_id).
+ self.select(:mentionable_id).
where(:mentionable_type => klass.table_name.classify).
where(:mentioner_type => mentioner.class.to_s).
where(:mentioner_id => mentioner.id)
@@ -0,0 +1,8 @@
+module Socialization
+ module RedisStores
+ class Base
+ @@after_create_hook = nil
+ @@after_destroy_hook = nil
+ end
+ end
+end
@@ -0,0 +1,11 @@
+module Socialization
+ class << self
+ def redis
+ @redis ||= Redis.new
+ end
+
+ def redis=(redis)
+ @redis = redis
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 2794f65

Please sign in to comment.