X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fuser.rb;h=69ca60db1364257be4a8db161a3bd867ce09f4bc;hb=refs%2Fheads%2Fmaster;hp=90958af695845c20a37338c588142b955b788020;hpb=baeeb875e66e472a4c9eb82ed399bcaeaf2f8990;p=pettanr%2Fpettanr.git diff --git a/app/models/user.rb b/app/models/user.rb index 90958af6..69ca60db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,22 +1,81 @@ class User < ActiveRecord::Base has_one :author + has_one :artist # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, - :recoverable, :rememberable, :trackable, :token_authenticatable, :validatable#, :confirmable - - # Setup accessible (or protected) attributes for your model - attr_accessible :id, :name, :password, :password_confirmation, :remember_me , :email - + :recoverable, :rememberable, :trackable, :validatable, + :omniauthable, :omniauth_providers => [:twitter, :google_oauth2] + #, :confirmable + def create_token - self.ensure_authentication_token + loop do + token = Devise.friendly_token + if token_suitable?(token) + self.authentication_token = token + break + end + end self.save end + def token_suitable?(token) + !self.class.exists?(authentication_token: token) + end + def delete_token self.authentication_token = nil self.save end + def destroy + res = false + User.transaction do + self.author.scrolls.each do |scroll| + raise ActiveRecord::Rollback unless scroll.destroy_with_scroll_panel + end + self.author.panels.each do |panel| + raise ActiveRecord::Rollback unless panel.destroy_with_elements + end + if self.artist + self.artist.original_pictures.each do |original_picture| + raise ActiveRecord::Rollback unless original_picture.destroy_with_resource_picture + end + raise ActiveRecord::Rollback unless self.artist.destroy + end + raise ActiveRecord::Rollback unless self.author.destroy + raise ActiveRecord::Rollback unless super + res = true + end + res + end + + def self.from_omniauth(auth) + where(provider: auth.provider, uid: auth.uid).first_or_create do |user| + user.name = auth.info.nickname + user.email = auth.info.email + end + end + + def self.new_with_session(params, session) + if session["devise.user_attributes"] + new(session["devise.user_attributes"], without_protection: true) do |user| + user.attributes = params + user.valid? + end + else + super + end + end + + def password_required? + super && provider.blank? + end + + private + def user_params + params.require(:user).permit(:email, :password, :password_confirmation, :remember_me, :authentication_token) + end + end