forgeplus/app/forms/gitea/user_form.rb

38 lines
875 B
Ruby
Raw Normal View History

2020-03-09 00:40:16 +08:00
class Gitea::UserForm
include ActiveModel::Model
EMAIL_REGEX = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
include ActiveModel::Model
attr_accessor :username, :email, :password
validates :username, presence: true
validates :email, presence: true, format: { with: EMAIL_REGEX, multiline: true }
validates :password, presence: true
validate :check_username, :check_email
attr_reader :record
def persist
@record = id ? User.find(id) : User.new
if valid?
@record.attributes = attributes.except(:password_confirmation, :id)
@record.save!
true
else
false
end
end
private
def check_username
# errors.add(:login, :exist)
raise "#{username} 已使用." if User.exists?(login: username.strip)
end
def check_email
raise "#{email} 已使用." if User.exists?(mail: email.strip)
end
end