Skip to content

Instantly share code, notes, and snippets.

@kent
Forked from wycats/easy_select_fields.rb
Created April 12, 2010 18:34
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 kent/363862 to your computer and use it in GitHub Desktop.
Save kent/363862 to your computer and use it in GitHub Desktop.
module EasySelectFields
def easy_select_fields(a,options)
type_method_name = "#{a}_types".to_sym
array = options.each_with_index.map {|o,i| [o, i += 1] }
class_eval "def self.#{a}_types() #{array.inspect} end"
end
end
class Person
extend EasySelectFields
easy_select_fields "status", ["Interested", "Not Interested", "Available"]
end
require "active_support/core_ext/kernel/singleton_class"
module EasySelectFields
def easy_select_fields(a,options)
type_method_name = "#{a}_types".to_sym
array = options.each_with_index.map {|o,i| [o, i] }
singleton_class.class_eval do
define_method("#{a}_types") { array }
end
end
end
class Person
extend EasySelectFields
easy_select_fields "status", ["Interested", "Not Interested", "Available"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment