Project

General

Profile

Download (2.97 KB) Statistics
| Branch: | Tag: | Revision:
1
# Redmine Commit Relation Editor plugin for Redmine
2
# Copyright (C) 2011-2012  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
    options = {:controller => 'repositories', :action => 'revision', :id => @project, :rev => @rev}
41
    options[:repository_id] = params[:repository_id] if params[:repository_id].present?
42
    redirect_to url_for(options)
43
  end
44

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

    
63
    options = {:controller => 'repositories', :action => 'revision', :id => @project, :rev => @rev}
64
    options[:repository_id] = params[:repository_id] if params[:repository_id].present?
65
    redirect_to url_for(options)
66
  end
67
  
68
  private
69
  
70
  def find_changeset
71
    @changeset = Changeset.find(params[:changeset_id])
72
    @rev = @changeset.respond_to?(:identifier) ? @changeset.identifier : @changeset
73

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

    
89
end
    (1-1/1)