To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / enumeration.rb @ 437:102056ec2de9

History | View | Annotate | Download (3.77 KB)

1
# redMine - project management software
2
# Copyright (C) 2006  Jean-Philippe Lang
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 Enumeration < ActiveRecord::Base
19
  default_scope :order => "#{Enumeration.table_name}.position ASC"
20
  
21
  belongs_to :project
22
  
23
  acts_as_list :scope => 'type = \'#{type}\''
24
  acts_as_customizable
25
  acts_as_tree :order => 'position ASC'
26

    
27
  before_destroy :check_integrity
28
  
29
  validates_presence_of :name
30
  validates_uniqueness_of :name, :scope => [:type, :project_id]
31
  validates_length_of :name, :maximum => 30
32

    
33
  named_scope :shared, :conditions => { :project_id => nil }
34
  named_scope :active, :conditions => { :active => true }
35

    
36
  def self.default
37
    # Creates a fake default scope so Enumeration.default will check
38
    # it's type.  STI subclasses will automatically add their own
39
    # types to the finder.
40
    if self.descends_from_active_record?
41
      find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
42
    else
43
      # STI classes are
44
      find(:first, :conditions => { :is_default => true })
45
    end
46
  end
47
  
48
  # Overloaded on concrete classes
49
  def option_name
50
    nil
51
  end
52

    
53
  def before_save
54
    if is_default? && is_default_changed?
55
      Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
56
    end
57
  end
58
  
59
  # Overloaded on concrete classes
60
  def objects_count
61
    0
62
  end
63
  
64
  def in_use?
65
    self.objects_count != 0
66
  end
67

    
68
  # Is this enumeration overiding a system level enumeration?
69
  def is_override?
70
    !self.parent.nil?
71
  end
72
  
73
  alias :destroy_without_reassign :destroy
74
  
75
  # Destroy the enumeration
76
  # If a enumeration is specified, objects are reassigned
77
  def destroy(reassign_to = nil)
78
    if reassign_to && reassign_to.is_a?(Enumeration)
79
      self.transfer_relations(reassign_to)
80
    end
81
    destroy_without_reassign
82
  end
83
  
84
  def <=>(enumeration)
85
    position <=> enumeration.position
86
  end
87
  
88
  def to_s; name end
89

    
90
  # Returns the Subclasses of Enumeration.  Each Subclass needs to be
91
  # required in development mode.
92
  #
93
  # Note: subclasses is protected in ActiveRecord
94
  def self.get_subclasses
95
    @@subclasses[Enumeration]
96
  end
97

    
98
  # Does the +new+ Hash override the previous Enumeration?
99
  def self.overridding_change?(new, previous)
100
    if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
101
      return false
102
    else
103
      return true
104
    end
105
  end
106

    
107
  # Does the +new+ Hash have the same custom values as the previous Enumeration?
108
  def self.same_custom_values?(new, previous)
109
    previous.custom_field_values.each do |custom_value|
110
      if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
111
        return false
112
      end
113
    end
114

    
115
    return true
116
  end
117
  
118
  # Are the new and previous fields equal?
119
  def self.same_active_state?(new, previous)
120
    new = (new == "1" ? true : false)
121
    return new == previous
122
  end
123
  
124
private
125
  def check_integrity
126
    raise "Can't delete enumeration" if self.in_use?
127
  end
128

    
129
end
130

    
131
# Force load the subclasses in development mode
132
require_dependency 'time_entry_activity'
133
require_dependency 'document_category'
134
require_dependency 'issue_priority'