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
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_reportable_type_and_reportable_id (reportable_type,reportable_id)
21 # index_issues_on_reported_user_id (reported_user_id)
22 # index_issues_on_updated_by (updated_by)
26 # issues_reported_user_id_fkey (reported_user_id => users.id)
27 # issues_resolved_by_fkey (resolved_by => users.id)
28 # issues_updated_by_fkey (updated_by => users.id)
31 class Issue < ActiveRecord::Base
32 belongs_to :reportable, :polymorphic => true
33 belongs_to :reported_user, :class_name => "User", :foreign_key => :reported_user_id
34 belongs_to :user_resolved, :class_name => "User", :foreign_key => :resolved_by
35 belongs_to :user_updated, :class_name => "User", :foreign_key => :updated_by
37 has_many :reports, :dependent => :destroy
38 has_many :comments, :class_name => "IssueComment", :dependent => :destroy
40 validates :reportable_id, :uniqueness => { :scope => [:reportable_type] }
42 ASSIGNED_ROLES = %w[administrator moderator].freeze
43 validates :assigned_role, :presence => true, :inclusion => ASSIGNED_ROLES
45 before_validation :set_default_assigned_role
46 before_validation :set_reported_user
48 # Check if more statuses are needed
49 enum :status => %w[open ignored resolved]
51 scope :with_status, ->(issue_status) { where(:status => statuses[issue_status]) }
54 resolved_at.present? ? reports.where("updated_at < ?", resolved_at) : nil
58 resolved_at.present? ? reports.where("updated_at >= ?", resolved_at) : reports
62 aasm :column => :status, :no_direct_assignment => true do
63 state :open, :initial => true
68 transitions :from => :open, :to => :ignored
72 transitions :from => :open, :to => :resolved
74 self.resolved_at = Time.now.getutc
79 transitions :from => :resolved, :to => :open
80 transitions :from => :ignored, :to => :open
87 self.reported_user = case reportable.class.name
97 def set_default_assigned_role
98 role = %w[Note].include?(reportable.class.name) ? "moderator" : "administrator"
99 self.assigned_role = role if assigned_role.blank?