Created
July 24, 2012 03:29
-
-
Save frsyuki/3167843 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
module UniqueConstraint | |
def self.included(model) | |
model.extend(ClassMethods) | |
end | |
module ClassMethods | |
def unique_constraint_violation_message | |
"#{name} must be unique" | |
end | |
def set_unique_constraint_violation_message(message) | |
define_attr_method :unique_constraint_violation_message, message | |
end | |
alias :unique_constraint_violation_message= :set_unique_constraint_violation_message | |
end | |
# override ActiveRecord::Base | |
def save!(*args, &block) | |
super(*args, &block) | |
rescue ActiveRecord::RecordNotUnique | |
raise $!.exception(self.class.unique_constraint_violation_message) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment