moved email constraint to database_helper

This commit is contained in:
zmagod 2016-11-23 09:13:09 +01:00
parent 10d40a391a
commit d75d58b159
2 changed files with 16 additions and 10 deletions

View file

@ -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

View file

@ -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