Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renamed store classes

  • Loading branch information...
commit 694228e8501297a5805180ed7c8ca7af1dd494cb 1 parent 16c3120
@cmer authored
Showing with 193 additions and 65 deletions.
  1. +1 −1  CHANGELOG.md
  2. +2 −1  demo/app/models/follow.rb
  3. +1 −1  demo/app/models/like.rb
  4. +1 −1  demo/app/models/mention.rb
  5. +1 −1  lib/generators/socialization/templates/active_record/model_follow.rb
  6. +1 −1  lib/generators/socialization/templates/active_record/model_like.rb
  7. +1 −1  lib/generators/socialization/templates/active_record/model_mention.rb
  8. +1 −1  lib/generators/socialization/templates/redis/model_follow.rb
  9. +1 −1  lib/generators/socialization/templates/redis/model_like.rb
  10. +1 −1  lib/generators/socialization/templates/redis/model_mention.rb
  11. +3 −3 lib/socialization/actors/follower.rb
  12. +3 −3 lib/socialization/actors/liker.rb
  13. +3 −3 lib/socialization/actors/mentioner.rb
  14. +139 −0 lib/socialization/stores/active_record/follow.rb
  15. +1 −5 lib/socialization/stores/active_record/follow_store.rb
  16. +1 −5 lib/socialization/stores/active_record/{like_store.rb → like.rb}
  17. +1 −5 lib/socialization/stores/active_record/{mention_store.rb → mention.rb}
  18. +1 −1  lib/socialization/stores/redis/{follow_store.rb → follow.rb}
  19. +1 −1  lib/socialization/stores/redis/{like_store.rb → like.rb}
  20. +1 −1  lib/socialization/stores/redis/{mention_store.rb → mention.rb}
  21. +3 −3 test/stores/active_record/follow_store_test.rb
  22. +3 −3 test/stores/active_record/like_store_test.rb
  23. +3 −3 test/stores/active_record/mention_store_test.rb
  24. +3 −3 test/stores/redis/follow_store_test.rb
  25. +3 −3 test/stores/redis/like_store_test.rb
  26. +3 −3 test/stores/redis/mention_store_test.rb
  27. +1 −1  test/string_test.rb
  28. +9 −9 test/test_helper.rb
