speling fix
[gitorious:yousource.git] / app / controllers / merge_requests_controller.rb
1 class MergeRequestsController < ApplicationController
2   before_filter :login_required, :except => [:index, :show]
3   before_filter :find_project
4   before_filter :find_repository
5   before_filter :find_merge_request, :except => [:index, :show, :new, :create]
6   before_filter :assert_merge_request_ownership, :except => [:index, :show, :new, :create, :resolve]
7   before_filter :assert_merge_request_resolvable, :only => [:resolve]
8   
9   def index
10     @merge_requests = @repository.merge_requests
11     @comment_count = @repository.comments.count
12     #@proposed_merge_requests = @repository.proposed_merge_requests
13   end
14   
15   def show
16     @merge_request = @repository.merge_requests.find(params[:id])
17     @commits = @merge_request.target_repository.git.commit_deltas_from(@merge_request.source_repository.git)
18   end
19   
20   def new
21     @merge_request = @repository.proposed_merge_requests.new(:user => current_user)
22     @repositories = @project.repositories.find(:all, :conditions => ["id != ?", @repository.id])
23   end
24   
25   def create
26     @merge_request = @repository.proposed_merge_requests.new(params[:merge_request])
27     @merge_request.user = current_user
28     if @merge_request.save
29       flash[:success] = %Q{You sent a merge request to "#{@merge_request.target_repository.name}"}
30       redirect_to project_repository_path(@project, @repository) and return
31     else
32       @repositories = @project.repositories.find(:all, :conditions => ["id != ?", @repository.id])
33       render :action => "new"
34     end
35   end
36   
37   def resolve
38     # TODO: put to change status
39     @merge_request.status = params[:merge_request][:status]
40     if @merge_request.save
41       flash[:notice] = "The merge request was marked as #{@merge_request.status_string}"
42     end
43     redirect_to [@project, @repository, @merge_request]      
44   end
45   
46   def edit
47     @repositories = @project.repositories.find(:all, :conditions => ["id != ?", @repository.id])
48   end
49   
50   def update
51     @merge_request.attributes = params[:merge_request]
52     if @merge_request.save
53       flash[:success] = "Merge request was updated"
54       redirect_to [@project, @repository, @merge_request]
55     else
56       @repositories = @project.repositories.find(:all, :conditions => ["id != ?", @repository.id])
57       render :action => "edit"
58     end
59   end
60   
61   def destroy
62     @merge_request.destroy
63     flash[:success] = "Merge request was retracted"
64     redirect_to project_repository_path(@project, @repository)
65   end
66   
67   protected
68     def find_repository
69       @repository = @project.repositories.find_by_name!(params[:repository_id])
70     end
71     
72     def find_merge_request
73       @merge_request = @repository.merge_requests.find(params[:id])
74     end
75     
76     def assert_merge_request_resolvable
77       unless @merge_request.resolvable_by?(current_user)
78         respond_to do |format|
79           flash[:error] = "You're not permitted to resolve this merge request"
80           format.html { redirect_to([@project, @repository, @merge_request]) }
81           format.xml  { render :text => "You're not permitted to resolve this merge request", :status => :forbidden }
82         end
83         return
84       end
85     end
86     
87     def assert_merge_request_ownership
88       if @merge_request.user != current_user
89         respond_to do |format|
90           flash[:error] = "You're not the owner of this merge request"
91           format.html { redirect_to([@project, @repository]) }
92           format.xml  { render :text => "You're not the owner of this merge request", :status => :forbidden }
93         end
94         return
95       end
96     end
97   
98 end