ensl.org/app/models/forum.rb

82 lines
2 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: forums
#
# id :integer not null, primary key
# title :string(255)
# description :string(255)
# category_id :integer
# created_at :datetime
# updated_at :datetime
# position :integer
#
class Forum < ActiveRecord::Base
include Extra
BANS = 8
TRASH = 12
attr_protected :id, :updated_at, :created_at
scope :public_forums, -> { select("forums.*")
.joins("LEFT JOIN forumers ON forumers.forum_id = forums.id AND forumers.access = #{Forumer::ACCESS_READ}")
.where("forumers.id IS NULL") }
scope :of_forum, -> (forum) { where("forums.id", forum.id) }
scope :ordered, -> { order("position") }
has_many :topics
has_many :posts, :through => :topics
has_many :forumers
has_many :groups, :through => :forumers
has_one :forumer
belongs_to :category
after_create :update_position
2020-03-16 01:08:21 +00:00
2020-03-15 20:59:08 +00:00
acts_as_readable
def to_s
self.title
end
def update_position
update_attribute :position, self.id
end
def can_show? cuser
if cuser
Forum.available_to(cuser, Forumer::ACCESS_READ).of_forum(self).first
else
Forum.public_forums.where(id: self.id).exists?
end
end
def can_create? cuser
cuser and cuser.admin?
end
def can_update? cuser
cuser and cuser.admin?
end
def can_destroy? cuser
cuser and cuser.admin?
end
2015-04-09 11:44:30 +00:00
def self.available_to cuser, level
2020-03-15 18:31:00 +00:00
user_has_access =
Forum .joins("JOIN forumers ON forumers.forum_id = forums.id
AND forumers.access = #{level}")
.joins("JOIN groups ON forumers.group_id = groups.id")
.joins("JOIN groupers ON groupers.group_id = groups.id
AND groupers.user_id = #{cuser.id}")
2015-04-09 11:44:30 +00:00
2020-03-15 18:31:00 +00:00
is_admin = Grouper.where(user_id: cuser, group_id: Group::ADMINS)
Forum.where("EXISTS (#{is_admin.to_sql}) OR
id IN (SELECT q.id from (#{user_has_access.to_sql}) q ) OR
id IN (SELECT q.id from (#{Forum.public_forums.to_sql}) q )")
2015-04-09 11:44:30 +00:00
end
end