2014-03-26 11:09:39 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: categories
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2020-03-18 03:38:17 +00:00
|
|
|
# domain :integer
|
2014-03-26 11:09:39 +00:00
|
|
|
# name :string(255)
|
|
|
|
# sort :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2020-03-18 03:38:17 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_categories_on_domain (domain)
|
|
|
|
# index_categories_on_sort (sort)
|
2014-03-26 11:09:39 +00:00
|
|
|
#
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
class Category < ActiveRecord::Base
|
|
|
|
include Extra
|
|
|
|
|
|
|
|
MAIN = 1
|
|
|
|
SPECIAL = 10
|
|
|
|
INTERVIEWS = 11
|
2017-02-27 22:21:59 +00:00
|
|
|
RULES = 61
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
DOMAIN_NEWS = 0
|
|
|
|
DOMAIN_ARTICLES = 1
|
|
|
|
DOMAIN_ISSUES = 2
|
|
|
|
DOMAIN_SITES = 3
|
|
|
|
DOMAIN_FORUMS = 4
|
|
|
|
DOMAIN_MOVIES = 5
|
|
|
|
DOMAIN_GAMES = 6
|
|
|
|
|
|
|
|
PER_PAGE = 3
|
|
|
|
|
2020-03-16 23:57:47 +00:00
|
|
|
#attr_protected :id, :updated_at, :created_at, :sort
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
validates_length_of :name, :in => 1..30
|
|
|
|
validate :validate_domain
|
|
|
|
|
2019-06-01 10:56:09 +00:00
|
|
|
scope :ordered, -> { order("sort ASC, created_at DESC") }
|
|
|
|
scope :domain, -> (domain) { where(domain: domain) }
|
|
|
|
scope :nospecial, -> { where.not(name: 'Special') }
|
2019-06-02 01:26:36 +00:00
|
|
|
scope :newest, -> { includes(:articles).order("articles.created_at DESC") }
|
2019-06-01 10:56:09 +00:00
|
|
|
# scope :page, lambda { |page| {:limit => "#{(page-1)*PER_PAGE}, #{(page-1)*PER_PAGE+PER_PAGE}"} }
|
2020-03-18 04:41:13 +00:00
|
|
|
scope :of_user, -> (user) { where(articles: {user: user}).includes(:articles) }
|
2014-03-23 00:22:25 +00:00
|
|
|
|
2019-06-01 10:56:09 +00:00
|
|
|
has_many :articles, -> { order("created_at DESC") }
|
|
|
|
has_many :issues, -> { order("created_at DESC") }
|
|
|
|
has_many :forums, -> { order("forums.position") }
|
2014-03-23 00:22:25 +00:00
|
|
|
has_many :movies
|
|
|
|
has_many :maps
|
|
|
|
has_many :gathers
|
|
|
|
has_many :servers
|
|
|
|
|
2020-03-15 20:59:08 +00:00
|
|
|
acts_as_readable
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
def domains
|
|
|
|
{DOMAIN_NEWS => 'News',
|
|
|
|
DOMAIN_ARTICLES => 'Articles',
|
|
|
|
DOMAIN_ISSUES => 'Issues',
|
|
|
|
DOMAIN_SITES => "Sites",
|
|
|
|
DOMAIN_FORUMS => "Forums",
|
|
|
|
DOMAIN_MOVIES => "Movies",
|
|
|
|
DOMAIN_GAMES => "Games"}
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_domain
|
|
|
|
errors.add :domain, I18n.t(:invalid_domain) unless domains.include? domain
|
|
|
|
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
|
2020-03-18 03:38:17 +00:00
|
|
|
|
|
|
|
def self.params(params, cuser)
|
|
|
|
params.require(:ban).permit(:name, :sort, :domain)
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|