Skip to content

Commit

Permalink
Added support for STI.
Browse files Browse the repository at this point in the history
  • Loading branch information
balvig committed Feb 28, 2012
1 parent cd0eae4 commit 0f6fcfd
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/socialization/follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def toggle_follow!(followable)
# @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.
Expand Down
2 changes: 1 addition & 1 deletion lib/socialization/liker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def toggle_like!(likeable)
# @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.
Expand Down
2 changes: 1 addition & 1 deletion lib/socialization/mentioner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def toggle_mention!(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.
Expand Down
12 changes: 12 additions & 0 deletions test/follow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,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
Expand Down
12 changes: 12 additions & 0 deletions test/like_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,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
Expand Down
12 changes: 12 additions & 0 deletions test/mention_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,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
Expand Down
12 changes: 12 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
end

create_table :im_a_followables do |t|
t.string :type
t.timestamps
end

Expand All @@ -70,6 +71,7 @@
end

create_table :im_a_likeables do |t|
t.string :type
t.timestamps
end

Expand All @@ -78,6 +80,7 @@
end

create_table :im_a_mentionables do |t|
t.string :type
t.timestamps
end

Expand Down Expand Up @@ -136,6 +139,9 @@ class ImAFollowable < ActiveRecord::Base
acts_as_followable
end

class ImAFollowableChild < ImAFollowable
end

class ImALiker < ActiveRecord::Base
acts_as_liker
end
Expand All @@ -144,6 +150,9 @@ class ImALikeable < ActiveRecord::Base
acts_as_likeable
end

class ImALikeableChild < ImALikeable
end

class ImAMentioner < ActiveRecord::Base
acts_as_mentioner
end
Expand All @@ -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
Expand Down

0 comments on commit 0f6fcfd

Please sign in to comment.