event.rb 3.1 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# == Schema Information
#
# Table name: events
#
#  id          :integer          not null, primary key
#  target_type :string(255)
#  target_id   :integer
#  title       :string(255)
#  data        :text
#  project_id  :integer
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  action      :integer
#  author_id   :integer
#

D
Dmitriy Zaporozhets 已提交
17
class Event < ActiveRecord::Base
18
  include NoteEvent
19 20
  include PushEvent

21 22 23
  attr_accessible :project, :action, :data, :author_id, :project_id,
                  :target_id, :target_type

24 25
  default_scope where("author_id IS NOT NULL")

26 27 28 29 30 31
  Created   = 1
  Updated   = 2
  Closed    = 3
  Reopened  = 4
  Pushed    = 5
  Commented = 6
32
  Merged    = 7
A
Alex Denisov 已提交
33
  Joined    = 8 # User joined project
A
Alex Denisov 已提交
34
  Left      = 9 # User left project
35

N
Nihad Abbasov 已提交
36 37 38 39
  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

R
randx 已提交
40
  belongs_to :author, class_name: "User"
D
Dmitriy Zaporozhets 已提交
41
  belongs_to :project
42
  belongs_to :target, polymorphic: true
43

R
randx 已提交
44 45
  # For Hash only
  serialize :data
46

A
Andrey Kumanyaev 已提交
47
  # Scopes
48
  scope :recent, order("created_at DESC")
49
  scope :code_push, where(action: Pushed)
50
  scope :in_projects, ->(project_ids) { where(project_id: project_ids).recent }
51

A
Andrey Kumanyaev 已提交
52 53 54 55 56 57 58
  class << self
    def determine_action(record)
      if [Issue, MergeRequest].include? record.class
        Event::Created
      elsif record.kind_of? Note
        Event::Commented
      end
59
    end
D
Dmitriy Zaporozhets 已提交
60 61
  end

62 63 64 65 66 67 68 69
  def proper?
    if push?
      true
    elsif membership_changed?
      true
    else
      (issue? || merge_request? || note? || milestone?) && target
    end
D
Dmitriy Zaporozhets 已提交
70 71
  end

72 73 74 75
  def project_name
    if project
      project.name
    else
R
Riyad Preukschas 已提交
76
      "(deleted project)"
77 78 79
    end
  end

80 81 82 83
  def target_title
    target.try :title
  end

84
  def push?
85
    action == self.class::Pushed && valid_push?
86 87
  end

88 89 90 91
  def merged?
    action == self.class::Merged
  end

92 93 94 95 96 97 98 99
  def closed?
    action == self.class::Closed
  end

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

100 101 102 103 104 105 106 107
  def milestone?
    target_type == "Milestone"
  end

  def note?
    target_type == "Note"
  end

108
  def issue?
109
    target_type == "Issue"
110 111
  end

112
  def merge_request?
113
    target_type == "MergeRequest"
114 115
  end

116 117
  def new_issue?
    target_type == "Issue" &&
118 119 120
      action == Created
  end

121 122
  def new_merge_request?
    target_type == "MergeRequest" &&
123 124 125
      action == Created
  end

126 127
  def changed_merge_request?
    target_type == "MergeRequest" &&
128 129 130
      [Closed, Reopened].include?(action)
  end

131 132
  def changed_issue?
    target_type == "Issue" &&
133 134 135
      [Closed, Reopened].include?(action)
  end

A
Alex Denisov 已提交
136
  def joined?
A
Alex Denisov 已提交
137 138 139 140 141 142 143 144 145
    action == Joined
  end

  def left?
    action == Left
  end

  def membership_changed?
    joined? || left?
A
Alex Denisov 已提交
146 147
  end

148
  def issue
149 150 151 152 153 154 155
    target if target_type == "Issue"
  end

  def merge_request
    target if target_type == "MergeRequest"
  end

156
  def author
157
    @author ||= User.find(author_id)
158
  end
159 160 161 162 163 164

  def action_name
    if closed?
      "closed"
    elsif merged?
      "merged"
A
Alex Denisov 已提交
165 166
    elsif joined?
      'joined'
A
Alex Denisov 已提交
167 168
    elsif left?
      'left'
169
    else
170
      "opened"
171 172
    end
  end
D
Dmitriy Zaporozhets 已提交
173
end