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::Mention

after_mention, after_unmention, touch

Methods included from Stores::Mixins::Base

touch_actor?, touch_dependents, touch_victim?

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
# 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
    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


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

def mentionables(mentioner, klass, opts = {})
  rel = mentionables_relation(mentioner, klass, opts)
  if rel.is_a?(ActiveRecord::Relation)
    rel.to_a
  else
    rel
  end
end

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

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


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

def mentionables_relation(mentioner, klass, opts = {})
  rel = klass.where(:id =>
    self.select(:mentionable_id).
      where(:mentionable_type => klass.table_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


66
67
68
69
70
71
72
73
# File 'lib/socialization/stores/active_record/mention.rb', line 66

def mentioners(mentionable, klass, opts = {})
  rel = mentioners_relation(mentionable, klass, opts)
  if rel.is_a?(ActiveRecord::Relation)
    rel.to_a
  else
    rel
  end
end

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

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


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/socialization/stores/active_record/mention.rb', line 50

def mentioners_relation(mentionable, klass, opts = {})
  rel = klass.where(:id =>
    self.select(:mentioner_id).
      where(:mentioner_type => klass.table_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


45
46
47
# File 'lib/socialization/stores/active_record/mention.rb', line 45

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

.remove_mentionables(mentioner) ⇒ Object

Remove all the mentionables for mentioner


108
109
110
111
# File 'lib/socialization/stores/active_record/mention.rb', line 108

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


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

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

.unmention!(mentioner, mentionable) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'lib/socialization/stores/active_record/mention.rb', line 35

def unmention!(mentioner, mentionable)
  if mentions?(mentioner, mentionable)
    mention_for(mentioner, mentionable).destroy_all
    call_after_destroy_hooks(mentioner, mentionable)
    true
  else
    false
  end
end