1 class Issue < ActiveRecord::Base
2 belongs_to :reportable, :polymorphic => true
3 belongs_to :reported_user, :class_name => "User", :foreign_key => :reported_user_id
4 belongs_to :user_updated, :class_name => "User", :foreign_key => :updated_by
6 has_many :reports, :dependent => :destroy
7 has_many :comments, :class_name => "IssueComment", :dependent => :destroy
9 validates :reportable_id, :uniqueness => { :scope => [:reportable_type] }
10 validates :reported_user_id, :presence => true
12 before_validation :set_reported_user
14 # Check if more statuses are needed
15 enum :status => %w[open ignored resolved]
16 enum :type => %w[administrator moderator]
18 scope :with_status, ->(issue_status) { where(:status => statuses[issue_status]) }
21 resolved_at.present? ? reports.where("updated_at < ?", resolved_at) : nil
25 resolved_at.present? ? reports.where("updated_at >= ?", resolved_at) : reports
29 aasm :column => :status, :no_direct_assignment => true do
30 state :open, :initial => true
35 transitions :from => :open, :to => :ignored
39 transitions :from => :open, :to => :resolved
41 self.resolved_at = Time.now.getutc
46 transitions :from => :resolved, :to => :open
47 transitions :from => :ignored, :to => :open
54 self.reported_user = case reportable.class.name