aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/user.rb
blob: 1889a3bf0eb76b83b12ab6810d719c825fca9b00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :invitable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :current_password, :password_confirmation, :remember_me, :name, :organisation_attributes

  belongs_to :organisation

  accepts_nested_attributes_for :organisation
  
  validates_presence_of :email
  validates_presence_of :name
  validates_presence_of :password
  validates_presence_of :password_confirmation

  before_validation(:on => :create) do
    self.password ||= Devise.friendly_token.first(6)
    self.password_confirmation ||= self.password
  end
  
  # remove organisation and referentials if last user of it
  after_destroy :check_destroy_organisation
  def check_destroy_organisation
    if organisation.users.empty? 
      organisation.destroy
    end
  end

end