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 UserModule 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_one :user_card_delivery has_many :user_delivery_addresses, dependent: :destroy has_many :event_bookings, dependent: :destroy has_and_belongs_to_many :user_specializations has_and_belongs_to_many :brands 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 scope :internal_users, -> (condition) do joins(:user_card_delivery).where.not(user_card_deliveries: {id: nil} ).where(external_id: nil).uniq end scope :updated_at_last_day, -> (date) do where.not(external_id: nil) .joins(:user_specialization_approvals) .where("user_specialization_approvals.updated_at > ? OR users.updated_at > ?", date.to_datetime, date.to_datetime) .uniq 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 self[:user_type] 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 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