projects_controller.rb 2.8 KB
Newer Older
V
Valery Sizov 已提交
1 2
require File.join(Rails.root, 'lib', 'graph_commit')

G
gitlabhq 已提交
3
class ProjectsController < ApplicationController
N
Nihad Abbasov 已提交
4
  before_filter :project, :except => [:index, :new, :create]
G
gitlabhq 已提交
5
  layout :determine_layout
G
gitlabhq 已提交
6 7 8

  # Authorize
  before_filter :add_project_abilities
N
Nihad Abbasov 已提交
9 10
  before_filter :authorize_read_project!, :except => [:index, :new, :create]
  before_filter :authorize_admin_project!, :only => [:edit, :update, :destroy]
D
Dmitriy Zaporozhets 已提交
11
  before_filter :require_non_empty_project, :only => [:blob, :tree, :graph]
G
gitlabhq 已提交
12

G
gitlabhq 已提交
13 14 15 16 17 18 19 20
  def new
    @project = Project.new
  end

  def edit
  end

  def create
21
    @project = Project.create_by_user(params[:project], current_user)
G
gitlabhq 已提交
22 23 24 25

    respond_to do |format|
      if @project.valid?
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
N
Nihad Abbasov 已提交
26
        format.js
G
gitlabhq 已提交
27 28 29 30 31
      else
        format.html { render action: "new" }
        format.js
      end
    end
32
  rescue Gitlab::Gitolite::AccessDenied
33
    render :js => "location.href = '#{errors_githost_path}'" and return
G
gitlabhq 已提交
34 35 36 37 38 39 40
  rescue StandardError => ex
    @project.errors.add(:base, "Cant save project. Please try again later")
    respond_to do |format|
      format.html { render action: "new" }
      format.js
    end
  end
G
gitlabhq 已提交
41

G
gitlabhq 已提交
42
  def update
G
gitlabhq 已提交
43
    respond_to do |format|
G
gitlabhq 已提交
44
      if project.update_attributes(params[:project])
45
        format.html { redirect_to edit_project_path(project), :notice => 'Project was successfully updated.' }
N
Nihad Abbasov 已提交
46
        format.js
G
gitlabhq 已提交
47 48
      else
        format.html { render action: "edit" }
N
Nihad Abbasov 已提交
49
        format.js
G
gitlabhq 已提交
50
      end
G
gitlabhq 已提交
51 52 53 54
    end
  end

  def show
55 56
    limit = (params[:limit] || 20).to_i
    @events = @project.events.recent.limit(limit)
D
Dmitriy Zaporozhets 已提交
57 58

    respond_to do |format|
N
Nihad Abbasov 已提交
59
      format.html do
D
Dmitriy Zaporozhets 已提交
60
         if @project.repo_exists? && @project.has_commits?
D
Dmitriy Zaporozhets 已提交
61
           @last_push = current_user.recent_push(@project.id)
D
Dmitriy Zaporozhets 已提交
62 63 64 65 66 67
           render :show
         else
           render "projects/empty"
         end
      end
    end
68 69 70
  end

  def files
D
Dmitriy Zaporozhets 已提交
71
    @notes = @project.notes.where("attachment != 'NULL'").order("created_at DESC").limit(100)
72 73
  end

G
gitlabhq 已提交
74 75 76
  #
  # Wall
  #
77

G
gitlabhq 已提交
78
  def wall
79
    return render_404 unless @project.wall_enabled
G
gitlabhq 已提交
80
    @note = Note.new
G
gitlabhq 已提交
81

N
Nihad Abbasov 已提交
82
    respond_to do |format|
G
gitlabhq 已提交
83 84
      format.html
    end
G
gitlabhq 已提交
85
  end
86

V
Valery Sizov 已提交
87
  def graph
88
    @days_json, @commits_json = GraphCommit.to_graph(project)
V
Valery Sizov 已提交
89 90
  end

G
gitlabhq 已提交
91
  def destroy
92 93 94
    # Disable the UsersProject update_repository call, otherwise it will be
    # called once for every person removed from the project
    UsersProject.skip_callback(:destroy, :after, :update_repository)
G
gitlabhq 已提交
95
    project.destroy
96
    UsersProject.set_callback(:destroy, :after, :update_repository)
G
gitlabhq 已提交
97 98

    respond_to do |format|
R
randx 已提交
99
      format.html { redirect_to root_path }
G
gitlabhq 已提交
100 101 102
    end
  end

N
Nihad Abbasov 已提交
103
  protected
G
gitlabhq 已提交
104

N
Nihad Abbasov 已提交
105
  def project
G
gitlabhq 已提交
106
    @project ||= Project.find_by_code(params[:id])
107
    @project || render_404
G
gitlabhq 已提交
108
  end
G
gitlabhq 已提交
109 110 111 112 113 114 115 116

  def determine_layout
    if @project && !@project.new_record?
      "project"
    else
      "application"
    end
  end
G
gitlabhq 已提交
117
end