lib/popular/popular.rb in popular-0.4.0 vs lib/popular/popular.rb in popular-0.5.0

- old
+ new

@@ -5,10 +5,12 @@ extend ActiveSupport::Concern included do |base| has_many :friendships, class_name: 'Popular::Friendship', as: :popular_model, dependent: :destroy has_many :friends, through: :friendships, source_type: base + has_many :inverse_friendships, class_name: 'Popular::Friendship', as: :friend, foreign_key: :friend_id + has_many :inverse_friends, through: :inverse_friendships, source: :popular_model, source_type: base include ActiveSupport::Callbacks define_callbacks :befriend, :unfriend [:before_unfriend, :after_unfriend, :before_befriend, :after_befriend].each do |callback| @@ -64,11 +66,30 @@ run_callbacks :unfriend do friendships.where( friend: friend ).first.destroy end end - # Helper method for finding whether or not the instance is friends with + # Helper method for finding whether or not the instance has + # been befriended by another given popular_model + # + # @param [Object] popular_model + # @return [Boolean] if the instance has been friended by another popular_model + # + # @example + # user = User.create name: "Justin" + # other_user = User.create name: "Jenny" + # + # user.friended_by? other_user #=> false + # + # other_user.befriend user + # + # user.friended_by? other_user #=> true + def friended_by? popular_model + inverse_friends.include? popular_model + end + + # Helper method for finding whether or not the instance has befriended # another given popular_model # # @param [Object] popular_model # @return [Boolean] if the instance has popular_model as a friend # @@ -80,20 +101,18 @@ # # user.befriend other_user # # user.friends_with? other_user #=> true def friends_with? popular_model - friendships.where( friend: popular_model ).any? + friends.include? popular_model end # ClassMethods included in popular models module ClassMethods # after_unfriend callback convenience class method # - # @since 0.4.0 - # # @example # # class User < ActiveRecord::Base # after_unfriend :do_something_amazing # @@ -111,12 +130,10 @@ set_callback :unfriend, :after, *args, &blk end # before_unfriend callback convenience class method # - # @since 0.4.0 - # # @example # # class User < ActiveRecord::Base # before_unfriend :do_something_amazing # @@ -134,12 +151,10 @@ set_callback :unfriend, :before, *args, &blk end # before_befriend callback convenience class method # - # @since 0.3.0 - # # @example # # class User < ActiveRecord::Base # before_befriend :do_something_amazing # @@ -155,11 +170,9 @@ def before_befriend *args, &blk set_callback :befriend, :before, *args, &blk end # after_befriend callback convenience class method - # - # @since 0.3.0 # # @example # # class User < ActiveRecord::Base # after_befriend :do_something_amazing