event.rb 2.85 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
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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
23
  belongs_to :project
24
  belongs_to :target, polymorphic: true
25

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

29
  scope :recent, order("created_at DESC")
30
  scope :code_push, where(action: Pushed)
31

32 33 34 35 36 37 38
  def self.determine_action(record)
    if [Issue, MergeRequest].include? record.class
      Event::Created
    elsif record.kind_of? Note
      Event::Commented
    end
  end
39

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
40
  def self.recent_for_user user
41
    where(project_id: user.projects.map(&:id)).recent
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
42 43
  end

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

52 53 54 55 56 57 58 59
  def project_name
    if project
      project.name
    else
      "(deleted)"
    end
  end

60
  def push?
61
    action == self.class::Pushed && valid_push?
62 63
  end

64 65 66 67
  def merged?
    action == self.class::Merged
  end

68 69 70 71 72 73 74 75
  def closed?
    action == self.class::Closed
  end

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

76
  def issue?
77
    target_type == "Issue"
78 79
  end

80
  def merge_request?
81
    target_type == "MergeRequest"
82 83
  end

84 85
  def new_issue?
    target_type == "Issue" &&
86 87 88
      action == Created
  end

89 90
  def new_merge_request?
    target_type == "MergeRequest" &&
91 92 93
      action == Created
  end

94 95
  def changed_merge_request?
    target_type == "MergeRequest" &&
96 97 98
      [Closed, Reopened].include?(action)
  end

99 100
  def changed_issue?
    target_type == "Issue" &&
101 102 103
      [Closed, Reopened].include?(action)
  end

104
  def joined?
Alex Denisov's avatar
Alex Denisov committed
105 106 107 108 109 110 111 112 113
    action == Joined
  end

  def left?
    action == Left
  end

  def membership_changed?
    joined? || left?
114 115
  end

116
  def issue
117 118 119 120 121 122 123
    target if target_type == "Issue"
  end

  def merge_request
    target if target_type == "MergeRequest"
  end

124
  def author
125
    @author ||= User.find(author_id)
126
  end
127 128 129 130 131 132

  def action_name
    if closed?
      "closed"
    elsif merged?
      "merged"
133 134
    elsif joined?
      'joined'
Alex Denisov's avatar
Alex Denisov committed
135 136
    elsif left?
      'left'
137
    else
138
      "opened"
139 140
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
141
end
Nihad Abbasov's avatar
Nihad Abbasov committed
142

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
143 144 145 146
# == Schema Information
#
# Table name: events
#
Nihad Abbasov's avatar
Nihad Abbasov committed
147
#  id          :integer         not null, primary key
148
#  target_type :string(255)
Nihad Abbasov's avatar
Nihad Abbasov committed
149
#  target_id   :integer
150 151
#  title       :string(255)
#  data        :text
Nihad Abbasov's avatar
Nihad Abbasov committed
152
#  project_id  :integer
153 154
#  created_at  :datetime        not null
#  updated_at  :datetime        not null
Nihad Abbasov's avatar
Nihad Abbasov committed
155 156
#  action      :integer
#  author_id   :integer
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
157
#