2016-11-22 17:10:12 +08:00
|
|
|
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|
|
2021-07-23 17:56:28 +08:00
|
|
|
if args.blank? || args[:domain].blank?
|
2016-11-22 17:10:12 +08:00
|
|
|
puts 'Please add the email domain'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
domain = args[:domain]
|
|
|
|
domain = domain.strip.gsub(/\./, '\\.')
|
|
|
|
|
2024-01-08 19:01:06 +08:00
|
|
|
ActiveRecord::Migration.add_check_constraint(
|
|
|
|
:users,
|
|
|
|
"email ~* '^[A-Za-z0-9._%-+]+@#{domain}'",
|
|
|
|
name: 'email_must_be_company_email',
|
|
|
|
validate: false
|
2016-11-29 23:53:40 +08:00
|
|
|
)
|
2016-11-22 17:10:12 +08:00
|
|
|
puts "Created the following domain constraint: #{args[:domain]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Remove email domain constraint from the users table.'
|
|
|
|
task remove_domain: :environment do
|
2024-01-08 19:01:06 +08:00
|
|
|
ActiveRecord::Migration.remove_check_constraint :users, name: 'email_must_be_company_email'
|
2016-11-22 17:10:12 +08:00
|
|
|
puts 'Email constraint has been removed'
|
|
|
|
end
|
|
|
|
end
|