class FollowSystem::Follow

Follow class

This class defines the follow model in follow system

Public Class Methods

follow(follower, followee) click to toggle source

Creates a {Follow} relationship between a {Follower} object and a {Followee} object

@param [Follower] follower - the {Follower} of the relationship @param [Followee] followee - the {Followee} of the relationship @return [Boolean]

# File lib/follow_system/follow.rb, line 30
def self.follow(follower, followee)
  validate_followee(followee)
  validate_follower(follower)

  if follows?(follower, followee)
    false
  else
    follow = scope_by_follower(follower).scope_by_followee(followee).build
    follow.save
    true
  end
end
follows?(follower, followee) click to toggle source

Specifies if a {Follower} object follows a {Followee} object

@param [Follower] follower - the {Follower} object to test against @param [Followee] followee - the {Followee} object to test against @return [Boolean]

# File lib/follow_system/follow.rb, line 88
def self.follows?(follower, followee)
  validate_followee(followee)
  validate_follower(follower)

  scope_by_follower(follower).scope_by_followee(followee).exists?
end
scope_by_followee(followee) click to toggle source

Retrieves a scope of {Follow} objects filtered by a {Followee} object

@param [Followee] followee - the {Followee} to filter @return [ActiveRecord::Relation]

# File lib/follow_system/follow.rb, line 101
def self.scope_by_followee(followee)
  where(followee: followee)
end
scope_by_followee_type(klass) click to toggle source

Retrieves a scope of {Follow} objects filtered by a {Followee} type

@param [Class] klass - the {Class} to filter @return [ActiveRecord::Relation]

# File lib/follow_system/follow.rb, line 111
def self.scope_by_followee_type(klass)
  where(followee_type: klass.to_s.classify)
end
scope_by_follower(follower) click to toggle source

Retrieves a scope of {Follow} objects filtered by a {Follower} object

@param [Follower] follower - the {Follower} to filter @return [ActiveRecord::Relation]

# File lib/follow_system/follow.rb, line 121
def self.scope_by_follower(follower)
  where(follower: follower)
end
scope_by_follower_type(klass) click to toggle source

Retrieves a scope of {Follow} objects filtered by a {Follower} type

@param [Class] klass - the {Class} to filter @return [ActiveRecord::Relation]

# File lib/follow_system/follow.rb, line 131
def self.scope_by_follower_type(klass)
  where(follower_type: klass.to_s.classify)
end
toggle_follow(follower, followee) click to toggle source

Toggles a {Follow} relationship between a {Follower} object and a {Followee} object

@param [Follower] follower - the {Follower} of the relationship @param [Followee] followee - the {Followee} of the relationship @return [Boolean]

# File lib/follow_system/follow.rb, line 70
def self.toggle_follow(follower, followee)
  validate_followee(followee)
  validate_follower(follower)

  if follows?(follower, followee)
    unfollow(follower, followee)
  else
    follow(follower, followee)
  end
end
unfollow(follower, followee) click to toggle source

Destroys a {Follow} relationship between a {Follower} object and a {Followee} object

@param [Follower] follower - the {Follower} of the relationship @param [Followee] followee - the {Followee} of the relationship @return [Boolean]

# File lib/follow_system/follow.rb, line 50
def self.unfollow(follower, followee)
  validate_followee(followee)
  validate_follower(follower)

  if follows?(follower, followee)
    follow = scope_by_follower(follower).scope_by_followee(followee).take
    follow.destroy
    true
  else
    false
  end
end

Private Class Methods

validate_followee(followee) click to toggle source

Validates a followee object

@raise [ArgumentError] if the followee object is invalid

# File lib/follow_system/follow.rb, line 141
def self.validate_followee(followee)
  raise ArgumentError.new unless followee.respond_to?(:is_followee?) && followee.is_followee?
end
validate_follower(follower) click to toggle source

Validates a follower object

@raise [ArgumentError] if the follower object is invalid

# File lib/follow_system/follow.rb, line 150
def self.validate_follower(follower)
  raise ArgumentError.new unless follower.respond_to?(:is_follower?) && follower.is_follower?
end