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.



90
91
92
93
94
95
96
97
# File 'lib/enum_table/record.rb', line 90

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



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

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
71
# 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}"
    return {} if EnumTable.missing_tables_allowed? && !connection.tables.include?(table_name)
    connection.execute("SELECT id, value FROM #{connection.quote_table_name table_name}").each do |row|
      map[row[1]] = row[0]
    end
    map
  end
end

#enum_value(name, id) ⇒ Object



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

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

#expand_attribute_names_for_aggregates(attribute_names) ⇒ Object

Enables .find_by_name(value) for enums.



138
139
140
141
142
143
144
145
# File 'lib/enum_table/record.rb', line 138

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 .where(name: value) for enums.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/enum_table/record.rb', line 116

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:



111
112
113
# File 'lib/enum_table/record.rb', line 111

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

#inheritance_columnObject

:nodoc:



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

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

#inheritance_enumObject

:nodoc:



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

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.



149
150
151
152
153
154
155
156
157
# File 'lib/enum_table/record.rb', line 149

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



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

def reflect_on_enum(name)
  enums[name]
end

#sti_nameObject

:nodoc:



107
108
109
# File 'lib/enum_table/record.rb', line 107

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