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



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

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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 :user_videos, dependent: :destroy
    has_many :likes, dependent: :destroy
    has_many :liked_user_videos, -> { uniq }, through: :likes, source: :user_video
    has_many :orders
    has_many :notifications
    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

    scope :angels, ->  do
      where(is_angel: true)
    end

  end

end

Instance Method Details

#available_categories_idsObject



127
128
129
# File 'lib/subj_models/user.rb', line 127

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



123
124
125
# File 'lib/subj_models/user.rb', line 123

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

Returns:

  • (Boolean)


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

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

#natural_person?Boolean

Returns:

  • (Boolean)


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

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

#passwordObject



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

def password
  self.encrypted_password
end

#password=(password) ⇒ Object



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

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

#to_sObject



119
120
121
# File 'lib/subj_models/user.rb', line 119

def to_s
  email
end

#user_type_valueObject



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

def user_type_value
  self[:user_type]
end

#valid_password?(password) ⇒ Boolean

Returns:

  • (Boolean)


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

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