Class: Enumeration

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/enumeration.rb

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

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.

See doc/COPYRIGHT.rdoc for more details. ++

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject


33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/enumeration.rb', line 33

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?
    find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
  else
    # STI classes are
    find(:first, :conditions => { :is_default => true })
  end
end

.get_subclassesObject

Returns the Subclasses of Enumeration. Each Subclass needs to be required in development mode.

Note: subclasses is protected in ActiveRecord


91
92
93
# File 'app/models/enumeration.rb', line 91

def self.get_subclasses
  @@subclasses[Enumeration]
end

.overridding_change?(new, previous) ⇒ Boolean

Does the new Hash override the previous Enumeration?

Returns:

  • (Boolean)

96
97
98
99
100
101
102
# File 'app/models/enumeration.rb', line 96

def self.overridding_change?(new, previous)
  if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
    return false
  else
    return true
  end
end

.same_active_state?(new, previous) ⇒ Boolean

Are the new and previous fields equal?

Returns:

  • (Boolean)

116
117
118
119
# File 'app/models/enumeration.rb', line 116

def self.same_active_state?(new, previous)
  new = (new == "1" ? true : false)
  return new == previous
end

.same_custom_values?(new, previous) ⇒ Boolean

Does the new Hash have the same custom values as the previous Enumeration?

Returns:

  • (Boolean)

105
106
107
108
109
110
111
112
113
# File 'app/models/enumeration.rb', line 105

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

Instance Method Details

#<=>(enumeration) ⇒ Object


81
82
83
# File 'app/models/enumeration.rb', line 81

def <=>(enumeration)
  position <=> enumeration.position
end

#before_saveObject


50
51
52
53
54
# File 'app/models/enumeration.rb', line 50

def before_save
  if is_default? && is_default_changed?
    Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
  end
end

#destroy(reassign_to = nil) ⇒ Object

Destroy the enumeration If a enumeration is specified, objects are reassigned


74
75
76
77
78
79
# File 'app/models/enumeration.rb', line 74

def destroy(reassign_to = nil)
  if reassign_to && reassign_to.is_a?(Enumeration)
    self.transfer_relations(reassign_to)
  end
  destroy_without_reassign
end

#destroy_without_reassignObject


70
# File 'app/models/enumeration.rb', line 70

alias :destroy_without_reassign :destroy

#in_use?Boolean

Returns:

  • (Boolean)

61
62
63
# File 'app/models/enumeration.rb', line 61

def in_use?
  self.objects_count != 0
end

#is_override?Boolean

Is this enumeration overiding a system level enumeration?

Returns:

  • (Boolean)

66
67
68
# File 'app/models/enumeration.rb', line 66

def is_override?
  !self.parent.nil?
end

#objects_countObject

Overloaded on concrete classes


57
58
59
# File 'app/models/enumeration.rb', line 57

def objects_count
  0
end

#option_nameObject

Overloaded on concrete classes


46
47
48
# File 'app/models/enumeration.rb', line 46

def option_name
  nil
end

#to_sObject


85
# File 'app/models/enumeration.rb', line 85

def to_s; name end