require 'digest/sha1' require 'subj_models/services/values_checker' require 'subj_models/services/types_support' require 'subj_models/concerns/shared_scopes' require 'subj_models/concerns/comprising_external_id' module SubjModels module User include SubjModels::TypesSupport::UserTypes include SubjModels::TypesSupport::CardReceiveTypes include SubjModels::ValuesChecker module ClassMethods def get_by_email(email) self.where(:email => email).first end end def self.included(including_class) including_class.extend ClassMethods including_class.class_eval do include RademadeAdmin::UserModule include SubjModels::ComprisingExternalId include SubjModels::SharedScopes enum user_type: USER_TYPES enum card_receive_type: CARD_RECEIVE_TYPES # has_many :orders # has_many :user_specialization_approvals, dependent: :destroy # has_many :user_cards, dependent: :destroy # has_one :user_work_place, dependent: :destroy # has_many :user_delivery_addresses, dependent: :destroy # has_and_belongs_to_many :user_specializations # belongs_to :document_file # belongs_to :city # belongs_to :avatar, class_name: "DocumentFile" validates :first_name, :last_name, presence: true, on: :update, unless: lambda { |user| user.legal_person? || user.skip_validations } validates :email, uniqueness: { case_sensitive: false }, presence: true, on: :update, unless: :skip_validations validates :phone, uniqueness: { case_sensitive: false }, presence: true, on: :update, unless: :skip_validations validates :user_type, inclusion: { in: user_types.keys }, on: :update, unless: :skip_validations attr_accessor :skip_validations scope :external_id, -> external_id do external_id ? where(external_id: external_id) : User.none end end end def natural_person? user_type == USER_TYPES.key(NATURAL_PERSON) end def legal_person? user_type == USER_TYPES.key(LEGAL_PERSON) end def user_type=(value) super(check_string_for_int(value)) end def self.get_by_email(email) self.where(:email => email).first end def password=(password) self.encrypted_password = encrypt_password(password) unless password.blank? end def password self.encrypted_password end def valid_password?(password) self.encrypted_password == encrypt_password(password) end def to_s email end def admin? self.user_type == 'админ' end def available_nomenclature_ids Nomenclature.joins(access_groups: [user_specializations: :users]).where("users.id" => self.id).uniq.pluck(:id) end def available_categories_ids Category.joins(:nomenclatures).where("nomenclatures.id IN (?) OR nomenclatures.is_professional=FALSE", available_nomenclature_ids).uniq.pluck(:id) end private def encrypt_password(password) Digest::SHA1.hexdigest(password) end end end