Files
mozo-backend/app/models/user.rb
T

62 lines
1.6 KiB
Ruby

class User
include SimplyStored::Couch
property :name
property :active_list_id
property :admin, type: :boolean, default: false
#FACEBOOK
property :provider
property :uid
property :oauth_token
property :oauth_expires_at
property :auth_data
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :token_authenticatable, :omniauthable, :omniauth_providers => [:facebook] # , :registerable
property :authentication_token
has_and_belongs_to_many :lists, storing_keys: false
has_many :orders
validates_uniqueness_of :email
before_save :ensure_authentication_token
view :by_authentication_token, key: :authentication_token
view :by_email, key: :email
view :by_facebook, key: [:provider, :uid]
def self.find_for_facebook_oauth(auth_data, user)
user = database.view(self.by_facebook(key: [auth_data.provider, auth_data.uid], limit: 1)).first
user || create(
provider: auth_data.provider,
uid: auth_data.uid,
name: auth_data.info.nickname,
email: auth_data.info.email,
password: Devise.friendly_token[0,20],
oauth_token: auth_data.credentials.token,
oauth_expires_at: auth_data.credentials.expires ? Time.at(auth_data.credentials.expires_at) : nil,
auth_data: auth_data
)
end
def self.from_omniauth(auth)
binding.pry
end
def list_is_closed!
self.active_list_id = nil
save
end
# This is the user name as it is shown to the supplier
def supplier_name
auth_data['info']['name'] rescue I18n.t('user.unknown_supplier_name')
end
def has_active_list?
active_list_id.present?
end
end