Skip to content

adding additional validation to email uniqueness #242

Closed
@dhruvg

Description

@dhruvg

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions