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