Class: PostBase

Inherits:
ApplicationRecord show all
Defined in:
app/models/post_base.rb

Direct Known Subclasses

Episode, Post

Constant Summary collapse

COMMENTS_POWER =
50

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tags_from_tags_list(tags_list) ⇒ Object


109
110
111
112
113
# File 'app/models/post_base.rb', line 109

def self.tags_from_tags_list(tags_list)
  Tag.where(slug: tags_list.to_s.split(',')
    .map(&:strip).map { |t| Tag.generate_slug(t) }
    .uniq.compact).sort_by { |t| t.post_ids.size }
end

Instance Method Details

#blocked?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'app/models/post_base.rb', line 36

def blocked?
  blocked_at.present?
end

#has_comments?Boolean

Returns:

  • (Boolean)

40
41
42
# File 'app/models/post_base.rb', line 40

def has_comments?
  comments_count > 0
end

#has_picture?Boolean

Returns:

  • (Boolean)

52
53
54
# File 'app/models/post_base.rb', line 52

def has_picture?
  picture_url.present?
end

#picture_urlObject


56
57
58
# File 'app/models/post_base.rb', line 56

def picture_url
  self[:picture_url].to_s.gsub(/^http:/, '')
end

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/post_base.rb', line 72

def related(options = {})
  limit = options[:limit] || 10

  ids = Rails.cache.fetch("#{cache_key}#related##{options.to_json}") do
    result = []

    _tags = tags

    (1..[_tags.size, 3].min).to_a.reverse.each do |n|
      _tags.sort_by { |t| t.post_ids.size }.combination(n).each do |tags_combination|
        tagged = tags_combination.map(&:post_ids).inject(&:&)

        tagged ||= []

        tagged -= [id]

        # only older than current
        result += tagged.reject { |id| id > self.id }

        result = result.uniq

        break if result.size >= limit
      end

      break if result.size >= limit
    end

    result.sort.reverse.take(limit)
  end

  posts = Post.where(id: ids).recent

  posts
end

#source_urlObject


44
45
46
# File 'app/models/post_base.rb', line 44

def source_url
  self[:source_url] unless blog.hide_source_url?
end

#tagsObject


60
61
62
# File 'app/models/post_base.rb', line 60

def tags
  self.class.tags_from_tags_list(tags_list)
end

#tags_wasObject


64
65
66
# File 'app/models/post_base.rb', line 64

def tags_was
  self.class.tags_from_tags_list(tags_list_was)
end

#titleObject


48
49
50
# File 'app/models/post_base.rb', line 48

def title
  super.html_safe if super.present?
end

#to_paramObject


68
69
70
# File 'app/models/post_base.rb', line 68

def to_param
  friendly_param.presence || "#{id}-#{title.parameterize}"
end