Skip to content

Commit

Permalink
Added mentionees method to mentionner
Browse files Browse the repository at this point in the history
  • Loading branch information
Masahiro Ihara committed Feb 25, 2012
1 parent c54161c commit 3af0023
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/socialization/mentionner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ def mentions?(mentionable)
!self.mentions.where(:mentionable_type => mentionable.class.to_s, :mentionable_id => mentionable.id).empty?
end

def mentionees(klass)
klass = klass.to_s.singularize.camelize.constantize unless klass.is_a?(Class)
klass.joins("INNER JOIN mentions ON mentions.mentionable_id = #{klass.to_s.tableize}.id AND mentions.mentionable_type = '#{klass.to_s}'").
where("mentions.mentionner_type = '#{self.class.to_s}'").
where("mentions.mentionner_id = #{self.id}")
end

private
def ensure_mentionable!(mentionable)
raise ArgumentError, "#{mentionable} is not mentionable!" unless mentionable.is_mentionable?
end

end
end
end
end
11 changes: 10 additions & 1 deletion test/mention_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class MentionTest < Test::Unit::TestCase
should "be able to mention itself" do
@mentionner_and_mentionable.mention!(@mentionner_and_mentionable)
end

should "expose a list of its mentionees" do
Mention.create :mentionner => @mentionner1, :mentionable => @mentionable1
assert @mentionner1.mentionees(ImAMentionner).is_a?(ActiveRecord::Relation)
assert_equal [@mentionable1], @mentionner1.mentionees(ImAMentionable).all

assert_equal @mentionner1.mentionees(ImAMentionable), @mentionner1.mentionees(:im_a_mentionables)
assert_equal @mentionner1.mentionees(ImAMentionable), @mentionner1.mentionees("im_a_mentionable")
end
end

context "a Mentionable" do
Expand Down Expand Up @@ -104,4 +113,4 @@ def seed
@mentionable2 = ImAMentionable.create
@mentionner_and_mentionable = ImAMentionnerAndMentionable.create
end
end
end

0 comments on commit 3af0023

Please sign in to comment.