Class: MentionSystem::Mention
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MentionSystem::Mention
- Defined in:
- lib/mention_system/mention.rb
Overview
Mention class
This class defines the mention model in mention system
Class Method Summary collapse
- .mention(mentioner, mentionee) ⇒ Boolean
- .mentions?(mentioner, mentionee) ⇒ Boolean
- .scope_by_mentionee(mentionee) ⇒ ActiveRecord::Relation
- .scope_by_mentionee_type(klass) ⇒ ActiveRecord::Relation
- .scope_by_mentioner(mentioner) ⇒ ActiveRecord::Relation
- .scope_by_mentioner_type(klass) ⇒ ActiveRecord::Relation
- .toggle_mention(mentioner, mentionee) ⇒ Boolean
- .unmention(mentioner, mentionee) ⇒ Boolean
-
.validate_mentionee(mentionee) ⇒ Object
private
Validates a mentionee object.
-
.validate_mentioner(mentioner) ⇒ Object
private
Validates a mentioner object.
Class Method Details
.mention(mentioner, mentionee) ⇒ Boolean
Creates a MentionSystem::Mention relationship between a MentionSystem::Mentioner object and a MentionSystem::Mentionee object
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
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
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
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
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
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
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
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
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
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 |