From f8b73074413d6a29932096ca0f6f28dab3b8e1ac Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Fri, 13 May 2011 12:14:55 -0700 Subject: [PATCH] Downcase emails upon inviting --- app/controllers/users_controller.rb | 2 +- app/models/invitation.rb | 1 + app/models/user.rb | 2 +- config/deploy.rb | 1 + ...20110512220310_downcase_usernames_again.rb | 10 ------- ...0513175000_eliminate_stray_user_records.rb | 29 +++++++++++++++++++ spec/models/invitation_spec.rb | 2 +- 7 files changed, 34 insertions(+), 13 deletions(-) delete mode 100644 db/migrate/20110512220310_downcase_usernames_again.rb create mode 100644 db/migrate/20110513175000_eliminate_stray_user_records.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 431259e51..a0e0f2f51 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -32,7 +32,7 @@ class UsersController < ApplicationController if u[:email_preferences] @user.update_user_preferences(u[:email_preferences]) flash[:notice] = I18n.t 'users.update.email_notifications_changed' - # change passowrd + # change password elsif u[:current_password] && u[:password] && u[:password_confirmation] if @user.update_with_password(u) flash[:notice] = I18n.t 'users.update.password_changed' diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 0ac394dba..a0527977f 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -11,6 +11,7 @@ class Invitation < ActiveRecord::Base validates_presence_of :sender, :recipient, :aspect def self.invite(opts = {}) + opts[:identifier].downcase! return false if opts[:identifier] == opts[:from].email existing_user = self.find_existing_user(opts[:service], opts[:identifier]) diff --git a/app/models/user.rb b/app/models/user.rb index 8707c4f6d..23894a777 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -15,7 +15,7 @@ class User < ActiveRecord::Base :recoverable, :rememberable, :trackable, :validatable, :timeoutable - before_validation :strip_and_downcase_username, :on => :create + before_validation :strip_and_downcase_username before_validation :set_current_language, :on => :create validates_presence_of :username diff --git a/config/deploy.rb b/config/deploy.rb index 39052bb6c..2069cec3c 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -17,6 +17,7 @@ set :use_sudo, false set :scm_verbose, true set :repository_cache, "remote_cache" set :deploy_via, :checkout +set :branch, 'devise_invitable-update' namespace :deploy do task :symlink_config_files do diff --git a/db/migrate/20110512220310_downcase_usernames_again.rb b/db/migrate/20110512220310_downcase_usernames_again.rb deleted file mode 100644 index 02eeed8eb..000000000 --- a/db/migrate/20110512220310_downcase_usernames_again.rb +++ /dev/null @@ -1,10 +0,0 @@ -require 'db/migrate/20110421120744_downcase_usernames' -class DowncaseUsernamesAgain < ActiveRecord::Migration - def self.up - DowncaseUsernames.up - end - - def self.down - DowncaseUsernames.down - end -end diff --git a/db/migrate/20110513175000_eliminate_stray_user_records.rb b/db/migrate/20110513175000_eliminate_stray_user_records.rb new file mode 100644 index 000000000..856d36ab7 --- /dev/null +++ b/db/migrate/20110513175000_eliminate_stray_user_records.rb @@ -0,0 +1,29 @@ +class EliminateStrayUserRecords < ActiveRecord::Migration + def self.up + duplicated_emails = execute("SELECT LOWER(email) from users GROUP BY LOWER(email) HAVING COUNT(*) > 1").to_a + duplicated_emails.each do |email| + records = execute("SELECT users.id, users.username, users.created_at from users WHERE LOWER(users.email) = '#{email}'").to_a + with_username = records.select { |r| !r[1].blank? } + if with_username.length == 1 + execute("DELETE FROM users WHERE LOWER(users.email) = '#{email}' AND users.username IS NULL") + end + if with_username.length == 0 + newest_record = records.sort_by{|r| r[2].to_i}.last + execute("DELETE FROM users WHERE LOWER(users.email) = '#{email}' AND users.id != #{newest_record[0]}") + end + end + execute <