Skip to content

Instantly share code, notes, and snippets.

@mironov
Created June 13, 2010 14:42
Show Gist options
  • Save mironov/436707 to your computer and use it in GitHub Desktop.
Save mironov/436707 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
# gradual engagement scheme for authlogic:
# 1. user is created by admin, only name and phone number required
# admin may set email address (primary_email) at creation
# 2. user is then sent invitation to activate their account
# 3. user#active is set true by hidden field on activation form where
# the user sets their login and password
# cannot use :if condition since authlogic uses them
# fatal flaw: will break any authlogic :unless condition
# (fortunately, authlogic currently does not use the :unless condition)
acts_as_authentic do |c|
### email: only validate if email is present
c.email_field = :primary_email
c.merge_validates_length_of_email_field_options({:unless => :skip_email_validation})
c.merge_validates_format_of_email_field_options({:unless => :skip_email_validation})
c.merge_validates_uniqueness_of_email_field_options({:unless => :skip_email_validation})
### password: only validate if user is active (require_password? is authlogic method)
c.merge_validates_confirmation_of_password_field_options({:unless => :inactive?})
c.merge_validates_length_of_password_field_options({:unless => :inactive?, :minimum => 3})
c.merge_validates_length_of_password_confirmation_field_options({:unless => :inactive?, :minimum => 3})
### login: only validate if user is active
c.merge_validates_length_of_login_field_options({:unless => :inactive?})
c.merge_validates_format_of_login_field_options({:unless => :inactive?})
c.merge_validates_uniqueness_of_login_field_options({:unless => :inactive?})
end
def inactive?
!active
end
def skip_email_validation
!attribute_present?('primary_email')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment