To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / enumeration.rb @ 1532:a0460a3d154f
History | View | Annotate | Download (3.99 KB)
| 1 | 909:cbb26bc654de | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1494:e248c7af89ec | Chris | # Copyright (C) 2006-2014 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 1115:433d4f72a19b | Chris | include Redmine::SubclassFactory |
| 20 | |||
| 21 | 0:513646585e45 | Chris | default_scope :order => "#{Enumeration.table_name}.position ASC" |
| 22 | 909:cbb26bc654de | Chris | |
| 23 | 0:513646585e45 | Chris | belongs_to :project
|
| 24 | 909:cbb26bc654de | Chris | |
| 25 | 0:513646585e45 | Chris | acts_as_list :scope => 'type = \'#{type}\'' |
| 26 | acts_as_customizable |
||
| 27 | 1464:261b3d9a4903 | Chris | acts_as_tree :order => "#{Enumeration.table_name}.position ASC" |
| 28 | 0:513646585e45 | Chris | |
| 29 | before_destroy :check_integrity
|
||
| 30 | 909:cbb26bc654de | Chris | before_save :check_default
|
| 31 | |||
| 32 | 1115:433d4f72a19b | Chris | attr_protected :type
|
| 33 | |||
| 34 | 0:513646585e45 | Chris | validates_presence_of :name
|
| 35 | validates_uniqueness_of :name, :scope => [:type, :project_id] |
||
| 36 | validates_length_of :name, :maximum => 30 |
||
| 37 | |||
| 38 | 1464:261b3d9a4903 | Chris | scope :shared, lambda { where(:project_id => nil) } |
| 39 | scope :sorted, lambda { order("#{table_name}.position ASC") } |
||
| 40 | scope :active, lambda { where(:active => true) } |
||
| 41 | scope :system, lambda { where(:project_id => nil) } |
||
| 42 | 1115:433d4f72a19b | Chris | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} |
| 43 | 0:513646585e45 | Chris | |
| 44 | def self.default |
||
| 45 | # Creates a fake default scope so Enumeration.default will check
|
||
| 46 | # it's type. STI subclasses will automatically add their own
|
||
| 47 | # types to the finder.
|
||
| 48 | if self.descends_from_active_record? |
||
| 49 | 1115:433d4f72a19b | Chris | where(:is_default => true, :type => 'Enumeration').first |
| 50 | 0:513646585e45 | Chris | else
|
| 51 | # STI classes are
|
||
| 52 | 1115:433d4f72a19b | Chris | where(:is_default => true).first |
| 53 | 0:513646585e45 | Chris | end
|
| 54 | end
|
||
| 55 | 909:cbb26bc654de | Chris | |
| 56 | 0:513646585e45 | Chris | # Overloaded on concrete classes
|
| 57 | def option_name |
||
| 58 | nil
|
||
| 59 | end
|
||
| 60 | |||
| 61 | 909:cbb26bc654de | Chris | def check_default |
| 62 | 0:513646585e45 | Chris | if is_default? && is_default_changed?
|
| 63 | 1517:dffacf8a6908 | Chris | Enumeration.where({:type => type}).update_all({:is_default => false}) |
| 64 | 0:513646585e45 | Chris | end
|
| 65 | end
|
||
| 66 | 909:cbb26bc654de | Chris | |
| 67 | 0:513646585e45 | Chris | # Overloaded on concrete classes
|
| 68 | def objects_count |
||
| 69 | 0
|
||
| 70 | end
|
||
| 71 | 909:cbb26bc654de | Chris | |
| 72 | 0:513646585e45 | Chris | def in_use? |
| 73 | self.objects_count != 0 |
||
| 74 | end
|
||
| 75 | |||
| 76 | # Is this enumeration overiding a system level enumeration?
|
||
| 77 | def is_override? |
||
| 78 | !self.parent.nil?
|
||
| 79 | end
|
||
| 80 | 909:cbb26bc654de | Chris | |
| 81 | 0:513646585e45 | Chris | alias :destroy_without_reassign :destroy |
| 82 | 909:cbb26bc654de | Chris | |
| 83 | 0:513646585e45 | Chris | # Destroy the enumeration
|
| 84 | # If a enumeration is specified, objects are reassigned
|
||
| 85 | def destroy(reassign_to = nil) |
||
| 86 | if reassign_to && reassign_to.is_a?(Enumeration) |
||
| 87 | self.transfer_relations(reassign_to)
|
||
| 88 | end
|
||
| 89 | destroy_without_reassign |
||
| 90 | end
|
||
| 91 | 909:cbb26bc654de | Chris | |
| 92 | 0:513646585e45 | Chris | def <=>(enumeration) |
| 93 | position <=> enumeration.position |
||
| 94 | end
|
||
| 95 | 909:cbb26bc654de | Chris | |
| 96 | 0:513646585e45 | Chris | def to_s; name end |
| 97 | |||
| 98 | # Returns the Subclasses of Enumeration. Each Subclass needs to be
|
||
| 99 | # required in development mode.
|
||
| 100 | #
|
||
| 101 | # Note: subclasses is protected in ActiveRecord
|
||
| 102 | def self.get_subclasses |
||
| 103 | 1115:433d4f72a19b | Chris | subclasses |
| 104 | 0:513646585e45 | Chris | end
|
| 105 | |||
| 106 | # Does the +new+ Hash override the previous Enumeration?
|
||
| 107 | def self.overridding_change?(new, previous) |
||
| 108 | if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous) |
||
| 109 | return false |
||
| 110 | else
|
||
| 111 | return true |
||
| 112 | end
|
||
| 113 | end
|
||
| 114 | |||
| 115 | # Does the +new+ Hash have the same custom values as the previous Enumeration?
|
||
| 116 | def self.same_custom_values?(new, previous) |
||
| 117 | previous.custom_field_values.each do |custom_value|
|
||
| 118 | if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s] |
||
| 119 | return false |
||
| 120 | end
|
||
| 121 | end
|
||
| 122 | |||
| 123 | return true |
||
| 124 | end
|
||
| 125 | 909:cbb26bc654de | Chris | |
| 126 | 0:513646585e45 | Chris | # Are the new and previous fields equal?
|
| 127 | def self.same_active_state?(new, previous) |
||
| 128 | new = (new == "1" ? true : false) |
||
| 129 | return new == previous
|
||
| 130 | end
|
||
| 131 | 909:cbb26bc654de | Chris | |
| 132 | 0:513646585e45 | Chris | private |
| 133 | def check_integrity |
||
| 134 | raise "Can't delete enumeration" if self.in_use? |
||
| 135 | end
|
||
| 136 | |||
| 137 | end
|
||
| 138 | |||
| 139 | # Force load the subclasses in development mode
|
||
| 140 | require_dependency 'time_entry_activity'
|
||
| 141 | require_dependency 'document_category'
|
||
| 142 | require_dependency 'issue_priority' |