Skip to content

Instantly share code, notes, and snippets.

@fermion
Created July 8, 2011 12:18
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 fermion/1071706 to your computer and use it in GitHub Desktop.
Save fermion/1071706 to your computer and use it in GitHub Desktop.
required MM Boolean field w/ :default => false solution
module ValidateBooleanFields
extend ActiveSupport::Concern
included do
class_eval(<<-EOS, __FILE__, __LINE__)
cattr_accessor :boolean_fields
EOS
validate :validate_boolean_fields
def validate_boolean_fields
self.class.class_variable_get(:@@boolean_fields).each do |boolean_field|
errors[boolean_field] << "can't be blank" if self.send(boolean_field).nil?
end
end
end
module ClassMethods
def validates_presence_of_boolean_fields(*fields)
class_variable_set(:@@boolean_fields, fields)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment