mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-31 03:59:51 +08:00
moved email constraint to database_helper
This commit is contained in:
parent
10d40a391a
commit
d75d58b159
2 changed files with 16 additions and 10 deletions
|
@ -54,4 +54,16 @@ module DatabaseHelper
|
|||
).getvalue(0, 0).to_i
|
||||
end
|
||||
|
||||
end
|
||||
# Adds email domain constraint to the users table.
|
||||
def add_email_constraint(domain)
|
||||
ActiveRecord::Base.connection.execute(
|
||||
"ALTER TABLE " \
|
||||
"users " \
|
||||
"DROP CONSTRAINT IF EXISTS email_must_be_company_email, " \
|
||||
"ADD CONSTRAINT " \
|
||||
"email_must_be_company_email " \
|
||||
"CHECK ( email ~* '^[A-Za-z0-9._%-]+@#{domain}' ) " \
|
||||
"not valid;"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,6 +2,8 @@ namespace :sign_up_constraint do
|
|||
desc 'Adds email domain constraint to the users table. '\
|
||||
'E.g: scinote.net'
|
||||
task :email_domain, [:domain] => :environment do |_, args|
|
||||
include DatabaseHelper
|
||||
|
||||
if args.blank? ||
|
||||
args.empty? ||
|
||||
args[:domain].blank?
|
||||
|
@ -12,15 +14,7 @@ namespace :sign_up_constraint do
|
|||
domain = args[:domain]
|
||||
domain = domain.strip.gsub(/\./, '\\.')
|
||||
|
||||
ActiveRecord::Base.connection.execute(
|
||||
"ALTER TABLE " \
|
||||
"users " \
|
||||
"DROP CONSTRAINT IF EXISTS email_must_be_company_email, " \
|
||||
"ADD CONSTRAINT " \
|
||||
"email_must_be_company_email " \
|
||||
"CHECK ( email ~* '^[A-Za-z0-9._%-]+@#{domain}' ) " \
|
||||
"not valid;"
|
||||
)
|
||||
add_email_constraint(domain)
|
||||
puts "Created the following domain constraint: #{args[:domain]}"
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue