Class: Socialization::ActiveRecordStores::Mention

Inherits:
ActiveRecord::Base show all
Extended by:
Socialization::ActiveRecordStores::Mixins::Base, Stores::Mixins::Base, Stores::Mixins::Mention
Defined in:
lib/socialization/stores/active_record/mention.rb

Direct Known Subclasses

Mention

Class Method Summary collapse

Methods included from Stores::Mixins::Base

touch_actor?, touch_dependents, touch_subject?

Methods included from Stores::Mixins::Mention

after_mention, after_unmention, touch

Methods included from Socialization::ActiveRecordStores::Mixins::Base

update_counter

Methods inherited from ActiveRecord::Base

#is_followable?, #is_follower?, #is_likeable?, #is_liker?, #is_mentionable?, #is_mentioner?

Class Method Details

.mention!(mentioner, mentionable) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/socialization/stores/active_record/mention.rb', line 22

def mention!(mentioner, mentionable)
  unless mentions?(mentioner, mentionable)
    self.create! do |mention|
      mention.mentioner = mentioner
      mention.mentionable = mentionable
    end
    update_counter(mentioner, mentionees_count: +1)
    update_counter(mentionable, mentioners_count: +1)
    call_after_create_hooks(mentioner, mentionable)
    true
  else
    false
  end
end

.mentionables(mentioner, klass, opts = {}) ⇒ Object

Returns all the mentionables of a certain type that are mentioned by mentioner



91
92
93
# File 'lib/socialization/stores/active_record/mention.rb', line 91

def mentionables(mentioner, klass, opts = {})
  mentionables_relation(mentioner, klass, opts)
end

.mentionables_relation(mentioner, klass, opts = {}) ⇒ Object

Returns an ActiveRecord::Relation of all the mentionables of a certain type that are mentioned by mentioner



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/socialization/stores/active_record/mention.rb', line 75

def mentionables_relation(mentioner, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    self.select(:mentionable_id).
      where(:mentionable_type => klass.name.classify).
      where(:mentioner_type => mentioner.class.to_s).
      where(:mentioner_id => mentioner.id)
  )

  if opts[:pluck]
    rel.pluck(opts[:pluck])
  else
    rel
  end
end

.mentioners(mentionable, klass, opts = {}) ⇒ Object

Returns all the mentioners of a certain type that are mentioning mentionable



70
71
72
# File 'lib/socialization/stores/active_record/mention.rb', line 70

def mentioners(mentionable, klass, opts = {})
  mentioners_relation(mentionable, klass, opts)
end

.mentioners_relation(mentionable, klass, opts = {}) ⇒ Object

Returns an ActiveRecord::Relation of all the mentioners of a certain type that are mentioning mentionable



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/socialization/stores/active_record/mention.rb', line 54

def mentioners_relation(mentionable, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    self.select(:mentioner_id).
      where(:mentioner_type => klass.name.classify).
      where(:mentionable_type => mentionable.class.to_s).
      where(:mentionable_id => mentionable.id)
  )

  if opts[:pluck]
    rel.pluck(opts[:pluck])
  else
    rel
  end
end

.mentions?(mentioner, mentionable) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/socialization/stores/active_record/mention.rb', line 49

def mentions?(mentioner, mentionable)
  !mention_for(mentioner, mentionable).empty?
end

.remove_mentionables(mentioner) ⇒ Object

Remove all the mentionables for mentioner



102
103
104
105
# File 'lib/socialization/stores/active_record/mention.rb', line 102

def remove_mentionables(mentioner)
  self.where(:mentioner_type => mentioner.class.name.classify).
       where(:mentioner_id => mentioner.id).destroy_all
end

.remove_mentioners(mentionable) ⇒ Object

Remove all the mentioners for mentionable



96
97
98
99
# File 'lib/socialization/stores/active_record/mention.rb', line 96

def remove_mentioners(mentionable)
  self.where(:mentionable_type => mentionable.class.name.classify).
       where(:mentionable_id => mentionable.id).destroy_all
end

.unmention!(mentioner, mentionable) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/socialization/stores/active_record/mention.rb', line 37

def unmention!(mentioner, mentionable)
  if mentions?(mentioner, mentionable)
    mention_for(mentioner, mentionable).destroy_all
    update_counter(mentioner, mentionees_count: -1)
    update_counter(mentionable, mentioners_count: -1)
    call_after_destroy_hooks(mentioner, mentionable)
    true
  else
    false
  end
end