Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow toggling follows/likes/mentions on and off. #5

Merged
merged 2 commits into from

2 participants

@balvig

Useful for Twitter like toggle buttons where you can click it to toggle following on and off!

balvig added some commits
@balvig balvig Allow toggling follows/likes/mentions on and off.
Useful for Twitter like toggle buttons where you
can click it to toggle following on and off.
cd0eae4
@balvig balvig Added support for STI. 0f6fcfd
@balvig

Also added support for liking/following models using STI since it was needed for our project.

@cmer cmer merged commit 0f6fcfd into cmer:master
@cmer
Owner

Thanks for this! I made as few changes to your code and tests.

In the future, could you please send separate pull requests for different features? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 28, 2012
  1. @balvig

    Allow toggling follows/likes/mentions on and off.

    balvig authored
    Useful for Twitter like toggle buttons where you
    can click it to toggle following on and off.
  2. @balvig

    Added support for STI.

    balvig authored
This page is out of date. Refresh to see the latest.
View
14 lib/socialization/follower.rb
@@ -45,13 +45,25 @@ def unfollow!(followable)
true
end
+ # Toggles a {FollowStore follow} relationship.
+ #
+ # @param [Followable] followable the object to follow/unfollow.
+ # @return [Boolean]
+ def toggle_follow!(followable)
+ if follows?(followable)
+ unfollow!(followable)
+ else
+ follow!(followable)
+ end
+ end
+
# Specifies if self follows a {Followable} object.
#
# @param [Followable] followable the {Followable} object to test against.
# @return [Boolean]
def follows?(followable)
raise ArgumentError, "#{followable} is not followable!" unless followable.is_followable?
- !self.follows.where(:followable_type => followable.class.to_s, :followable_id => followable.id).empty?
+ !self.follows.where(:followable_type => followable.class.table_name.classify, :followable_id => followable.id).empty?
end
# Returns a scope of the {Followable}s followed by self.
View
14 lib/socialization/liker.rb
@@ -44,13 +44,25 @@ def unlike!(likeable)
end
end
+ # Toggles a {LikeStore like} relationship.
+ #
+ # @param [Likeable] likeable the object to like/unlike.
+ # @return [Boolean]
+ def toggle_like!(likeable)
+ if likes?(likeable)
+ unlike!(likeable)
+ else
+ like!(likeable)
+ end
+ end
+
# Specifies if self likes a {Likeable} object.
#
# @param [Likeable] likeable the {Likeable} object to test against.
# @return [Boolean]
def likes?(likeable)
ensure_likeable!(likeable)
- !self.likes.where(:likeable_type => likeable.class.to_s, :likeable_id => likeable.id).empty?
+ !self.likes.where(:likeable_type => likeable.class.table_name.classify, :likeable_id => likeable.id).empty?
end
# Returns a scope of the {Likeable}s followed by self.
View
14 lib/socialization/mentioner.rb
@@ -42,6 +42,18 @@ def unmention!(mentionable)
raise ActiveRecord::RecordNotFound
end
end
+ #
+ # Toggles a {MentionStore mention} relationship.
+ #
+ # @param [Mentionable] mentionable the object to mention/unmention.
+ # @return [Boolean]
+ def toggle_mention!(mentionable)
+ if mentions?(mentionable)
+ unmention!(mentionable)
+ else
+ mention!(mentionable)
+ end
+ end
# Specifies if self mentions a {Mentionable} object.
#
@@ -49,7 +61,7 @@ def unmention!(mentionable)
# @return [Boolean]
def mentions?(mentionable)
ensure_mentionable!(mentionable)
- !self.mentions.where(:mentionable_type => mentionable.class.to_s, :mentionable_id => mentionable.id).empty?
+ !self.mentions.where(:mentionable_type => mentionable.class.table_name.classify, :mentionable_id => mentionable.id).empty?
end
# Returns a scope of the {Mentionable}s mentioned by self.
View
21 test/follow_test.rb
@@ -36,6 +36,15 @@ class FollowTest < Test::Unit::TestCase
end
end
+ should "be able to toggle following on/off" do
+ @follower1.toggle_follow!(@followable1)
+ assert_equal true, @follower1.follows?(@followable1)
+ @follower1.toggle_follow!(@followable1)
+ assert_equal false, @follower1.follows?(@followable1)
+ @follower1.toggle_follow!(@followable1)
+ assert_equal true, @follower1.follows?(@followable1)
+ end
+
should "expose a list of its followees" do
Follow.create :follower => @follower1, :followable => @followable1
assert @follower1.followees(ImAFollowable).is_a?(ActiveRecord::Relation)
@@ -116,6 +125,18 @@ class FollowTest < Test::Unit::TestCase
end
end
+ context "Single Table Inheritance" do
+ setup do
+ @follower = ImAFollower.create
+ @followable_child = ImAFollowableChild.create
+ end
+
+ should "be able to follow a model inheriting from Followable" do
+ assert @follower.follow!(@followable_child)
+ assert_equal true, @follower.follows?(@followable_child)
+ end
+ end
+
def seed
@follower1 = ImAFollower.create
@follower2 = ImAFollower.create
View
21 test/like_test.rb
@@ -36,6 +36,15 @@ class LikeTest < Test::Unit::TestCase
end
end
+ should "be able to toggle likes on/off" do
+ @liker1.toggle_like!(@likeable1)
+ assert_equal true, @liker1.likes?(@likeable1)
+ @liker1.toggle_like!(@likeable1)
+ assert_equal false, @liker1.likes?(@likeable1)
+ @liker1.toggle_like!(@likeable1)
+ assert_equal true, @liker1.likes?(@likeable1)
+ end
+
should "expose a list of its likes" do
Like.create :liker => @liker1, :likeable => @likeable1
assert @liker1.likees(ImALikeable).is_a?(ActiveRecord::Relation)
@@ -116,6 +125,18 @@ class LikeTest < Test::Unit::TestCase
end
end
+ context "Single Table Inheritance" do
+ setup do
+ @liker = ImALiker.create
+ @likeable_child = ImALikeableChild.create
+ end
+
+ should "be able to like a model inheriting from a Likeable" do
+ assert @liker.like!(@likeable_child)
+ assert_equal true, @liker.likes?(@likeable_child)
+ end
+ end
+
def seed
@liker1 = ImALiker.create
@liker2 = ImALiker.create
View
21 test/mention_test.rb
@@ -40,6 +40,15 @@ class MentionTest < Test::Unit::TestCase
@mentioner_and_mentionable.mention!(@mentioner_and_mentionable)
end
+ should "be able to toggle mentions on/off" do
+ @mentioner1.toggle_mention!(@mentionable1)
+ assert_equal true, @mentioner1.mentions?(@mentionable1)
+ @mentioner1.toggle_mention!(@mentionable1)
+ assert_equal false, @mentioner1.mentions?(@mentionable1)
+ @mentioner1.toggle_mention!(@mentionable1)
+ assert_equal true, @mentioner1.mentions?(@mentionable1)
+ end
+
should "expose a list of its mentionees" do
Mention.create :mentioner => @mentioner1, :mentionable => @mentionable1
assert @mentioner1.mentionees(ImAMentioner).is_a?(ActiveRecord::Relation)
@@ -106,6 +115,18 @@ class MentionTest < Test::Unit::TestCase
end
end
+ context "Single Table Inheritance" do
+ setup do
+ @mentioner = ImAMentioner.create
+ @mentionable_child = ImAMentionableChild.create
+ end
+
+ should "be able to mention a model inheriting from mentionable" do
+ assert @mentioner.mention!(@mentionable_child)
+ assert_equal true, @mentioner.mentions?(@mentionable_child)
+ end
+ end
+
def seed
@mentioner1 = ImAMentioner.create
@mentioner2 = ImAMentioner.create
View
12 test/test_helper.rb
@@ -62,6 +62,7 @@
end
create_table :im_a_followables do |t|
+ t.string :type
t.timestamps
end
@@ -70,6 +71,7 @@
end
create_table :im_a_likeables do |t|
+ t.string :type
t.timestamps
end
@@ -78,6 +80,7 @@
end
create_table :im_a_mentionables do |t|
+ t.string :type
t.timestamps
end
@@ -136,6 +139,9 @@ class ImAFollowable < ActiveRecord::Base
acts_as_followable
end
+class ImAFollowableChild < ImAFollowable
+end
+
class ImALiker < ActiveRecord::Base
acts_as_liker
end
@@ -144,6 +150,9 @@ class ImALikeable < ActiveRecord::Base
acts_as_likeable
end
+class ImALikeableChild < ImALikeable
+end
+
class ImAMentioner < ActiveRecord::Base
acts_as_mentioner
end
@@ -152,6 +161,9 @@ class ImAMentionable < ActiveRecord::Base
acts_as_mentionable
end
+class ImAMentionableChild < ImAMentionable
+end
+
class ImAMentionerAndMentionable < ActiveRecord::Base
acts_as_mentioner
acts_as_mentionable
Something went wrong with that request. Please try again.