adding additional validation to email uniqueness #242

Open
dhruvg opened this Issue Mar 17, 2011 · 1 comment

Comments

Projects
None yet
2 participants

dhruvg commented Mar 17, 2011

I am maintaining another model for each user called secondary_email where users can basically tag on multiple email addresses to their account. I want to modify my User model to also check that a new user's email is not an active secondary email... Here is the code I have so far:

acts_as_authentic do |c|
c.merge_validates_length_of_password_confirmation_field_options(
:minimum => 6, :maximum => 40)
c.merge_validates_uniqueness_of_email_field_options(:if =>
"#{!SecondaryEmail.active?(email_field)}".to_sym)
end

returns true if the given email is active, false otherwise

def self.active?(email)
SecondaryEmail.where("email = :email and pending = 0",
:email => email).length > 0
end

It doesn't seem to be passing my test case:

it "should reject if email is an active secondary email" do
@user = User.unsafe_create(@attr)
secondary_email = Factory(:secondary_email, :user_id => @user[:id])
User.unsafe_create(@attr.merge(:email =>
secondary_email[:email])).should_not be_valid
end

The Factory which creates the SecondaryEmail appropriately sets pending to 0 which is checked in the active? function:

Secondary Email

Factory.define :secondary_email do |se|
se.sequence(:email) { |n| "voltaire_se#{n}@example.com"}
se.pending "0"
se.user_id "1"
se.crypt "random"
se.num_verifications "5"
end

So, I am pretty sure my test case is correct. I think the issue likes in the acts_as_authentic block -- is the code above the right way to go about it? What is the correct approach?

Thanks!

Your :if condition will evaluate to :true or :false, neither of which are methods on the model. This may be of some help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment