Created
December 30, 2010 23:49
-
-
Save aliang/760495 to your computer and use it in GitHub Desktop.
validate reserved names in Rails 3
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
# app/validators/reserved_name_validator.rb | |
class ReservedNameValidator < ActiveModel::EachValidator | |
RESERVED_NAMES = %w{ | |
about account add admin api | |
app apps archive archives auth | |
blog | |
config connect contact create | |
delete direct_messages downloads | |
edit email | |
faq favorites feed feeds follow followers following | |
help home | |
invitations invite | |
jobs | |
login log-in log_in logout log-out log_out logs | |
map maps | |
oauth oauth_clients openid | |
privacy | |
register remove replies rss | |
save search sessions settings | |
signup sign-up sign_up signin sign-in sign_in signout sign-out sign_out | |
sitemap ssl subscribe | |
terms test trends | |
unfollow unsubscribe url user | |
widget widgets | |
xfn xmpp | |
} | |
def validate_each(object, attribute, value) | |
if RESERVED_NAMES.include?(value) | |
object.errors[attribute] << (options[:message] || "is not a valid name") | |
end | |
end | |
end | |
# app/models/#{model}.rb | |
class User < ActiveRecord::Base | |
validates :username, :reserved_name => true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment