Created
August 5, 2015 15:19
-
-
Save neurodynamic/e91883852e17284f6fbf to your computer and use it in GitHub Desktop.
custom scoped attribute uniqueness validation method for Volt framework
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
def validate_uniqueness_scope(*attribute_array) | |
attributes_hash = Hash[attribute_array.map { |i| [ i, send(i)] }] | |
store.send(self.class.name.downcase.pluralize).where(attributes_hash).count.then do |count| | |
if count > 0 | |
{ self.class.name.downcase.to_sym => ["#{attributes_hash.values.join(', ')} already exists."] } | |
else | |
{} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment