class Socialization::ActiveRecordStores::Mention

Public Class Methods

mention!(mentioner, mentionable) click to toggle source
# 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 = {}) click to toggle source

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

# File lib/socialization/stores/active_record/mention.rb, line 96
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 = {}) click to toggle source

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

# File lib/socialization/stores/active_record/mention.rb, line 80
def mentionables_relation(mentioner, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    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 = {}) click to toggle source

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

# File lib/socialization/stores/active_record/mention.rb, line 70
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 = {}) click to toggle source

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

# 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.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) click to toggle source
# File lib/socialization/stores/active_record/mention.rb, line 49
def mentions?(mentioner, mentionable)
  !mention_for(mentioner, mentionable).empty?
end
remove_mentionables(mentioner) click to toggle source

Remove all the mentionables for mentioner

# File lib/socialization/stores/active_record/mention.rb, line 112
def remove_mentionables(mentioner)
  self.where(:mentioner_type => mentioner.class.name.classify).
       where(:mentioner_id => mentioner.id).destroy_all
end
remove_mentioners(mentionable) click to toggle source

Remove all the mentioners for mentionable

# File lib/socialization/stores/active_record/mention.rb, line 106
def remove_mentioners(mentionable)
  self.where(:mentionable_type => mentionable.class.name.classify).
       where(:mentionable_id => mentionable.id).destroy_all
end
unmention!(mentioner, mentionable) click to toggle source
# 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

Private Class Methods

mention_for(mentioner, mentionable) click to toggle source
# File lib/socialization/stores/active_record/mention.rb, line 118
def mention_for(mentioner, mentionable)
  mentioned_by(mentioner).mentioning(mentionable)
end