groups_controller.rb 2.0 KB
Newer Older
1
class Admin::GroupsController < Admin::ApplicationController
A
Andrey Kumanyaev 已提交
2
  before_filter :group, only: [:edit, :show, :update, :destroy, :project_update, :project_teams_update]
3 4

  def index
5
    @groups = Group.order('name ASC')
6 7 8 9 10 11 12 13
    @groups = @groups.search(params[:name]) if params[:name].present?
    @groups = @groups.page(params[:page]).per(20)
  end

  def show
    @projects = Project.scoped
    @projects = @projects.not_in_group(@group) if @group.projects.present?
    @projects = @projects.all
14
    @projects.reject!(&:empty_repo?)
A
Andrey Kumanyaev 已提交
15 16

    @users = User.active
17 18 19 20 21 22 23 24 25 26 27
  end

  def new
    @group = Group.new
  end

  def edit
  end

  def create
    @group = Group.new(params[:group])
28
    @group.path = @group.name.dup.parameterize if @group.name
29 30 31 32 33 34 35 36 37 38
    @group.owner = current_user

    if @group.save
      redirect_to [:admin, @group], notice: 'Group was successfully created.'
    else
      render action: "new"
    end
  end

  def update
39 40
    group_params = params[:group].dup
    owner_id =group_params.delete(:owner_id)
41 42 43 44 45

    if owner_id
      @group.owner = User.find(owner_id)
    end

46
    if @group.update_attributes(group_params)
47 48 49 50 51 52 53 54
      redirect_to [:admin, @group], notice: 'Group was successfully updated.'
    else
      render action: "edit"
    end
  end

  def project_update
    project_ids = params[:project_ids]
55 56

    Project.where(id: project_ids).each do |project|
57
      project.transfer(@group)
58
    end
59 60 61 62

    redirect_to :back, notice: 'Group was successfully updated.'
  end

63 64
  def remove_project
    @project = Project.find(params[:project_id])
65
    @project.transfer(nil)
66 67 68 69

    redirect_to :back, notice: 'Group was successfully updated.'
  end

A
Andrey Kumanyaev 已提交
70
  def project_teams_update
71
    @group.add_users_to_project_teams(params[:user_ids], params[:project_access])
A
Andrey Kumanyaev 已提交
72 73 74
    redirect_to [:admin, @group], notice: 'Users was successfully added.'
  end

75
  def destroy
76 77
    @group.truncate_teams

78 79
    @group.destroy

R
randx 已提交
80
    redirect_to admin_groups_path, notice: 'Group was successfully deleted.'
81 82 83 84 85
  end

  private

  def group
86
    @group = Group.find_by_path(params[:id])
87 88
  end
end