mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-21 22:33:13 +08:00
adds rake task that creates a sign up email domain constraint
This commit is contained in:
parent
7deb80bb06
commit
10d40a391a
1 changed files with 36 additions and 0 deletions
36
lib/tasks/sign_up_constraint.rake
Normal file
36
lib/tasks/sign_up_constraint.rake
Normal file
|
@ -0,0 +1,36 @@
|
|||
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|
|
||||
if args.blank? ||
|
||||
args.empty? ||
|
||||
args[:domain].blank?
|
||||
puts 'Please add the email domain'
|
||||
return
|
||||
end
|
||||
|
||||
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;"
|
||||
)
|
||||
puts "Created the following domain constraint: #{args[:domain]}"
|
||||
end
|
||||
|
||||
desc 'Remove email domain constraint from the users table.'
|
||||
task remove_domain: :environment do
|
||||
ActiveRecord::Base.connection.execute(
|
||||
'ALTER TABLE ' \
|
||||
'users ' \
|
||||
'DROP CONSTRAINT IF EXISTS email_must_be_company_email; '
|
||||
)
|
||||
puts 'Email constraint has been removed'
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue