Module: SubjModels::UserModule

Includes:
TypesSupport::CardReceiveTypes, TypesSupport::UserTypes, ValuesChecker
Defined in:
lib/subj_models/user.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from TypesSupport::CardReceiveTypes

TypesSupport::CardReceiveTypes::CARD_RECEIVE_TYPES, TypesSupport::CardReceiveTypes::PICKUP, TypesSupport::CardReceiveTypes::POST_DELIVERY, TypesSupport::CardReceiveTypes::WITH_ORDER

Constants included from TypesSupport::UserTypes

TypesSupport::UserTypes::ADMIN, TypesSupport::UserTypes::DISTRIBUTOR, TypesSupport::UserTypes::LEGAL_PERSON, TypesSupport::UserTypes::NATURAL_PERSON, TypesSupport::UserTypes::USER_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValuesChecker

#check_string_for_int

Class Method Details

.get_by_email(email) ⇒ Object



92
93
94
# File 'lib/subj_models/user.rb', line 92

def self.get_by_email(email)
  self.where(:email => email).first
end

.included(including_class) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/subj_models/user.rb', line 21

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)
    end

  end

end

Instance Method Details

#available_categories_idsObject



116
117
118
# File 'lib/subj_models/user.rb', line 116

def available_categories_ids
  Category.joins(:nomenclatures).where("nomenclatures.id IN (?) OR nomenclatures.is_professional=FALSE", available_nomenclature_ids).uniq.pluck(:id)
end

#available_nomenclature_idsObject



112
113
114
# File 'lib/subj_models/user.rb', line 112

def available_nomenclature_ids
  Nomenclature.joins(access_groups: [user_specializations: :users]).where("users.id" => self.id).uniq.pluck(:id)
end

Returns:

  • (Boolean)


80
81
82
# File 'lib/subj_models/user.rb', line 80

def legal_person?
  user_type == USER_TYPES.key(LEGAL_PERSON)
end

#natural_person?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/subj_models/user.rb', line 76

def natural_person?
  user_type == USER_TYPES.key(NATURAL_PERSON)
end

#passwordObject



100
101
102
# File 'lib/subj_models/user.rb', line 100

def password
  self.encrypted_password
end

#password=(password) ⇒ Object



96
97
98
# File 'lib/subj_models/user.rb', line 96

def password=(password)
  self.encrypted_password = encrypt_password(password) unless password.blank?
end

#to_sObject



108
109
110
# File 'lib/subj_models/user.rb', line 108

def to_s
  email
end

#user_type=(value) ⇒ Object



88
89
90
# File 'lib/subj_models/user.rb', line 88

def user_type=(value)
  super(check_string_for_int(value))
end

#user_type_valueObject



84
85
86
# File 'lib/subj_models/user.rb', line 84

def user_type_value
  self[:user_type]
end

#valid_password?(password) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/subj_models/user.rb', line 104

def valid_password?(password)
  self.encrypted_password == encrypt_password(password)
end