Skip to content

Instantly share code, notes, and snippets.

@juanhiplogiq
Created February 19, 2015 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanhiplogiq/6825c8f08823517e9985 to your computer and use it in GitHub Desktop.
Save juanhiplogiq/6825c8f08823517e9985 to your computer and use it in GitHub Desktop.
ACTIVE_RECORD_ENUMS = %w{
AccountType
}
ACTIVE_RECORD_ENUMS.each do |eclass|
eclass.constantize = Class.new(ActiveRecord::Base) do
def self.id_for(name)
ids[name.to_s.strip.humanize.downcase]
end
def self.value_for(id)
values[id.to_i]
end
def self.values
@values ||= find(:all).inject({}) {|h,m| h[m.send(primary_key)] = m.name;h}
end
def self.ids
@ids ||= self.values.inject({}) {|h,(k,v)| h[v.downcase] = k;h}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment