1 # == Schema Information
5 # id :integer not null, primary key
6 # reportable_type :string not null
7 # reportable_id :integer not null
8 # reported_user_id :integer
9 # status :enum default("open"), not null
10 # assigned_role :enum not null
11 # resolved_at :datetime
12 # resolved_by :integer
14 # reports_count :integer default(0)
15 # created_at :datetime not null
16 # updated_at :datetime not null
20 # index_issues_on_assigned_role (assigned_role)
21 # index_issues_on_reportable_type_and_reportable_id (reportable_type,reportable_id)
22 # index_issues_on_reported_user_id (reported_user_id)
23 # index_issues_on_status (status)
24 # index_issues_on_updated_by (updated_by)
28 # issues_reported_user_id_fkey (reported_user_id => users.id)
29 # issues_resolved_by_fkey (resolved_by => users.id)
30 # issues_updated_by_fkey (updated_by => users.id)
33 class Issue < ApplicationRecord
34 belongs_to :reportable, :polymorphic => true
35 belongs_to :reported_user, :class_name => "User", :optional => true
36 belongs_to :user_resolved, :class_name => "User", :foreign_key => :resolved_by, :optional => true
37 belongs_to :user_updated, :class_name => "User", :foreign_key => :updated_by, :optional => true
39 has_many :reports, :dependent => :destroy
40 has_many :comments, :class_name => "IssueComment", :dependent => :destroy
42 validates :reportable_id, :uniqueness => { :scope => [:reportable_type] }
44 ASSIGNED_ROLES = %w[administrator moderator].freeze
45 validates :assigned_role, :presence => true, :inclusion => ASSIGNED_ROLES
47 before_validation :set_reported_user
49 scope :with_status, ->(issue_status) { where(:status => issue_status) }
50 scope :visible_to, ->(user) { where(:assigned_role => user.roles.map(&:role)) }
53 resolved_at.present? ? reports.where("updated_at < ?", resolved_at) : nil
57 resolved_at.present? ? reports.where("updated_at >= ?", resolved_at) : reports
61 aasm :column => :status, :no_direct_assignment => true do
62 state :open, :initial => true
67 transitions :from => :open, :to => :ignored
71 transitions :from => :open, :to => :resolved
73 self.resolved_at = Time.now.utc
78 transitions :from => :resolved, :to => :open
79 transitions :from => :ignored, :to => :open
86 self.reported_user = case reportable.class.name