enumeration.rb
168 lines
| 4.8 KiB
| text/x-ruby
|
RubyLexer
|
r6381 | # Redmine - project management software | ||
|
r14856 | # Copyright (C) 2006-2016 Jean-Philippe Lang | ||
|
r330 | # | ||
# 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. | ||||
|
r6381 | # | ||
|
r330 | # 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. | ||||
|
r6381 | # | ||
|
r330 | # 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. | ||||
class Enumeration < ActiveRecord::Base | ||||
|
r8069 | include Redmine::SubclassFactory | ||
|
r13145 | default_scope lambda {order(:position)} | ||
|
r6381 | |||
|
r2833 | belongs_to :project | ||
|
r6381 | |||
|
r14245 | acts_as_list :scope => 'type = \'#{type}\' AND #{parent_id ? "parent_id = #{parent_id}" : "parent_id IS NULL"}' | ||
|
r2831 | acts_as_customizable | ||
|
r13146 | acts_as_tree | ||
|
r789 | |||
|
r330 | before_destroy :check_integrity | ||
|
r7307 | before_save :check_default | ||
|
r6381 | |||
|
r8069 | attr_protected :type | ||
|
r2677 | validates_presence_of :name | ||
|
r2835 | validates_uniqueness_of :name, :scope => [:type, :project_id] | ||
|
r590 | validates_length_of :name, :maximum => 30 | ||
|
r789 | |||
|
r10722 | scope :shared, lambda { where(:project_id => nil) } | ||
|
r13144 | scope :sorted, lambda { order(:position) } | ||
|
r10722 | scope :active, lambda { where(:active => true) } | ||
|
r11532 | scope :system, lambda { where(:project_id => nil) } | ||
|
r9530 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} | ||
|
r2832 | |||
|
r2677 | def self.default | ||
# Creates a fake default scope so Enumeration.default will check | ||||
# it's type. STI subclasses will automatically add their own | ||||
# types to the finder. | ||||
if self.descends_from_active_record? | ||||
|
r9530 | where(:is_default => true, :type => 'Enumeration').first | ||
|
r2677 | else | ||
# STI classes are | ||||
|
r9530 | where(:is_default => true).first | ||
|
r2677 | end | ||
end | ||||
|
r6381 | |||
|
r2677 | # Overloaded on concrete classes | ||
|
r330 | def option_name | ||
|
r2677 | nil | ||
end | ||||
|
r792 | |||
|
r7307 | def check_default | ||
|
r2121 | if is_default? && is_default_changed? | ||
|
r12235 | Enumeration.where({:type => type}).update_all({:is_default => false}) | ||
|
r2121 | end | ||
|
r792 | end | ||
|
r6381 | |||
|
r2677 | # Overloaded on concrete classes | ||
|
r1544 | def objects_count | ||
|
r2677 | 0 | ||
|
r1544 | end | ||
|
r6381 | |||
|
r1544 | def in_use? | ||
self.objects_count != 0 | ||||
end | ||||
|
r2833 | |||
|
r12797 | # Is this enumeration overriding a system level enumeration? | ||
|
r2833 | def is_override? | ||
!self.parent.nil? | ||||
end | ||||
|
r6381 | |||
|
r1544 | alias :destroy_without_reassign :destroy | ||
|
r6381 | |||
|
r1544 | # Destroy the enumeration | ||
# If a enumeration is specified, objects are reassigned | ||||
def destroy(reassign_to = nil) | ||||
if reassign_to && reassign_to.is_a?(Enumeration) | ||||
|
r2677 | self.transfer_relations(reassign_to) | ||
|
r1544 | end | ||
destroy_without_reassign | ||||
end | ||||
|
r6381 | |||
|
r866 | def <=>(enumeration) | ||
position <=> enumeration.position | ||||
end | ||||
|
r6381 | |||
|
r771 | def to_s; name end | ||
|
r2677 | |||
# Returns the Subclasses of Enumeration. Each Subclass needs to be | ||||
# required in development mode. | ||||
# | ||||
# Note: subclasses is protected in ActiveRecord | ||||
def self.get_subclasses | ||||
|
r8857 | subclasses | ||
|
r2677 | end | ||
|
r2835 | |||
# Does the +new+ Hash override the previous Enumeration? | ||||
|
r12781 | def self.overriding_change?(new, previous) | ||
|
r2835 | if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous) | ||
return false | ||||
else | ||||
return true | ||||
end | ||||
end | ||||
# Does the +new+ Hash have the same custom values as the previous Enumeration? | ||||
def self.same_custom_values?(new, previous) | ||||
previous.custom_field_values.each do |custom_value| | ||||
if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s] | ||||
return false | ||||
end | ||||
end | ||||
return true | ||||
end | ||||
|
r6381 | |||
|
r2835 | # Are the new and previous fields equal? | ||
def self.same_active_state?(new, previous) | ||||
new = (new == "1" ? true : false) | ||||
return new == previous | ||||
end | ||||
|
r6381 | |||
|
r14245 | # Overrides acts_as_list reset_positions_in_list so that enumeration overrides | ||
# get the same position as the overriden enumeration | ||||
def reset_positions_in_list | ||||
|
r14252 | acts_as_list_class.where(scope_condition).reorder("#{position_column} ASC, id ASC").each_with_index do |item, i| | ||
acts_as_list_class.where("id = :id OR parent_id = :id", :id => item.id). | ||||
update_all({position_column => (i + 1)}) | ||||
|
r14251 | end | ||
|
r14245 | end | ||
|
r330 | private | ||
def check_integrity | ||||
|
r13399 | raise "Cannot delete enumeration" if self.in_use? | ||
|
r2 | end | ||
|
r2677 | |||
|
r14245 | # Overrides acts_as_list add_to_list_bottom so that enumeration overrides | ||
# get the same position as the overriden enumeration | ||||
def add_to_list_bottom | ||||
if parent | ||||
self[position_column] = parent.position | ||||
else | ||||
super | ||||
end | ||||
end | ||||
# Overrides acts_as_list remove_from_list so that enumeration overrides | ||||
# get the same position as the overriden enumeration | ||||
def remove_from_list | ||||
if parent_id.blank? | ||||
super | ||||
end | ||||
end | ||||
|
r2 | end | ||
|
r2677 | |||
# Force load the subclasses in development mode | ||||
require_dependency 'time_entry_activity' | ||||
require_dependency 'document_category' | ||||
require_dependency 'issue_priority' | ||||