Module: ActiveRecord::Acts::Enumerated::ClassMethods

Defined in:
lib/active_record/acts/enumerated.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enumeration_model_updates_permittedObject

Returns the value of attribute enumeration_model_updates_permitted.



33
34
35
# File 'lib/active_record/acts/enumerated.rb', line 33

def enumeration_model_updates_permitted
  @enumeration_model_updates_permitted
end

Instance Method Details

#[](arg) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_record/acts/enumerated.rb', line 43

def [](arg)
  case arg
  when Symbol
    rval = lookup_name(arg.id2name) and return rval
  when String
    rval = lookup_name(arg) and return rval
  when Fixnum
    rval = lookup_id(arg) and return rval
  when nil
    rval = nil
  else
    raise TypeError, "#{self.name}[]: argument should be a String, Symbol or Fixnum but got a: #{arg.class.name}"
  end
  self.send((read_inheritable_attribute(:acts_enumerated_on_lookup_failure) || :enforce_strict_literals), arg)
end

#allObject



35
36
37
38
39
40
41
# File 'lib/active_record/acts/enumerated.rb', line 35

def all
  return @all if @all
  @all = find(:all,
              :conditions => read_inheritable_attribute(:acts_enumerated_conditions),
              :order => read_inheritable_attribute(:acts_enumerated_order)
              ).collect{|val| val.freeze}.freeze
end

#include?(arg) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_record/acts/enumerated.rb', line 67

def include?(arg)
  case arg
  when Symbol
    return !lookup_name(arg.id2name).nil?
  when String
    return !lookup_name(arg).nil?
  when Fixnum
    return !lookup_id(arg).nil?
  when self
    possible_match = lookup_id(arg.id)
    return !possible_match.nil? && possible_match == arg
  end
  return false
end

#lookup_id(arg) ⇒ Object



59
60
61
# File 'lib/active_record/acts/enumerated.rb', line 59

def lookup_id(arg)
  all_by_id[arg]
end

#lookup_name(arg) ⇒ Object



63
64
65
# File 'lib/active_record/acts/enumerated.rb', line 63

def lookup_name(arg)
  all_by_name[arg]
end

#purge_enumerations_cacheObject

NOTE: purging the cache is sort of pointless because of the per-process rails model. By default this blows up noisily just in case you try to be more clever than rails allows. For those times (like in Migrations) when you really do want to alter the records you can silence the carping by setting enumeration_model_updates_permitted to true.



89
90
91
92
93
94
# File 'lib/active_record/acts/enumerated.rb', line 89

def purge_enumerations_cache
  unless self.enumeration_model_updates_permitted
    raise "#{self.name}: cache purging disabled for your protection"
  end
  @all = @all_by_name = @all_by_id = nil
end