OSDN Git Service

classname rename to module_name
[pettanr/pettanr.git] / app / models / user.rb
1 class User < ActiveRecord::Base
2   has_one :author
3   has_one :artist
4   
5   # Include default devise modules. Others available are:
6   # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
7   devise :database_authenticatable, :registerable,
8          :recoverable, :rememberable, :trackable, :token_authenticatable, :validatable#, :confirmable
9
10   # Setup accessible (or protected) attributes for your model
11   attr_accessible :id, :name, :password, :password_confirmation, :remember_me , :email
12
13   def create_token
14     self.ensure_authentication_token
15     self.save
16   end
17   
18   def delete_token
19     self.authentication_token = nil
20     self.save
21   end
22   
23   def destroy
24     res = false
25     User.transaction do
26       self.author.scrolls.each do |scroll|
27         raise ActiveRecord::Rollback unless scroll.destroy_with_scroll_panel
28       end
29       self.author.panels.each do |panel|
30         raise ActiveRecord::Rollback unless panel.destroy_with_elements
31       end
32       if self.artist
33         self.artist.original_pictures.each do |original_picture|
34           raise ActiveRecord::Rollback unless original_picture.destroy_with_resource_picture
35         end
36         raise ActiveRecord::Rollback unless self.artist.destroy
37       end
38       raise ActiveRecord::Rollback unless self.author.destroy
39       raise ActiveRecord::Rollback unless super
40       res = true
41     end
42     res
43   end
44   
45 end