Module: Enum::ModelSupport

Defined in:
lib/iron/enum/model_support.rb

Overview

Provides ActiveRecord-specific functionality for enum attributes

Instance Method Summary collapse

Instance Method Details

#attribute_for_inspect(name) ⇒ Object

Provide pretty output of enumerated values in console, overrides Rails’ internal method for doing the same for AR models generally.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/iron/enum/model_support.rb', line 8

def attribute_for_inspect(name)
  # Convert to symbol and get our current value
  name = name.to_sym
  val = self.send(name)

  # Check to see if we're non-nil and that this attribute is an enumeration attribute
  if val && self.class.enum_attr?(name)
    # Get the enum for this attribute
    enum = self.class.enum_for_attr(name)
    # Get the key version of the value
    key = enum.key(val)
    # Generate our pretty version in Enum::VALUE_KEY format
    "#{enum.to_s}::#{key.to_s.upcase}"

  else
    # Not an enum attr or nil - fall back on standard implementation
    super
  end
end