summaryrefslogtreecommitdiffstats
path: root/app/models/committership.rb
blob: 637a9e2e19304e34b39751e749b2aca3627f645b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#   Copyright (C) 2007 Johan Sørensen <johan@johansorensen.com>
#   Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
#   Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

class Committership < ActiveRecord::Base
  belongs_to :committer, :polymorphic => true
  belongs_to :repository
  belongs_to :creator, :class_name => 'User'
  
  validates_presence_of :committer_id, :committer_type, :repository_id
  after_create :notify_repository_owners
  after_create :add_new_committer_event
  after_destroy :add_removed_committer_event
  
  named_scope :groups, :conditions => { :committer_type => "Group" }
  named_scope :users,  :conditions => { :committer_type => "User" }  
  
  def breadcrumb_parent
    Breadcrumb::Committerships.new(repository)
  end
  
  def title
    new_record? ? "New committer" : "Committer"
  end
  
  # returns all the users in this committership, eg if it's a group it'll 
  # return an array of the group members, otherwise a single-member array of
  # the user
  def members
    case committer
    when Group
      committer.members
    else
      [committer]
    end
  end
  
  protected
    def notify_repository_owners
      return unless creator
      recipients = repository.owners
      recipients.each do |r|
        message = Message.new({
          :sender => creator,
          :recipient => r,
          :subject => I18n.t("committership.notification_subject"),
          :body => I18n.t("committership.notification_body", {
            :inviter => creator.title,
            :user => committer.title,
            :repository => repository.name,
            :project => repository.project.title
          }),
          :notifiable => self
        })
        message.save
      end
    end
    
    def add_new_committer_event
      repository.project.create_event(Action::ADD_COMMITTER, repository, 
                                      creator, committer.title)
    end
    
    def add_removed_committer_event
      repository.project.create_event(Action::REMOVE_COMMITTER, repository, 
                                      creator, committer.title)
    end
end