View
2  CHANGELOG.md
@@ -3,7 +3,7 @@
## 0.5.0.beta (June 6, 2012)
* **IMPORTANT:** This release includes many changes, some breaking. Make sure to test your code carefully after upgrading.
-* **BREAKING CHANGE:** Your Like, Follow and Mention models should now inherit the Socialization store base class instead of using the acts_as helper. (e.g.: class Follow < Socialization::ActiveRecordStores::FollowStore). See demo app for an example.
+* **BREAKING CHANGE:** Your Like, Follow and Mention models should now inherit the Socialization store base class instead of using the acts_as helper. (e.g.: class Follow < Socialization::ActiveRecordStores::Follow). See demo app for an example.
* **BREAKING CHANGE:** the `followers`, `followables` etc methods now return an array of objects. Use methods such as `followers_relation` for an ActiveRecord::Relation.
* Changed: The persistence logic has now been moved to the Socialization::ActiveRecordStores namespace. More stores can be easily added.
* Changed: `like!`, `follow!`, and `mention!` now return a boolean. True when the action was successful, false when it wasn't (e.g.: the relationship already exists).
View
3  demo/app/models/follow.rb
@@ -1,2 +1,3 @@
-class Follow < Socialization::ActiveRecordStores::FollowStore
+class Follow < Socialization::ActiveRecordStores::Follow
+ self.table_name = 'follows'
end
View
2  demo/app/models/like.rb
@@ -1,2 +1,2 @@
-class Like < Socialization::ActiveRecordStores::LikeStore
+class Like < Socialization::ActiveRecordStores::Like
end
View
2  demo/app/models/mention.rb
@@ -1,2 +1,2 @@
-class Mention < Socialization::ActiveRecordStores::MentionStore
+class Mention < Socialization::ActiveRecordStores::Mention
end
View
2  lib/generators/socialization/templates/active_record/model_follow.rb
@@ -1,2 +1,2 @@
-class Follow < Socialization::ActiveRecordStores::FollowStore
+class Follow < Socialization::ActiveRecordStores::Follow
end
View
2  lib/generators/socialization/templates/active_record/model_like.rb
@@ -1,2 +1,2 @@
-class Like < Socialization::ActiveRecordStores::LikeStore
+class Like < Socialization::ActiveRecordStores::Like
end
View
2  lib/generators/socialization/templates/active_record/model_mention.rb
@@ -1,2 +1,2 @@
-class Mention < Socialization::ActiveRecordStores::MentionStore
+class Mention < Socialization::ActiveRecordStores::Mention
end
View
2  lib/generators/socialization/templates/redis/model_follow.rb
@@ -1,2 +1,2 @@
-class Follow < Socialization::RedisStores::FollowStore
+class Follow < Socialization::RedisStores::Follow
end
View
2  lib/generators/socialization/templates/redis/model_like.rb
@@ -1,2 +1,2 @@
-class Like < Socialization::RedisStores::LikeStore
+class Like < Socialization::RedisStores::Like
end
View
2  lib/generators/socialization/templates/redis/model_mention.rb
@@ -1,2 +1,2 @@
-class Mention < Socialization::RedisStores::MentionStore
+class Mention < Socialization::RedisStores::Mention
end
View
6 lib/socialization/actors/follower.rb
@@ -18,7 +18,7 @@ def is_follower?
true
end
- # Create a new {FollowStore follow} relationship.
+ # Create a new {Follow follow} relationship.
#
# @param [Followable] followable the object to be followed.
# @return [Boolean]
@@ -27,7 +27,7 @@ def follow!(followable)
Socialization.follow_model.follow!(self, followable)
end
- # Delete a {FollowStore follow} relationship.
+ # Delete a {Follow follow} relationship.
#
# @param [Followable] followable the object to unfollow.
# @return [Boolean]
@@ -36,7 +36,7 @@ def unfollow!(followable)
Socialization.follow_model.unfollow!(self, followable)
end
- # Toggles a {FollowStore follow} relationship.
+ # Toggles a {Follow follow} relationship.
#
# @param [Followable] followable the object to follow/unfollow.
# @return [Boolean]
View
6 lib/socialization/actors/liker.rb
@@ -18,7 +18,7 @@ def is_liker?
true
end
- # Create a new {LikeStore like} relationship.
+ # Create a new {Like like} relationship.
#
# @param [Likeable] likeable the object to be liked.
# @return [Boolean]
@@ -27,7 +27,7 @@ def like!(likeable)
Socialization.like_model.like!(self, likeable)
end
- # Delete a {LikeStore like} relationship.
+ # Delete a {Like like} relationship.
#
# @param [Likeable] likeable the object to unlike.
# @return [Boolean]
@@ -36,7 +36,7 @@ def unlike!(likeable)
Socialization.like_model.unlike!(self, likeable)
end
- # Toggles a {LikeStore like} relationship.
+ # Toggles a {Like like} relationship.
#
# @param [Likeable] likeable the object to like/unlike.
# @return [Boolean]
View
6 lib/socialization/actors/mentioner.rb
@@ -18,7 +18,7 @@ def is_mentioner?
true
end
- # Create a new {MentionStore mention} relationship.
+ # Create a new {Mention mention} relationship.
#
# @param [Mentionable] mentionable the object to be mentioned.
# @return [Boolean]
@@ -27,7 +27,7 @@ def mention!(mentionable)
Socialization.mention_model.mention!(self, mentionable)
end
- # Delete a {MentionStore mention} relationship.
+ # Delete a {Mention mention} relationship.
#
# @param [Mentionable] mentionable the object to unmention.
# @return [Boolean]
@@ -36,7 +36,7 @@ def unmention!(mentionable)
Socialization.mention_model.unmention!(self, mentionable)
end
- # Toggles a {MentionStore mention} relationship.
+ # Toggles a {Mention mention} relationship.
#
# @param [Mentionable] mentionable the object to mention/unmention.
# @return [Boolean]
View
139 lib/socialization/stores/active_record/follow.rb
@@ -0,0 +1,139 @@
+module Socialization
+ module ActiveRecordStores
+ class Follow < ActiveRecord::Base
+ belongs_to :follower, :polymorphic => true
+ belongs_to :followable, :polymorphic => true
+
+ scope :followed_by, lambda { |follower| where(
+ :follower_type => follower.class.table_name.classify,
+ :follower_id => follower.id)
+ }
+
+ scope :following, lambda { |followable| where(
+ :followable_type => followable.class.table_name.classify,
+ :followable_id => followable.id)
+ }
+
+ @@after_create_hook = nil
+ @@after_destroy_hook = nil
+
+ class << self
+ def follow!(follower, followable)
+ unless follows?(follower, followable)
+ self.create! do |follow|
+ follow.follower = follower
+ follow.followable = followable
+ end
+ call_after_create_hook(follower, followable)
+ follower.touch if [:all, :follower].include?(touch) && follower.respond_to?(:touch)
+ followable.touch if [:all, :followable].include?(touch) && followable.respond_to?(:touch)
+ true
+ else
+ false
+ end
+ end
+
+ def unfollow!(follower, followable)
+ if follows?(follower, followable)
+ follow_for(follower, followable).destroy_all
+ call_after_destroy_hook(follower, followable)
+ follower.touch if [:all, :follower].include?(touch) && follower.respond_to?(:touch)
+ followable.touch if [:all, :followable].include?(touch) && followable.respond_to?(:touch)
+ true
+ else
+ false
+ end
+ end
+
+ def follows?(follower, followable)
+ !follow_for(follower, followable).empty?
+ end
+
+ # 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 =>
+ self.select(:follower_id).
+ where(:follower_type => klass.table_name.classify).
+ where(:followable_type => followable.class.to_s).
+ where(:followable_id => followable.id)
+ )
+
+ if opts[:pluck]
+ rel.pluck(opts[:pluck])
+ else
+ rel
+ end
+ end
+
+ # Returns all the followers of a certain type that are following followable
+ def followers(followable, klass, opts = {})
+ rel = followers_relation(followable, klass, opts)
+ if rel.is_a?(ActiveRecord::Relation)
+ rel.all
+ else
+ rel
+ end
+ end
+
+ # 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 =>
+ self.select(:followable_id).
+ where(:followable_type => klass.table_name.classify).
+ where(:follower_type => follower.class.to_s).
+ where(:follower_id => follower.id)
+ )
+
+ if opts[:pluck]
+ rel.pluck(opts[:pluck])
+ else
+ rel
+ end
+ end
+
+ # Returns all the followables of a certain type that are followed by follower
+ def followables(follower, klass, opts = {})
+ rel = followables_relation(follower, klass, opts)
+ if rel.is_a?(ActiveRecord::Relation)
+ rel.all
+ else
+ rel
+ end
+ end
+
+ def touch(what = nil)
+ if what.nil?
+ @touch || false
+ else
+ raise ArgumentError unless [:all, :follower, :followable, false, nil].include?(what)
+ @touch = what
+ end
+ end
+
+ def after_follow(method)
+ raise ArgumentError unless method.is_a?(Symbol) || method.nil?
+ @after_create_hook = method
+ end
+
+ def after_unfollow(method)
+ raise ArgumentError unless method.is_a?(Symbol) || method.nil?
+ @after_destroy_hook = method
+ end
+
+ private
+ def call_after_create_hook(follower, followable)
+ self.send(@after_create_hook, follower, followable) if @after_create_hook
+ end
+
+ def call_after_destroy_hook(follower, followable)
+ self.send(@after_destroy_hook, follower, followable) if @after_destroy_hook
+ end
+
+ def follow_for(follower, followable)
+ followed_by(follower).following(followable)
+ end
+ end # class << self
+
+ end
+ end
+end
View
6 lib/socialization/stores/active_record/follow_store.rb
@@ -1,6 +1,6 @@
module Socialization
module ActiveRecordStores
- class FollowStore < ActiveRecord::Base
+ class Follow < ActiveRecord::Base
belongs_to :follower, :polymorphic => true
belongs_to :followable, :polymorphic => true
@@ -18,10 +18,6 @@ class FollowStore < ActiveRecord::Base
@@after_destroy_hook = nil
class << self
- def table_name
- 'follows'
- end
-
def follow!(follower, followable)
unless follows?(follower, followable)
self.create! do |follow|
View
6 ...zation/stores/active_record/like_store.rb → ...ocialization/stores/active_record/like.rb
@@ -1,6 +1,6 @@
module Socialization
module ActiveRecordStores
- class LikeStore < ActiveRecord::Base
+ class Like < ActiveRecord::Base
belongs_to :liker, :polymorphic => true
belongs_to :likeable, :polymorphic => true
@@ -18,10 +18,6 @@ class LikeStore < ActiveRecord::Base
@@after_destroy_hook = nil
class << self
- def table_name
- 'likes'
- end
-
def like!(liker, likeable)
unless likes?(liker, likeable)
self.create! do |like|
View
6 ...ion/stores/active_record/mention_store.rb → ...alization/stores/active_record/mention.rb
@@ -1,6 +1,6 @@
module Socialization
module ActiveRecordStores
- class MentionStore < ActiveRecord::Base
+ class Mention < ActiveRecord::Base
belongs_to :mentioner, :polymorphic => true
belongs_to :mentionable, :polymorphic => true
@@ -18,10 +18,6 @@ class MentionStore < ActiveRecord::Base
@@after_destroy_hook = nil
class << self
- def table_name
- 'mentions'
- end
-
def mention!(mentioner, mentionable)
unless mentions?(mentioner, mentionable)
self.create! do |mention|
View
2  ...ocialization/stores/redis/follow_store.rb → lib/socialization/stores/redis/follow.rb
@@ -2,7 +2,7 @@
module Socialization
module RedisStores
- class FollowStore < Socialization::RedisStores::Base
+ class Follow < Socialization::RedisStores::Base
class << self
def follow!(follower, followable)
unless follows?(follower, followable)
View
2  lib/socialization/stores/redis/like_store.rb → lib/socialization/stores/redis/like.rb
@@ -2,7 +2,7 @@
module Socialization
module RedisStores
- class LikeStore < Socialization::RedisStores::Base
+ class Like < Socialization::RedisStores::Base
class << self
def like!(liker, likeable)
unless likes?(liker, likeable)
View
2  ...cialization/stores/redis/mention_store.rb → lib/socialization/stores/redis/mention.rb
@@ -2,7 +2,7 @@
module Socialization
module RedisStores
- class MentionStore < Socialization::RedisStores::Base
+ class Mention < Socialization::RedisStores::Base
class << self
def mention!(mentioner, mentionable)
unless mentions?(mentioner, mentionable)
View
6 test/stores/active_record/follow_store_test.rb
@@ -3,7 +3,7 @@
class ActiveRecordFollowStoreTest < Test::Unit::TestCase
context "ActiveRecordStores::FollowStoreTest" do
setup do
- @klass = Socialization::ActiveRecordStores::FollowStore
+ @klass = Socialization::ActiveRecordStores::Follow
@klass.touch nil
@klass.after_follow nil
@klass.after_unfollow nil
@@ -12,8 +12,8 @@ class ActiveRecordFollowStoreTest < Test::Unit::TestCase
end
context "data store" do
- should "inherit Socialization::ActiveRecordStores::FollowStore" do
- assert_equal Socialization::ActiveRecordStores::FollowStore, Socialization.follow_model
+ should "inherit Socialization::ActiveRecordStores::Follow" do
+ assert_equal Socialization::ActiveRecordStores::Follow, Socialization.follow_model
end
end
View
6 test/stores/active_record/like_store_test.rb
@@ -3,7 +3,7 @@
class ActiveRecordLikeStoreTest < Test::Unit::TestCase
context "ActiveRecordStore::LikeStoreTest" do
setup do
- @klass = Socialization::ActiveRecordStores::LikeStore
+ @klass = Socialization::ActiveRecordStores::Like
@klass.touch nil
@klass.after_like nil
@klass.after_unlike nil
@@ -12,8 +12,8 @@ class ActiveRecordLikeStoreTest < Test::Unit::TestCase
end
context "data store" do
- should "inherit Socialization::ActiveRecordStores::LikeStore" do
- assert_equal Socialization::ActiveRecordStores::LikeStore, Socialization.like_model
+ should "inherit Socialization::ActiveRecordStores::Like" do
+ assert_equal Socialization::ActiveRecordStores::Like, Socialization.like_model
end
end
View
6 test/stores/active_record/mention_store_test.rb
@@ -3,7 +3,7 @@
class ActiveRecordMentionStoreTest < Test::Unit::TestCase
context "ActiveRecordStores::MentionStoreTest" do
setup do
- @klass = Socialization::ActiveRecordStores::MentionStore
+ @klass = Socialization::ActiveRecordStores::Mention
@klass.touch nil
@klass.after_mention nil
@klass.after_unmention nil
@@ -12,8 +12,8 @@ class ActiveRecordMentionStoreTest < Test::Unit::TestCase
end
context "data store" do
- should "inherit Socialization::ActiveRecordStores::MentionStore" do
- assert_equal Socialization::ActiveRecordStores::MentionStore, Socialization.mention_model
+ should "inherit Socialization::ActiveRecordStores::Mention" do
+ assert_equal Socialization::ActiveRecordStores::Mention, Socialization.mention_model
end
end
View
6 test/stores/redis/follow_store_test.rb
@@ -4,7 +4,7 @@ class RedisFollowStoreTest < Test::Unit::TestCase
context "RedisStores::FollowStoreTest" do
setup do
use_redis_store
- @klass = Socialization::RedisStores::FollowStore
+ @klass = Socialization::RedisStores::Follow
@klass.touch nil
@klass.after_follow nil
@klass.after_unfollow nil
@@ -13,8 +13,8 @@ class RedisFollowStoreTest < Test::Unit::TestCase
end
context "Stores" do
- should "inherit Socialization::RedisStores::FollowStore" do
- assert_equal Socialization::RedisStores::FollowStore, Socialization.follow_model
+ should "inherit Socialization::RedisStores::Follow" do
+ assert_equal Socialization::RedisStores::Follow, Socialization.follow_model
end
end
View
6 test/stores/redis/like_store_test.rb
@@ -4,7 +4,7 @@ class RedisLikeStoreTest < Test::Unit::TestCase
context "RedisStores::LikeStoreTest" do
setup do
use_redis_store
- @klass = Socialization::RedisStores::LikeStore
+ @klass = Socialization::RedisStores::Like
@klass.touch nil
@klass.after_like nil
@klass.after_unlike nil
@@ -13,8 +13,8 @@ class RedisLikeStoreTest < Test::Unit::TestCase
end
context "Stores" do
- should "inherit Socialization::RedisStores::LikeStore" do
- assert_equal Socialization::RedisStores::LikeStore, Socialization.like_model
+ should "inherit Socialization::RedisStores::Like" do
+ assert_equal Socialization::RedisStores::Like, Socialization.like_model
end
end
View
6 test/stores/redis/mention_store_test.rb
@@ -4,7 +4,7 @@ class RedisMentionStoreTest < Test::Unit::TestCase
context "RedisStores::MentionStoreTest" do
setup do
use_redis_store
- @klass = Socialization::RedisStores::MentionStore
+ @klass = Socialization::RedisStores::Mention
@klass.touch nil
@klass.after_mention nil
@klass.after_unmention nil
@@ -13,8 +13,8 @@ class RedisMentionStoreTest < Test::Unit::TestCase
end
context "Stores" do
- should "inherit Socialization::RedisStores::MentionStore" do
- assert_equal Socialization::RedisStores::MentionStore, Socialization.mention_model
+ should "inherit Socialization::RedisStores::Mention" do
+ assert_equal Socialization::RedisStores::Mention, Socialization.mention_model
end
end
View
2  test/string_test.rb
@@ -5,7 +5,7 @@ class StringTest < Test::Unit::TestCase
should "return a class" do
assert_equal Socialization, "Socialization".deep_const_get
assert_equal Socialization::ActiveRecordStores, "Socialization::ActiveRecordStores".deep_const_get
- assert_equal Socialization::ActiveRecordStores::FollowStore, "Socialization::ActiveRecordStores::FollowStore".deep_const_get
+ assert_equal Socialization::ActiveRecordStores::Follow, "Socialization::ActiveRecordStores::Follow".deep_const_get
assert_raise(NameError) { "Foo::Bar".deep_const_get }
end
View
18 test/test_helper.rb
@@ -42,16 +42,16 @@ def teardown
end
def use_redis_store
- Socialization.follow_model = Socialization::RedisStores::FollowStore
- Socialization.mention_model = Socialization::RedisStores::MentionStore
- Socialization.like_model = Socialization::RedisStores::LikeStore
+ Socialization.follow_model = Socialization::RedisStores::Follow
+ Socialization.mention_model = Socialization::RedisStores::Mention
+ Socialization.like_model = Socialization::RedisStores::Like
setup_model_shortcuts
end
def use_ar_store
- Socialization.follow_model = Socialization::ActiveRecordStores::FollowStore
- Socialization.mention_model = Socialization::ActiveRecordStores::MentionStore
- Socialization.like_model = Socialization::ActiveRecordStores::LikeStore
+ Socialization.follow_model = Socialization::ActiveRecordStores::Follow
+ Socialization.mention_model = Socialization::ActiveRecordStores::Mention
+ Socialization.like_model = Socialization::ActiveRecordStores::Like
setup_model_shortcuts
end
@@ -176,9 +176,9 @@ class Movie < ActiveRecord::Base
has_many :comments
end
-# class Follow < Socialization::ActiveRecordStores::FollowStore; end
-# class Like < Socialization::ActiveRecordStores::LikeStore; end
-# class Mention < Socialization::ActiveRecordStores::MentionStore; end
+# class Follow < Socialization::ActiveRecordStores::Follow; end
+# class Like < Socialization::ActiveRecordStores::Like; end
+# class Mention < Socialization::ActiveRecordStores::Mention; end
class ImAFollower < ActiveRecord::Base
acts_as_follower
Please sign in to comment.
Something went wrong with that request. Please try again.