cirandas.net

ref: master

plugins/relevant_content/lib/ext/article.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require_dependency 'article'

class Article

  scope :relevant_content, -> { where "articles.published = true and (articles.type != 'UploadedFile' and articles.type != 'Blog' and articles.type != 'RssFeed') OR articles.type is NULL" }

  def self.articles_columns
    Article.column_names.map {|c| "articles.#{c}"} .join(",")
  end

  def self.most_accessed(owner, limit = nil)
    conditions = owner.kind_of?(Environment) ?  ["hits > 0"] : ["profile_id = ? and hits > 0", owner.id]
    result = Article.relevant_content.order('hits desc').limit(limit).where(conditions)
    result.paginate({:page => 1, :per_page => limit})
  end

  def self.most_commented_relevant_content(owner, limit)
    conditions = owner.kind_of?(Environment) ? ["comments_count > 0"] : ["profile_id = ? and comments_count > 0", owner.id]
    result = Article.relevant_content.order('comments_count desc').limit(limit).where(conditions)
    result.paginate({:page => 1, :per_page => limit})
  end

  def self.more_positive_votes(owner, limit = nil)
    conditions = owner.kind_of?(Environment) ? {'votes.voteable_type' => 'Article'} : ["profile_id = ? and votes.voteable_type = ? ", owner.id, 'Article']
    result = Article.relevant_content.
      order('sum(vote) desc').group('voteable_id, ' + articles_columns).
      limit(limit).having('sum(vote) > 0').
      where(conditions).joins('INNER JOIN votes ON articles.id = votes.voteable_id')
    result.paginate({:page => 1, :per_page => limit})
  end

  def self.more_negative_votes(owner, limit = nil)
    conditions = owner.kind_of?(Environment) ? {'votes.voteable_type' => 'Article'} : ["profile_id = ? and votes.voteable_type = 'Article' ", owner.id]
    result = Article.relevant_content.
      order('sum(vote) asc').group('voteable_id, ' + articles_columns).
      limit(limit).having('sum(vote) < 0').
      where(conditions).joins('INNER JOIN votes ON articles.id = votes.voteable_id')
    result.paginate({:page => 1, :per_page => limit})
  end

  def self.most_liked(owner, limit = nil)
    conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article' and vote > 0"] : ["votes.voteable_type = 'Article' and vote > 0 and profile_id = ? ", owner.id]
    result = Article.relevant_content.
      select(articles_columns).order('count(voteable_id) desc').
      group('voteable_id, ' + articles_columns).limit(limit).
      where(conditions).joins('INNER JOIN votes ON articles.id = votes.voteable_id')
    result.paginate({:page => 1, :per_page => limit})
  end

  def self.most_disliked(owner, limit = nil)
    conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article' and vote < 0"] : ["votes.voteable_type = 'Article' and vote < 0 and profile_id = ? ", owner.id]
    result = Article.relevant_content.
      order('count(voteable_id) desc').group('voteable_id, ' + articles_columns).
      limit(limit).where(conditions).joins('INNER JOIN votes ON articles.id = votes.voteable_id')
    result.paginate({:page => 1, :per_page => limit})
  end

  def self.most_voted(owner, limit = nil)
    conditions = owner.kind_of?(Environment) ? ["votes.voteable_type = 'Article'"] : ["votes.voteable_type = 'Article' and profile_id = ? ", owner.id]
    result = Article.relevant_content.
      select(articles_columns).order('count(voteable_id) desc').
      group('voteable_id, ' + articles_columns).limit(limit).
      where(conditions).joins('INNER JOIN votes ON articles.id = votes.voteable_id')
    result.paginate({:page => 1, :per_page => limit})
  end
end