Created
July 8, 2011 12:18
-
-
Save fermion/1071706 to your computer and use it in GitHub Desktop.
required MM Boolean field w/ :default => false solution
This file contains 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 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