Revisions

gist: 148498 Download_button fork
public
Public Clone URL: git://gist.github.com/148498.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# not all callbacks should be put in an observer, just things not directly associated with the model itself:
 
class UserObserver < ActiveRecord::Observer
  def after_create(user)
    ClearanceMailer::deliver_user_credentials(user)
  end
end
 
# callbacks are still cool in AR models if it deals with the class itself
class User < ActiveRecord::Base
  before_validation_on_create :generate_random_password,
                              :if => lambda {|user| user.password.blank? }
  protected
 
  def generate_random_password
    self.password = self.password_confirmation = ActiveSupport::SecureRandom.hex(6)
  end
end