Class: MentionSystem::Mention

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/mention_system/mention.rb

Overview

Mention class

This class defines the mention model in mention system

Class Method Summary collapse

Class Method Details

.mention(mentioner, mentionee) ⇒ Boolean

Creates a MentionSystem::Mention relationship between a MentionSystem::Mentioner object and a MentionSystem::Mentionee object

Parameters:

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Boolean

Specifies if a MentionSystem::Mentioner object mentions a MentionSystem::Mentionee object

Parameters:

Returns:

  • (Boolean)


88
89
90
91
92
93
# 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) ⇒ ActiveRecord::Relation

Retrieves a scope of MentionSystem::Mention objects filtered by a MentionSystem::Mentionee object

Parameters:

Returns:

  • (ActiveRecord::Relation)


101
102
103
# File 'lib/mention_system/mention.rb', line 101

def self.scope_by_mentionee(mentionee)
  where(mentionee: mentionee)
end

.scope_by_mentionee_type(klass) ⇒ ActiveRecord::Relation

Retrieves a scope of MentionSystem::Mention objects filtered by a MentionSystem::Mentionee type

Parameters:

  • klass (Class)
    • the Class to filter

Returns:

  • (ActiveRecord::Relation)


111
112
113
# 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) ⇒ ActiveRecord::Relation

Retrieves a scope of MentionSystem::Mention objects filtered by a MentionSystem::Mentioner object

Parameters:

Returns:

  • (ActiveRecord::Relation)


121
122
123
# File 'lib/mention_system/mention.rb', line 121

def self.scope_by_mentioner(mentioner)
  where(mentioner: mentioner)
end

.scope_by_mentioner_type(klass) ⇒ ActiveRecord::Relation

Retrieves a scope of MentionSystem::Mention objects filtered by a MentionSystem::Mentioner type

Parameters:

  • klass (Class)
    • the Class to filter

Returns:

  • (ActiveRecord::Relation)


131
132
133
# 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) ⇒ Boolean

Toggles a MentionSystem::Mention relationship between a MentionSystem::Mentioner object and a MentionSystem::Mentionee object

Parameters:

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Boolean

Destroys a MentionSystem::Mention relationship between a MentionSystem::Mentioner object and a MentionSystem::Mentionee object

Parameters:

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
# 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

.validate_mentionee(mentionee) ⇒ Object (private)

Validates a mentionee object

Raises:

  • (ArgumentError)

    if the mentionee object is invalid



141
142
143
# 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) ⇒ Object (private)

Validates a mentioner object

Raises:

  • (ArgumentError)

    if the mentioner object is invalid



150
151
152
# 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