summaryrefslogtreecommitdiffstats
path: root/app/controllers/groups_controller.rb
blob: b01586067e0164e1ea96e975a7aeffbaaf292c80 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
#   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 GroupsController < ApplicationController
  before_filter :login_required, :except => [:index, :show]
  before_filter :find_group_and_ensure_group_adminship, :only => [:edit, :update, :avatar]
  renders_in_global_context
  
  def index
    @groups = Group.paginate(:all, :page => params[:page])
  end
  
  def show
    @group = Group.find_by_name!(params[:id], 
              :include => [:members, :projects, :repositories, :committerships])
    @events = Event.top.paginate(:all, 
      :page => params[:page],
      :conditions => ["events.user_id in (:user_ids) and events.project_id in (:project_ids)", {
        :user_ids => @group.members.map{|u| u.id },
        :project_ids => @group.all_related_project_ids,
      }], 
      :order => "events.created_at desc", 
      :include => [:user, :project])
    @memberships = @group.memberships.find(:all, :include => [:user, :role])
  end
  
  def new
    @group = Group.new
  end
  
  def edit
  end
  
  def update
    @group.description = params[:group][:description]
    @group.avatar = params[:group][:avatar]
    @group.save!
    redirect_to group_path(@group)
    rescue ActiveRecord::RecordInvalid
      render :action => 'edit'
  end
  
  def create
    @group = Group.new(params[:group])
    @group.transaction do
      @group.creator = current_user
      @group.save!
      @group.memberships.create!({
        :user => current_user,
        :role => Role.admin,
      })
    end
    flash[:success] = I18n.t "groups_controller.group_created"
    redirect_to group_path(@group)
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound
    render :action => "new"
  end
  
  def destroy
    @group = Group.find_by_name!(params[:id])
    if current_user.site_admin? || (@group.admin?(current_user) && @group.deletable?)
      @group.destroy
      flash[:success] = "The team was deleted"
      redirect_to groups_path
    else
      flash[:error] = "The team can't be deleted, since there's other members in it"
      redirect_to group_path(@group)
    end
  end
  
  # DELETE avatar
  def avatar
    @group.avatar.destroy
    @group.save
    flash[:success] = "The team image was deleted"
    redirect_to group_path(@group)
  end
  
  def auto_complete_for_project_slug
    @projects = Project.find(:all, 
      :conditions => ['LOWER(slug) LIKE ?', "%#{params[:project][:slug].downcase}%"],
      :limit => 10)
    render :layout => false
  end

  protected
    def find_group_and_ensure_group_adminship
      @group = Group.find_by_name!(params[:id])
      unless @group.admin?(current_user)
        access_denied and return
      end
    end
end