module Raddar class User < ActiveRecord::Base attr_accessor :login store_accessor :privacy store_accessor :email_preferences serialize :email_preferences, Raddar::HstoreSerializer serialize :privacy, Raddar::HstoreSerializer has_many :followerships, dependent: :destroy has_many :followers, class_name: 'Followership', as: :followable, dependent: :destroy has_and_belongs_to_many :roles has_many :notifications, dependent: :destroy has_many :external_accounts, dependent: :destroy mount_uploader :avatar, AvatarUploader # Include default devise modules. Others available are: # :token_authenticatable, # :lockable and :timeoutable devise :database_authenticatable, :registerable, :omniauthable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, authentication_keys: [:login] validates :name, presence: true, uniqueness: { case_sensitive: false }, format: { with: /\A(([a-z]|[A-Z]|[0-9]|_)+)\z/ }, length: { maximum: 20, minimum: 3 } validates :state, presence: true, inclusion: { in: ['active', 'blocked'] } def self.find_first_by_auth_conditions(warden_conditions) conditions = warden_conditions.dup if login = conditions.delete(:login) where(conditions).where('lower(name) = :value OR lower(email) = :value', value: login.downcase).first else where(conditions).first end end cattr_accessor(:privaciable_fields) do [:email, :gender, :location, :birthday] end cattr_accessor(:email_preferences_keys) do [:new_follower] end def admin? self.roles.exists?(name: 'admin') == '1' end def active_for_authentication? super && self.state == 'active' end def inactive_message (self.state == 'active') ? super : I18n.t('flash.users.sessions.blocked') end def privacy_keys account_keys = self.external_accounts.map(&:provider) @@privaciable_fields + account_keys end end end