Commit e7b637f76b4f598702aa2d3456941e2295752eec

Authored by Braulio Bhavamitra
2 parents 1213fdf1 a6b5dfe6

Merge branch 'normalize-email' into 'master'

Normalize email before save

Users were saving emails in uppercase

See merge request !413
app/models/user.rb
... ... @@ -102,6 +102,7 @@ class User < ActiveRecord::Base
102 102 validates_length_of :email, :within => 3..100, :if => (lambda {|user| !user.email.blank?})
103 103 validates_uniqueness_of :login, :email, :case_sensitive => false, :scope => :environment_id
104 104 before_save :encrypt_password
  105 + before_save :normalize_email
105 106 validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})
106 107  
107 108 validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('{fn} must be checked in order to signup.').fix_i18n
... ... @@ -336,6 +337,11 @@ class User < ActiveRecord::Base
336 337 end
337 338  
338 339 protected
  340 +
  341 + def normalize_email
  342 + self.email = self.email.squish.downcase
  343 + end
  344 +
339 345 # before filter
340 346 def encrypt_password
341 347 return if password.blank?
... ...
db/migrate/20150112233715_normalize_users_email.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class NormalizeUsersEmail < ActiveRecord::Migration
  2 + def up
  3 + User.find_each do |u|
  4 + u.update_column :email, u.send(:normalize_email)
  5 + end
  6 + end
  7 +
  8 + def down
  9 + say "this migration can't be reverted"
  10 + end
  11 +end
... ...