ensl.org/app/models/issue.rb
2020-03-18 22:03:56 +02:00

140 lines
3.3 KiB
Ruby

# == Schema Information
#
# Table name: issues
#
# id :integer not null, primary key
# solution :text(65535)
# status :integer
# text :text(65535)
# text_parsed :text(65535)
# title :string(255)
# created_at :datetime
# updated_at :datetime
# assigned_id :integer
# author_id :integer
# category_id :integer
#
# Indexes
#
# index_issues_on_assigned_id (assigned_id)
# index_issues_on_author_id (author_id)
# index_issues_on_category_id (category_id)
#
class Issue < ActiveRecord::Base
include Extra
STATUS_OPEN = 0
STATUS_SOLVED = 1
STATUS_REJECTED = 2
CATEGORY_WEBSITE = 17
CATEGORY_NSLPLUGIN = 20
CATEGORY_LEAGUE = 22
CATEGORY_GATHER = 54
attr_accessor :assigned_name
#attr_protected :id, :created_at, :updated_at
has_many :comments, :as => :commentable
belongs_to :category
belongs_to :author, :class_name => "User"
belongs_to :assigned, :class_name => "User"
#scope :unread_by,
# lambda { |user| {
# :joins => "LEFT JOIN readings ON readable_type = 'Issue' AND readable_id = issues.id AND readings.user_id = #{user.id}",
# :conditions => "readings.user_id IS NULL"} }
scope :with_status, -> (s) { where(status: s) }
validates_length_of :title, :in => 1..50
validates_length_of :text, :in => 1..65000
validate :validate_status
before_validation :init_variables, :if => Proc.new{|issue| issue.new_record?}
before_save :parse_text
# FIXME
# after_save :remove_readings
acts_as_readable
def to_s
title
end
def status_s
statuses[status]
end
def statuses
{STATUS_OPEN => "Open", STATUS_SOLVED => "Solved", STATUS_REJECTED => "Rejected"}
end
def color
if status == STATUS_SOLVED
"green"
elsif status == STATUS_OPEN
"yellow"
elsif status == STATUS_REJECTED
"red"
end
end
def init_variables
self.assigned = User.find_by_username(assigned_name) if assigned_name
self.status = STATUS_OPEN unless status
end
def validate_status
errors.add :status, I18n.t(:invalid_status) unless statuses.include? status
end
def parse_text
if self.text
self.text_parsed = bbcode_to_html(self.text)
end
end
def solution_formatted
bbcode_to_html(solution)
end
def can_show? cuser
return false unless cuser
return true if cuser.admin?
((author == cuser) or (Issue::allowed_categories(cuser).include?(self.category_id)))
end
def can_create? cuser
true
end
def can_update?(cuser, params = {})
return false unless cuser
return true if cuser.admin?
return false unless Issue::allowed_categories(cuser).include?(self.category_id)
!(params.member?(:category_id) && (self.category_id.to_s != params[:category_id]))
end
def can_destroy? cuser
cuser and cuser.admin?
end
# STATIC METHODS
def self.allowed_categories cuser
allowed = []
allowed << CATEGORY_GATHER if cuser.admin? || cuser.gather_moderator? # gather
allowed << CATEGORY_WEBSITE if cuser.admin? # website
allowed << CATEGORY_LEAGUE if cuser.admin? # league
allowed << CATEGORY_NSLPLUGIN if cuser.admin? # ensl plugin
allowed
end
def self.params(params, cuser)
params.require(:issue).permit(:solution, :status, :text, :title,
:assigned_id, :author_id, :category_id)
end
end