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