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.



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

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 <<-EOS, __FILE__, __LINE__ + 1
    def #{name}
      read_enum(:#{name})
    end

    def #{name}=(value)
      write_enum(:#{name}, value)
    end

    def #{name}?
      query_enum(:#{name})
    end

    def #{name}_changed?
      enum_changed?(:#{name})
    end

    def #{name}_was
      enum_was(:#{name})
    end

    def #{name}_change
      enum_change(:#{name})
    end
  EOS

  reflection
end

#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

#expand_attribute_names_for_aggregates(attribute_names) ⇒ Object

Enables .where(name: value) for enums.



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

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.



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

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:



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

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

#inheritance_columnObject

:nodoc:



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

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

#inheritance_enumObject

:nodoc:



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

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.



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

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:



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

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