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
# authlogic options pulled out and explicitly set here so that it is
# visible and so that :if conditional statements can be merged
acts_as_authentic do |c|
### email: only validate if email is present
c.email_field = :primary_email
c.validates_length_of_email_field_options({
:if => Proc.new {|user| user.attribute_present?('primary_email')},
:within => 6..100
})
c.validates_format_of_email_field_options({
:if => Proc.new {|user| user.attribute_present?('primary_email')},
:with => Authlogic::Regex.email,
:message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")
})
c.validates_uniqueness_of_email_field_options({
:if => Proc.new {|user| user.attribute_present?('primary_email') && user.primary_email_changed?},
:case_sensitive => false,
:scope => validations_scope
})
### password: only validate if user is active (require_password? is authlogic method)
c.validates_length_of_password_field_options({
:if => Proc.new {|user| user.active? && user.send(:require_password?)},
:minimum => 3
})
c.validates_confirmation_of_password_field_options({
:if => Proc.new {|user| user.active? && user.send(:require_password?)}
})
c.validates_length_of_password_confirmation_field_options({
:if => Proc.new {|user| user.active? && user.send(:require_password?)},
:minimum => 3
})
### login: only validate if user is active
c.validates_length_of_login_field_options({
:if => :active?,
:within => 3..100
})
c.validates_format_of_login_field_options({
:if => :active?,
:with => Authlogic::Regex.login,
:message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")
})
c.validates_uniqueness_of_login_field_options({
:if => Proc.new {|user| user.active? && user.login_changed?},
:case_sensitive => false,
:scope => validations_scope
})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment