Project

General

Profile

Download (2.83 KB) Statistics
| Branch: | Tag: | Revision:

root / app / controllers / commit_relation_editor_controller.rb @ 4:7d3a38ab714a

1
# Redmine Commit Relation Editor plugin for Redmine
2
# Copyright (C) 2011  Haruyuki Iida
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class CommitRelationEditorController < ApplicationController
19
  unloadable
20
  before_filter :find_project, :authorize, :find_changeset
21

    
22
  def new
23
    issue_id = params[:issue_id]
24
    issue = find_referenced_issue_by_id issue_id
25
    unless issue
26
      flash[:error] = l(:error_issue_not_found_in_project) + ":##{issue_id}"
27
    else
28
      if @changeset.issues.include? issue
29
        flash[:notice] = l(:error_issue_is_already_related, :issue_id => issue_id)
30
      else
31
        @changeset.issues << issue
32
        if (@changeset.save) 
33
          flash[:notice] = l(:notice_successful_update)
34
        else
35
          flash[:error] = @changeset.errors
36
        end
37
      end
38
    end
39

    
40
    redirect_to url_for(:controller => 'repositories', :action => 'revision', :id => @project, :rev => @rev)
41
  end
42

    
43
  def destroy
44
    issue_id = params[:issue_id]
45
    issue = Issue.find(:first, :conditions => ['id = ?', issue_id])
46
    unless issue
47
      flash[:error] = l(:error_issue_not_found_in_project) + ":##{issue_id}"
48
    else
49
      unless @changeset.issues.include? issue
50
        flash[:notice] = "Issue ##{issue_id} is not related."
51
      else
52
        @changeset.issues.delete(issue)
53
        if (@changeset.save) 
54
          flash[:notice] = l(:notice_successful_update)
55
        else
56
          flash[:error] = @changeset.errors
57
        end
58
      end
59
    end
60

    
61
    redirect_to url_for(:controller => 'repositories', :action => 'revision', :id => @project, :rev => @rev)
62
  end
63
  
64
  private
65
  
66
  def find_changeset
67
    @changeset = Changeset.find(params[:changeset_id])
68
    @rev = @changeset.respond_to?(:identifier) ? @changeset.identifier : @changeset
69

    
70
  end
71
  
72
  def find_referenced_issue_by_id(id)
73
    return nil if id.blank?
74
    issue = Issue.find_by_id(id.to_i, :include => :project)
75
    if issue
76
      unless issue.project &&
77
                (@project == issue.project || @project.is_ancestor_of?(issue.project) ||
78
                 @project.is_descendant_of?(issue.project))
79
        issue = nil
80
      end
81
    end
82
    issue
83
  end
84

    
85
end
    (1-1/1)