2014-03-26 11:09:39 +00:00
|
|
|
# == 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
|
|
|
|
#
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
class Forum < ActiveRecord::Base
|
|
|
|
include Extra
|
2014-04-12 01:35:42 +00:00
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
BANS = 8
|
|
|
|
TRASH = 12
|
|
|
|
|
|
|
|
attr_protected :id, :updated_at, :created_at
|
|
|
|
|
|
|
|
scope :public,
|
|
|
|
:select => "forums.*",
|
|
|
|
:joins => "LEFT JOIN forumers ON forumers.forum_id = forums.id AND forumers.access = #{Forumer::ACCESS_READ}",
|
|
|
|
:conditions => "forumers.id IS NULL"
|
|
|
|
scope :of_forum,
|
|
|
|
lambda { |forum| {:conditions => {"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
|
|
|
|
|
|
|
|
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.of_forum(self).first
|
|
|
|
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
|
|
|
|
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}")
|
|
|
|
|
|
|
|
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.to_sql}) q )")
|
|
|
|
end
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|