class MentionSystem::Mention

Mention class

This class defines the mention model in mention system

Public Class Methods

mention(mentioner, mentionee) click to toggle source

Creates a {Mention} relationship between a {Mentioner} object and a {Mentionee} object

@param [Mentioner] mentioner - the {Mentioner} of the relationship @param [Mentionee] mentionee - the {Mentionee} of the relationship @return [Boolean]

# File lib/mention_system/mention.rb, line 30
def self.mention(mentioner, mentionee)
  validate_mentionee(mentionee)
  validate_mentioner(mentioner)

  if mentions?(mentioner, mentionee)
    false
  else
    mention = scope_by_mentioner(mentioner).scope_by_mentionee(mentionee).build
    mention.save
    true
  end
end
mentions?(mentioner, mentionee) click to toggle source

Specifies if a {Mentioner} object mentions a {Mentionee} object

@param [Mentioner] mentioner - the {Mentioner} object to test against @param [Mentionee] mentionee - the {Mentionee} object to test against @return [Boolean]

# File lib/mention_system/mention.rb, line 88
def self.mentions?(mentioner, mentionee)
  validate_mentionee(mentionee)
  validate_mentioner(mentioner)

  scope_by_mentioner(mentioner).scope_by_mentionee(mentionee).exists?
end
scope_by_mentionee(mentionee) click to toggle source

Retrieves a scope of {Mention} objects filtered by a {Mentionee} object

@param [Mentionee] mentionee - the {Mentionee} to filter @return [ActiveRecord::Relation]

# File lib/mention_system/mention.rb, line 101
def self.scope_by_mentionee(mentionee)
  where(mentionee: mentionee)
end
scope_by_mentionee_type(klass) click to toggle source

Retrieves a scope of {Mention} objects filtered by a {Mentionee} type

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

# File lib/mention_system/mention.rb, line 111
def self.scope_by_mentionee_type(klass)
  where(mentionee_type: klass.to_s.classify)
end
scope_by_mentioner(mentioner) click to toggle source

Retrieves a scope of {Mention} objects filtered by a {Mentioner} object

@param [Mentioner] mentioner - the {Mentioner} to filter @return [ActiveRecord::Relation]

# File lib/mention_system/mention.rb, line 121
def self.scope_by_mentioner(mentioner)
  where(mentioner: mentioner)
end
scope_by_mentioner_type(klass) click to toggle source

Retrieves a scope of {Mention} objects filtered by a {Mentioner} type

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

# File lib/mention_system/mention.rb, line 131
def self.scope_by_mentioner_type(klass)
  where(mentioner_type: klass.to_s.classify)
end
toggle_mention(mentioner, mentionee) click to toggle source

Toggles a {Mention} relationship between a {Mentioner} object and a {Mentionee} object

@param [Mentioner] mentioner - the {Mentioner} of the relationship @param [Mentionee] mentionee - the {Mentionee} of the relationship @return [Boolean]

# File lib/mention_system/mention.rb, line 70
def self.toggle_mention(mentioner, mentionee)
  validate_mentionee(mentionee)
  validate_mentioner(mentioner)

  if mentions?(mentioner, mentionee)
    unmention(mentioner, mentionee)
  else
    mention(mentioner, mentionee)
  end
end
unmention(mentioner, mentionee) click to toggle source

Destroys a {Mention} relationship between a {Mentioner} object and a {Mentionee} object

@param [Mentioner] mentioner - the {Mentioner} of the relationship @param [Mentionee] mentionee - the {Mentionee} of the relationship @return [Boolean]

# File lib/mention_system/mention.rb, line 50
def self.unmention(mentioner, mentionee)
  validate_mentionee(mentionee)
  validate_mentioner(mentioner)

  if mentions?(mentioner, mentionee)
    mention = scope_by_mentioner(mentioner).scope_by_mentionee(mentionee).take
    mention.destroy
    true
  else
    false
  end
end

Private Class Methods

validate_mentionee(mentionee) click to toggle source

Validates a mentionee object

@raise [ArgumentError] if the mentionee object is invalid

# File lib/mention_system/mention.rb, line 141
def self.validate_mentionee(mentionee)
  raise ArgumentError.new unless mentionee.respond_to?(:is_mentionee?) && mentionee.is_mentionee?
end
validate_mentioner(mentioner) click to toggle source

Validates a mentioner object

@raise [ArgumentError] if the mentioner object is invalid

# File lib/mention_system/mention.rb, line 150
def self.validate_mentioner(mentioner)
  raise ArgumentError.new unless mentioner.respond_to?(:is_mentioner?) && mentioner.is_mentioner?
end