Read about our upcoming Code of Conduct on this issue

event.rb 2.98 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
class Event < ActiveRecord::Base
2
3
  include PushEvent

4
5
6
  attr_accessible :project, :action, :data, :author_id, :project_id,
                  :target_id, :target_type

7
8
  default_scope where("author_id IS NOT NULL")

9
10
11
12
13
14
  Created   = 1
  Updated   = 2
  Closed    = 3
  Reopened  = 4
  Pushed    = 5
  Commented = 6
15
  Merged    = 7
Alex Denisov's avatar
Alex Denisov committed
16
  Joined    = 8 # User joined project
Alex Denisov's avatar
Alex Denisov committed
17
  Left      = 9 # User left project
18

Nihad Abbasov's avatar
Nihad Abbasov committed
19
20
21
22
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
  delegate :title, to: :issue, prefix: true, allow_nil: true
  delegate :title, to: :merge_request, prefix: true, allow_nil: true

randx's avatar
randx committed
23
  belongs_to :author, class_name: "User"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
24
  belongs_to :project
25
  belongs_to :target, polymorphic: true
26

randx's avatar
randx committed
27
28
  # For Hash only
  serialize :data
29

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
30
  # Scopes
31
  scope :recent, order("created_at DESC")
32
  scope :code_push, where(action: Pushed)
33

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
34
35
36
37
38
39
40
  class << self
    def determine_action(record)
      if [Issue, MergeRequest].include? record.class
        Event::Created
      elsif record.kind_of? Note
        Event::Commented
      end
41
    end
42

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
43
44
45
    def recent_for_user user
      where(project_id: user.projects.map(&:id)).recent
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
46
47
  end

48
  # Next events currently enabled for system
49
  #  - push
50
51
  #  - new issue
  #  - merge request
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
52
  def allowed?
Alex Denisov's avatar
Alex Denisov committed
53
    push? || issue? || merge_request? || membership_changed?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
54
55
  end

56
57
58
59
60
61
62
63
  def project_name
    if project
      project.name
    else
      "(deleted)"
    end
  end

64
65
66
67
  def target_title
    target.try :title
  end

68
  def push?
69
    action == self.class::Pushed && valid_push?
70
71
  end

72
73
74
75
  def merged?
    action == self.class::Merged
  end

76
77
78
79
80
81
82
83
  def closed?
    action == self.class::Closed
  end

  def reopened?
    action == self.class::Reopened
  end

84
  def issue?
85
    target_type == "Issue"
86
87
  end

88
  def merge_request?
89
    target_type == "MergeRequest"
90
91
  end

92
93
  def new_issue?
    target_type == "Issue" &&
94
95
96
      action == Created
  end

97
98
  def new_merge_request?
    target_type == "MergeRequest" &&
99
100
101
      action == Created
  end

102
103
  def changed_merge_request?
    target_type == "MergeRequest" &&
104
105
106
      [Closed, Reopened].include?(action)
  end

107
108
  def changed_issue?
    target_type == "Issue" &&
109
110
111
      [Closed, Reopened].include?(action)
  end

Alex Denisov's avatar
Alex Denisov committed
112
  def joined?
Alex Denisov's avatar
Alex Denisov committed
113
114
115
116
117
118
119
120
121
    action == Joined
  end

  def left?
    action == Left
  end

  def membership_changed?
    joined? || left?
Alex Denisov's avatar
Alex Denisov committed
122
123
  end

124
  def issue
125
126
127
128
129
130
131
    target if target_type == "Issue"
  end

  def merge_request
    target if target_type == "MergeRequest"
  end

132
  def author
133
    @author ||= User.find(author_id)
134
  end
135
136
137
138
139
140

  def action_name
    if closed?
      "closed"
    elsif merged?
      "merged"
Alex Denisov's avatar
Alex Denisov committed
141
142
    elsif joined?
      'joined'
Alex Denisov's avatar
Alex Denisov committed
143
144
    elsif left?
      'left'
145
    else
146
      "opened"
147
148
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
149
end
Nihad Abbasov's avatar
Nihad Abbasov committed
150

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
151
152
153
154
# == Schema Information
#
# Table name: events
#
Nihad Abbasov's avatar
Nihad Abbasov committed
155
#  id          :integer         not null, primary key
156
#  target_type :string(255)
Nihad Abbasov's avatar
Nihad Abbasov committed
157
#  target_id   :integer
158
159
#  title       :string(255)
#  data        :text
Nihad Abbasov's avatar
Nihad Abbasov committed
160
#  project_id  :integer
161
162
#  created_at  :datetime        not null
#  updated_at  :datetime        not null
Nihad Abbasov's avatar
Nihad Abbasov committed
163
164
#  action      :integer
#  author_id   :integer
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
165
#