2014-03-26 11:09:39 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: topics
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# title :string(255)
|
|
|
|
# user_id :integer
|
|
|
|
# forum_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# state :integer default(0), not null
|
|
|
|
#
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
class Topic < ActiveRecord::Base
|
|
|
|
POSTS_PAGE = 30
|
|
|
|
STATE_NORMAL = 0
|
|
|
|
STATE_STICKY = 1
|
|
|
|
LATEST_PER_PAGE = 5
|
|
|
|
|
|
|
|
RULES = 12
|
|
|
|
|
|
|
|
include Extra
|
|
|
|
attr_protected :id, :updated_at, :created_at
|
|
|
|
attr_accessor :first_post
|
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :forum
|
|
|
|
has_one :lock, :as => :lockable
|
2019-06-02 01:26:36 +00:00
|
|
|
has_one :latest, -> { order("id DESC") }, :class_name => "Post"
|
|
|
|
has_many :posts, -> { order("id ASC") }, :dependent => :destroy
|
2014-03-23 00:22:25 +00:00
|
|
|
has_many :view_counts, :as => :viewable, :dependent => :destroy
|
|
|
|
|
2019-06-02 01:26:36 +00:00
|
|
|
scope :basic, -> { includes([:latest, { forum: :forumer }, :user]) }
|
|
|
|
scope :ordered, -> { order("state DESC, posts.id DESC") }
|
2014-03-23 00:22:25 +00:00
|
|
|
|
2014-04-12 01:35:42 +00:00
|
|
|
validates_presence_of :user_id, :forum_id
|
2014-03-23 00:22:25 +00:00
|
|
|
validates_length_of :title, :in => 1..50
|
|
|
|
validates_length_of :first_post, :in => 1..10000, :on => :create
|
|
|
|
|
|
|
|
after_create :make_post
|
|
|
|
|
2020-03-15 20:59:08 +00:00
|
|
|
acts_as_readable
|
2014-03-23 00:22:25 +00:00
|
|
|
|
2015-08-20 15:20:40 +00:00
|
|
|
def self.recent_topics
|
2015-08-22 10:53:26 +00:00
|
|
|
find_by_sql %q{
|
2015-09-05 16:09:20 +00:00
|
|
|
SELECT DISTINCT topics.*
|
2015-09-05 15:23:50 +00:00
|
|
|
FROM (SELECT max(id) as max_id, topic_id
|
2015-08-22 10:53:26 +00:00
|
|
|
FROM posts
|
2015-09-05 15:23:50 +00:00
|
|
|
GROUP BY topic_id
|
|
|
|
ORDER BY max_id DESC
|
2015-09-05 18:16:14 +00:00
|
|
|
LIMIT 200) AS T
|
2015-08-22 10:53:26 +00:00
|
|
|
INNER JOIN topics
|
|
|
|
ON T.topic_id = topics.id
|
|
|
|
INNER JOIN forums
|
|
|
|
ON forums.id = topics.forum_id
|
|
|
|
LEFT OUTER JOIN forumers
|
|
|
|
ON forumers.forum_id = forums.id
|
|
|
|
WHERE forumers.id IS NULL
|
|
|
|
LIMIT 5
|
2015-08-22 09:48:08 +00:00
|
|
|
}
|
2015-08-20 15:20:40 +00:00
|
|
|
end
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
def to_s
|
|
|
|
title
|
|
|
|
end
|
|
|
|
|
|
|
|
def record_view_count(ip_address, logged_in = false)
|
|
|
|
self.view_counts.create(:viewable => self, :ip_address => ip_address, :logged_in => logged_in)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def view_count
|
2014-04-13 11:16:51 +00:00
|
|
|
view_counts.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key(key)
|
|
|
|
"/topics/#{id}/#{key}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_view_count
|
2014-05-10 01:26:18 +00:00
|
|
|
Rails.cache.fetch(cache_key('view_count'), expires_in: 1.hours) do
|
2014-05-10 14:07:54 +00:00
|
|
|
self.view_count
|
2014-04-13 11:16:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_posts_count
|
2014-05-10 01:26:18 +00:00
|
|
|
Rails.cache.fetch(cache_key('posts'), expires_in: 10.minutes) do
|
2014-04-13 11:16:51 +00:00
|
|
|
posts.count - 1
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_post
|
|
|
|
c = posts.build
|
|
|
|
c.text = first_post
|
|
|
|
c.user = user
|
|
|
|
c.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_show? cuser
|
|
|
|
forum.can_show? cuser
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_create? cuser
|
|
|
|
return false unless cuser
|
|
|
|
errors.add :bans, I18n.t(:bans_mute) if cuser.banned?(Ban::TYPE_MUTE) and forum != Forum::BANS
|
|
|
|
errors.add :bans, I18n.t(:registered_for_week) unless cuser.verified?
|
|
|
|
(Forum.available_to(cuser, Forumer::ACCESS_TOPIC).of_forum(forum).first and errors.size == 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_update? cuser
|
|
|
|
cuser and cuser.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_destroy? cuser
|
|
|
|
cuser and cuser.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_page
|
|
|
|
[((posts.count - 1) / POSTS_PAGE) + 1, 1].max
|
|
|
|
end
|
|
|
|
|
|
|
|
def states
|
|
|
|
{STATE_NORMAL => "Normal", STATE_STICKY => "Sticky"}
|
|
|
|
end
|
|
|
|
end
|