Module: Tolliver::Utils::Enum::ClassMethods

Defined in:
lib/tolliver/utils/enum.rb

Instance Method Summary collapse

Instance Method Details

#enum_column(new_column, spec, options = {}) ⇒ Object

Add new enum column



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/tolliver/utils/enum.rb', line 22

def enum_column(new_column, spec, options = {})

  # Prepare internal structure
  if @enums.nil?
    @enums = {}
  end
  @enums[new_column] = {}

  # Fill out internal structure
  spec.each do |item|

    # Value check
    if item.is_a?(OpenStruct)
      item = item.to_h
    elsif item.is_a? Hash
      # OK
    else
      item = {value: item}
    end
    unless item[:value]
      raise "Enum definition cannot be empty."
    end

    # Identify special type
    item[:special_type] = :integer if item[:value].is_a?(Integer)

    # Retype to string
    item[:value] = item[:value].to_s

    # Label
    unless item[:label]
      item[:label] = I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{new_column.to_s}_values.#{item[:special_type] ? item[:special_type].to_s + "_" : ""}#{item[:value]}")
    end

    # Other attributes
    if options[:attributes]
      options[:attributes].each do |attribute|
        singular_attribute_name = attribute.to_s.singularize
        plural_attribute_name = attribute.to_s.pluralize
        if item[singular_attribute_name.to_sym].nil?
          item[singular_attribute_name.to_sym] = I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{new_column.to_s}_#{plural_attribute_name}.#{item[:special_type] ? item[:special_type].to_s + "_" : ""}#{item[:value]}")
        end
      end
    end

    # Store
    @enums[new_column][item[:value].to_s] = OpenStruct.new(item)
  end

  # Obj method
  define_method((new_column.to_s + "_obj").to_sym) do
    column = new_column

    # Get map Value => Object
    enums = self.class.enums[column]

    # Get possible special type
    special_type = enums.values.first.special_type if enums.first && enums.values.first.special_type

    # Get value and modify it according to special type
    value = self.send(column)
    value = value.to_i if special_type == :integer

    return self.class.enums[column][value.to_s]
  end

  # All method
  define_singleton_method(("available_" + new_column.to_s.pluralize).to_sym) do
    column = new_column
    return @enums[column].values
  end

  # All values method
  define_singleton_method(("available_" + new_column.to_s + "_values").to_sym) do
    column = new_column
    return @enums[column].values.map { |o| o.value.to_sym }
  end

  # Label method
  define_singleton_method((new_column.to_s + "_label").to_sym) do |value|
    column = new_column
    return @enums[column].values.select { |o| o.value.to_s == value.to_s }.map { |o| o.label }.first
  end

  # Default value
  if options[:default]
    before_validation do
      column = new_column
      default = options[:default]
      if self.send(column).nil?
        self.send(column.to_s + "=", default.to_s)
      end
    end
  end

end

#enumsObject

Get all defined enums



120
121
122
# File 'lib/tolliver/utils/enum.rb', line 120

def enums
  @enums
end

#has_enum?(column) ⇒ Boolean

Check if given column is enum defined on this model

Returns:

  • (Boolean)


125
126
127
# File 'lib/tolliver/utils/enum.rb', line 125

def has_enum?(column)
  !@enums.nil? && !@enums[column].nil?
end