Permalink
Browse files

Lowercase all email addresses.

  • Loading branch information...
1 parent 6e760f1 commit fe1619d1e1d5385979350c616f9e692c5c10ca11 @seven1m seven1m committed Oct 24, 2013
View
@@ -67,11 +67,13 @@ class Person < ActiveRecord::Base
def validate_email_unique
return unless email.present? and not deleted?
- if Person.where(["#{sql_lcase('email')} = ? and family_id != ? and id != ? and deleted = ?", email.downcase, family_id || 0, id || 0, false]).count > 0
+ if Person.where(["email = ? and family_id != ? and id != ? and deleted = ?", email, family_id || 0, id || 0, false]).any?
errors.add :email, :taken
end
end
+ lowercase_attribute :email, :alternate_email
+
after_initialize :guess_last_name, if: -> p { p.last_name.nil? }
def guess_last_name
@@ -20,3 +20,4 @@
require 'mail_destinations'
require 'locale'
require 'acts_as_password'
+require 'lowercase_attribute'
@@ -0,0 +1,14 @@
+class LowercaseAllEmails < ActiveRecord::Migration
+ def up
+ Site.each do
+ Person.find_each do |person|
+ person.email = person.email.downcase if person.email.present?
+ person.alternate_email = person.alternate_email.downcase if person.alternate_email.present?
+ person.save(validate: false) if person.changed?
+ end
+ end
+ end
+
+ def down
+ end
+end
View
@@ -0,0 +1,18 @@
+module LowercaseAttribute
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ def lowercase_attribute(*attrs)
+ attrs.each do |attr|
+ class_eval <<-END
+ def #{attr}=(e)
+ e = e.downcase if e.present?
+ super(e)
+ end
+ END
+ end
+ end
+ end
+end
+
+ActiveRecord::Base.send(:include, LowercaseAttribute)
View
@@ -126,6 +126,18 @@ class PersonTest < ActiveSupport::TestCase
assert @person.email_changed?
end
+ should 'lowercase email' do
+ @person = FactoryGirl.build(:person)
+ @person.email = 'TEST@example.COM'
+ assert_equal 'test@example.com', @person.email
+ end
+
+ should 'lowercase alternate_email' do
+ @person = FactoryGirl.build(:person)
+ @person.alternate_email = 'TEST@example.COM'
+ assert_equal 'test@example.com', @person.alternate_email
+ end
+
should "generate a custom directory pdf" do
@person = FactoryGirl.create(:person)
assert_match /PDF\-1\.3/, @person.generate_directory_pdf.to_s[0..100]

0 comments on commit fe1619d

Please sign in to comment.