Skip to content

Instantly share code, notes, and snippets.

@juanhiplogiq
Created February 20, 2015 02:15
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/3e241f824c5ba30f6ec2 to your computer and use it in GitHub Desktop.
Save juanhiplogiq/3e241f824c5ba30f6ec2 to your computer and use it in GitHub Desktop.
#Dean's code
ACTIVE_RECORD_ENUMS = %w{
AccountType
}
ACTIVE_RECORD_ENUMS.each do |eclass|
eval "class #{eclass} < ActiveRecord::Base; end"
eclass.constantize.class_eval do
class << self
def id_for(name)
ids[name.to_s.strip.humanize.downcase]
end
def value_for(id)
values[id.to_i]
end
def values
@values ||= find(:all).inject({}) {|h,m| h[m.send(primary_key)] = m.name;h}
end
def 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