From d75d58b1596f8e0a0ec5bf6155ed7dcb7b8515d4 Mon Sep 17 00:00:00 2001 From: zmagod Date: Wed, 23 Nov 2016 09:13:09 +0100 Subject: [PATCH] moved email constraint to database_helper --- app/helpers/database_helper.rb | 14 +++++++++++++- lib/tasks/sign_up_constraint.rake | 12 +++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/helpers/database_helper.rb b/app/helpers/database_helper.rb index 358239079..e65ebe285 100644 --- a/app/helpers/database_helper.rb +++ b/app/helpers/database_helper.rb @@ -54,4 +54,16 @@ module DatabaseHelper ).getvalue(0, 0).to_i end -end \ No newline at end of file + # 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 diff --git a/lib/tasks/sign_up_constraint.rake b/lib/tasks/sign_up_constraint.rake index 3dfbbd4e9..d8953e662 100644 --- a/lib/tasks/sign_up_constraint.rake +++ b/lib/tasks/sign_up_constraint.rake @@ -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