event.rb 2.98 KB
Newer Older
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
#

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
class Event < ActiveRecord::Base
18 19
  include PushEvent

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

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

25 26 27 28 29 30
  Created   = 1
  Updated   = 2
  Closed    = 3
  Reopened  = 4
  Pushed    = 5
  Commented = 6
31
  Merged    = 7
32
  Joined    = 8 # User joined project
Alex Denisov's avatar
Alex Denisov committed
33
  Left      = 9 # User left project
34

Nihad Abbasov's avatar
Nihad Abbasov committed
35 36 37 38
  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
39
  belongs_to :author, class_name: "User"
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
40
  belongs_to :project
41
  belongs_to :target, polymorphic: true
42

randx's avatar
randx committed
43 44
  # For Hash only
  serialize :data
45

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
46
  # Scopes
47
  scope :recent, order("created_at DESC")
48
  scope :code_push, where(action: Pushed)
49
  scope :in_projects, ->(project_ids) { where(project_id: project_ids).recent }
50

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
51 52 53 54 55 56 57
  class << self
    def determine_action(record)
      if [Issue, MergeRequest].include? record.class
        Event::Created
      elsif record.kind_of? Note
        Event::Commented
      end
58
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59 60
  end

61
  # Next events currently enabled for system
62
  #  - push
63 64
  #  - new issue
  #  - merge request
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
65
  def allowed?
Alex Denisov's avatar
Alex Denisov committed
66
    push? || issue? || merge_request? || membership_changed?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
67 68
  end

69 70 71 72
  def project_name
    if project
      project.name
    else
Riyad Preukschas's avatar
Riyad Preukschas committed
73
      "(deleted project)"
74 75 76
    end
  end

77 78 79 80
  def target_title
    target.try :title
  end

81
  def push?
82
    action == self.class::Pushed && valid_push?
83 84
  end

85 86 87 88
  def merged?
    action == self.class::Merged
  end

89 90 91 92 93 94 95 96
  def closed?
    action == self.class::Closed
  end

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

97
  def issue?
98
    target_type == "Issue"
99 100
  end

101
  def merge_request?
102
    target_type == "MergeRequest"
103 104
  end

105 106
  def new_issue?
    target_type == "Issue" &&
107 108 109
      action == Created
  end

110 111
  def new_merge_request?
    target_type == "MergeRequest" &&
112 113 114
      action == Created
  end

115 116
  def changed_merge_request?
    target_type == "MergeRequest" &&
117 118 119
      [Closed, Reopened].include?(action)
  end

120 121
  def changed_issue?
    target_type == "Issue" &&
122 123 124
      [Closed, Reopened].include?(action)
  end

125
  def joined?
Alex Denisov's avatar
Alex Denisov committed
126 127 128 129 130 131 132 133 134
    action == Joined
  end

  def left?
    action == Left
  end

  def membership_changed?
    joined? || left?
135 136
  end

137
  def issue
138 139 140 141 142 143 144
    target if target_type == "Issue"
  end

  def merge_request
    target if target_type == "MergeRequest"
  end

145
  def author
146
    @author ||= User.find(author_id)
147
  end
148 149 150 151 152 153

  def action_name
    if closed?
      "closed"
    elsif merged?
      "merged"
154 155
    elsif joined?
      'joined'
Alex Denisov's avatar
Alex Denisov committed
156 157
    elsif left?
      'left'
158
    else
159
      "opened"
160 161
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
162
end