Skip to content

Instantly share code, notes, and snippets.

@aliang
Created December 30, 2010 23:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aliang/760495 to your computer and use it in GitHub Desktop.
Save aliang/760495 to your computer and use it in GitHub Desktop.
validate reserved names in Rails 3
# 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