projects_controller.rb 2.54 KB
Newer Older
randx's avatar
randx committed
1
require Rails.root.join('lib', 'gitlab', 'graph', 'json_builder')
Valery Sizov's avatar
Valery Sizov committed
2

3
class ProjectsController < ProjectResourceController
4
  skip_before_filter :project, only: [:new, :create]
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
5
6

  # Authorize
7
8
9
  before_filter :authorize_read_project!, except: [:index, :new, :create]
  before_filter :authorize_admin_project!, only: [:edit, :update, :destroy]
  before_filter :require_non_empty_project, only: [:blob, :tree, :graph]
gitlabhq's avatar
gitlabhq committed
10

11
12
  layout 'application', only: [:new, :create]

gitlabhq's avatar
gitlabhq committed
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)
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
22
23

    respond_to do |format|
24
      flash[:notice] = 'Project was successfully created.' if @project.saved?
25
26
      format.html do
        if @project.saved?
27
          redirect_to @project
28
29
30
        else
          render action: "new"
        end
gitlabhq's avatar
gitlabhq committed
31
32
33
34
35
36
      end
      format.js
    end
  end

  def update
37
    status = ProjectUpdateContext.new(project, current_user, params).execute
38

gitlabhq's avatar
gitlabhq committed
39
    respond_to do |format|
40
      if status
41
        flash[:notice] = 'Project was successfully updated.'
42
        format.html { redirect_to edit_project_path(project), notice: 'Project was successfully updated.' }
Nihad Abbasov's avatar
Nihad Abbasov committed
43
        format.js
gitlabhq's avatar
gitlabhq committed
44
45
      else
        format.html { render action: "edit" }
Nihad Abbasov's avatar
Nihad Abbasov committed
46
        format.js
gitlabhq's avatar
gitlabhq committed
47
      end
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
48
49
50
51
    end
  end

  def show
52
    limit = (params[:limit] || 20).to_i
53
    @events = @project.events.recent.limit(limit).offset(params[:offset] || 0)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
54
55

    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
56
      format.html do
57
         unless @project.empty_repo?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
58
           @last_push = current_user.recent_push(@project.id)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59
60
61
62
63
           render :show
         else
           render "projects/empty"
         end
      end
64
      format.js
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
65
    end
66
67
68
  end

  def files
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
69
    @notes = @project.notes.where("attachment != 'NULL'").order("created_at DESC").limit(100)
70
71
  end

gitlabhq's avatar
gitlabhq committed
72
73
74
75
76
  #
  # Wall
  #

  def wall
77
    return render_404 unless @project.wall_enabled
gitlabhq's avatar
gitlabhq committed
78
    @note = Note.new
gitlabhq's avatar
gitlabhq committed
79

80
    respond_to do |format|
gitlabhq's avatar
gitlabhq committed
81
82
      format.html
    end
gitlabhq's avatar
gitlabhq committed
83
84
  end

Valery Sizov's avatar
Valery Sizov committed
85
  def graph
randx's avatar
randx committed
86
87
88
    graph = Gitlab::Graph::JsonBuilder.new(project)

    @days_json, @commits_json = graph.days_json, graph.commits_json
Valery Sizov's avatar
Valery Sizov committed
89
90
  end

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
91
  def destroy
92
93
    return access_denied! unless can?(current_user, :remove_project, project)

94
95
96
    # 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)
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
97
    project.destroy
98
    UsersProject.set_callback(:destroy, :after, :update_repository)
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
99
100

    respond_to do |format|
randx's avatar
randx committed
101
      format.html { redirect_to root_path }
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
102
103
104
    end
  end
end