Created
February 22, 2011 04:50
-
-
Save moro/838242 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TypeColumn | |
def type_column(column_name, types = nil) | |
types ||= const_get(column_name.to_s.pluralize.upcase) | |
raise if types.blank? | |
validates_inclusion_of column_name, :in => types | |
types.each do |t| | |
scope t, where(column_name => t) | |
class_eval(%[def #{t}?; #{column_name} == #{t.dump}; end], __FILE__, __LINE__) | |
end | |
end | |
end | |
class Note < ActiveRecord::Base | |
extend TypeColumn | |
CATEGORIES = %w[foo bar piyo] | |
type_column :category | |
end | |
Note.foo.all?{|n| n.foo? } # => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment