Created
August 25, 2011 13:05
-
-
Save asux/1170601 to your computer and use it in GitHub Desktop.
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
class ExeclyOneValidator < ActiveModel::Validator | |
def validate(record) | |
raise ArgumentError, ":fields option doesn't supplied" unless options.has_key?(:fields) | |
if options[:fields].select { |f| record.read_attribute(f.to_s).present? }.size != 1 | |
_fields = options[:fields].map(&:to_s).map(&:titleize).join(", ") | |
record.errors[:base] << "execly one of #{_fields} must be present" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment