]> git.openstreetmap.org Git - rails.git/blob - app/models/issue.rb
Added authorization + issues dashboard
[rails.git] / app / models / issue.rb
1 class Issue < ActiveRecord::Base
2         belongs_to :reportable, :polymorphic => true
3         has_many :reports
4         validates :reportable_id, :uniqueness => { :scope => [ :reportable_type ] }
5         belongs_to :user
6
7         # Check if more statuses are needed
8         enum status: %w( open ignored resolved )
9
10         scope :with_status, -> (issue_status) { where(:status => statuses[issue_status])}
11
12         def read_reports
13                 resolved_at.present? ? reports.where("created_at < ?", resolved_at) : nil
14         end
15
16         def unread_reports
17     resolved_at.present? ? reports.where("created_at >= ?", resolved_at) : reports
18         end
19
20         include AASM
21         aasm :column => :status, :no_direct_assignment => true do
22                 state :open, :initial => true
23                 state :ignored
24                 state :resolved
25
26                 event :ignore do
27                         transitions :from => :open, :to => :ignored 
28                 end
29
30                 event :resolve do
31                         transitions :from => :open, :to => :resolved
32                         after do
33                                 self.resolved_at = Time.now.getutc
34                         end
35                 end
36
37                 event :reopen do
38                         transitions :from => :resolved, :to => :open
39                 end
40
41         end
42
43 end