OSDN Git Service

fix: devise
[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,  :validatable, 
9          :omniauthable#, :confirmable
10
11   def create_token
12     self.ensure_authentication_token
13     self.save
14   end
15   
16   def delete_token
17     self.authentication_token = nil
18     self.save
19   end
20   
21   def destroy
22     res = false
23     User.transaction do
24       self.author.scrolls.each do |scroll|
25         raise ActiveRecord::Rollback unless scroll.destroy_with_scroll_panel
26       end
27       self.author.panels.each do |panel|
28         raise ActiveRecord::Rollback unless panel.destroy_with_elements
29       end
30       if self.artist
31         self.artist.original_pictures.each do |original_picture|
32           raise ActiveRecord::Rollback unless original_picture.destroy_with_resource_picture
33         end
34         raise ActiveRecord::Rollback unless self.artist.destroy
35       end
36       raise ActiveRecord::Rollback unless self.author.destroy
37       raise ActiveRecord::Rollback unless super
38       res = true
39     end
40     res
41   end
42   
43   private
44   def user_params
45     params.require(:user).permit(:email, :password, :password_confirmation, :remember_me, :authentication_token)
46   end
47   
48 end