Module: EnumTable::Record::ClassMethods

Defined in:
lib/enum_table/record.rb

Instance Method Summary collapse

Instance Method Details

#builtin_inheritance_columnObject

Enables enums for STI types.



83
84
85
86
87
88
89
90
# File 'lib/enum_table/record.rb', line 83

def builtin_inheritance_column  # :nodoc:
  # Can this be made less brittle?
  if self == ActiveRecord::Base
    'type'
  else
    (@builtin_inheritance_column ||= nil) || superclass.builtin_inheritance_column
  end
end

#enum(name, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/enum_table/record.rb', line 11

def enum(name, options={})
  name = name.to_sym
  reflection = enums[name] ? enums[name].dup : Reflection.new(name)
  [:type, :id_name].each do |key|
    value = options[key] and
      reflection.send "#{key}=", value
  end
  reflection.to_populate do |ref|
    enum_map(name, options).each do |value, id|
      ref.add_value id, value
    end
  end
  self.enums = enums.merge(name => reflection, name.to_s => reflection)

  class_eval "    def \#{name}\n      read_enum(:\#{name})\n    end\n\n    def \#{name}=(value)\n      write_enum(:\#{name}, value)\n    end\n\n    def \#{name}?\n      query_enum(:\#{name})\n    end\n\n    def \#{name}_changed?\n      enum_changed?(:\#{name})\n    end\n\n    def \#{name}_was\n      enum_was(:\#{name})\n    end\n\n    def \#{name}_change\n      enum_change(:\#{name})\n    end\n  EOS\n\n  reflection\nend\n", __FILE__, __LINE__ + 1

#enum_id(name, value) ⇒ Object



76
77
78
79
80
# File 'lib/enum_table/record.rb', line 76

def enum_id(name, value)
  reflection = enums[name] or
    raise ArgumentError, "no such enum: #{name}"
  reflection.id(value)
end

#enum_map(name, options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/enum_table/record.rb', line 54

def enum_map(name, options)
  case (table = options[:table])
  when Hash
    table
  when Array
    map = {}
    table.each_with_index { |element, i| map[element] = i + 1 }
    map
  else
    map = {}
    table_name = table || "#{self.table_name.singularize}_#{name.to_s.pluralize}"
    connection.execute("SELECT id, value FROM #{connection.quote_table_name table_name}").each do |row|
      map[row[1]] = row[0]
    end
    map
  end
end

#expand_attribute_names_for_aggregates(attribute_names) ⇒ Object

Enables .where(name: value) for enums.



130
131
132
133
134
135
136
137
# File 'lib/enum_table/record.rb', line 130

def expand_attribute_names_for_aggregates(attribute_names)  # :nodoc:
  attribute_names = super
  enums.each do |name, reflection|
    index = attribute_names.index(name) and
      attribute_names[index] = reflection.id_name
  end
  attribute_names
end

#expand_hash_conditions_for_aggregates(attrs) ⇒ Object

Enables .find_by_name(value) for enums.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/enum_table/record.rb', line 109

def expand_hash_conditions_for_aggregates(attrs)  # :nodoc:
  conditions = super
  enums.each do |name, reflection|
    if conditions.key?(name)
      value = conditions.delete(name)
    elsif conditions.key?((string_name = name.to_s))
      value = conditions.delete(string_name)
    else
      next
    end
    if value.is_a?(Array)
      id = value.map { |el| reflection.id(el) }
    else
      id = reflection.id(value)
    end
    conditions[reflection.id_name] = id
  end
  conditions
end

#find_sti_class(type_name) ⇒ Object

:nodoc:



104
105
106
# File 'lib/enum_table/record.rb', line 104

def find_sti_class(type_name)  # :nodoc:
  (reflection = inheritance_enum) ? super(reflection.value(type_name).to_s) : super
end

#inheritance_columnObject

:nodoc:



96
97
98
# File 'lib/enum_table/record.rb', line 96

def inheritance_column  # :nodoc:
  (reflection = inheritance_enum) ? reflection.id_name.to_s : super
end

#inheritance_enumObject

:nodoc:



92
93
94
# File 'lib/enum_table/record.rb', line 92

def inheritance_enum  # :nodoc:
  @inheritance_enum ||= enums[builtin_inheritance_column.to_sym]
end

#initialize_attributes(attributes) ⇒ Object

Enables state_machine to set initial values for states. Ick.



140
141
142
143
144
145
146
147
148
# File 'lib/enum_table/record.rb', line 140

def initialize_attributes(attributes, *)  # :nodoc:
  attributes = super
  enums.each do |name, reflection|
    if (value = attributes.delete(reflection.name.to_s))
      attributes[reflection.id_name.to_s] ||= reflection.id(value)
    end
  end
  attributes
end

#reflect_on_enum(name) ⇒ Object



72
73
74
# File 'lib/enum_table/record.rb', line 72

def reflect_on_enum(name)
  enums[name]
end

#sti_nameObject

:nodoc:



100
101
102
# File 'lib/enum_table/record.rb', line 100

def sti_name  # :nodoc:
  (reflection = inheritance_enum) ? reflection.id(super) : super
end