Skip to content

Instantly share code, notes, and snippets.

@swanandp
Created August 4, 2011 13:17
Show Gist options
  • Save swanandp/1125125 to your computer and use it in GitHub Desktop.
Save swanandp/1125125 to your computer and use it in GitHub Desktop.
Shows off a few samples for acts_as_authentic configuration
class User < ActiveRecord::Base
# gradual engagement is needed, to allow admin creation of users
# users should create authorization data only when they need to
# access some system feature that requires authentication
# So ... our gradual engagement scheme for authlogic:
# 1. admin creates user, setting only name and phone number (required)
# admin may set email address (optional)
# 2. user is sent email invitation with link to activate their account
# admin can re-send email at user's request
# 3. user sets login and password/password_confirmation using activation form
# #active attribute set true in controller #create method
# note: 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 primary_email 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
### authlogic minimum defaults to 4, our system is not as critical :)
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