# Issue Extensions plugin for Redmine
# Copyright (C) 2010  Takashi Takebayashi
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
require_dependency 'issue'

module IssueExtensionsIssuePatch
  def self.included base
    base.extend ClassMethods
    base.send :include, InstanceMethods
    base.class_eval do
      alias_method_chain :validate, :issue_extensions
    end
  end

  module ClassMethods
  end

  module InstanceMethods
    def validate_with_issue_extensions
      validate_without_issue_extensions
      project = Project.find read_attribute :project_id
      unless project.module_enabled? :issue_extensions == nil
        tracker = Tracker.find :first, :conditions => ["name = (?)", 'バグ']
        issue_status = IssueStatus.find :first, :conditions => ["name = (?)", '終了']
        if tracker != nil && issue_status != nil
          if tracker.id == read_attribute(:tracker_id) && issue_status.id == read_attribute(:status_id)
            custom_field = CustomField.find :first, :conditions => ["type = (?) and name = (?)", 'IssueCustomField', '修正バージョン']
            if custom_field != nil
              custom_values.each {|c|
                if !c.custom_field_id.blank?
                  if c.custom_field_id == custom_field.id
                    errors.add_to_base custom_field.name + " を入力してください。" if c.value.blank?
                  end
                end
              }
            end
          end
        end
      end
    end
  end
end
Issue.send(:include, IssueExtensionsIssuePatch)
