Class: User

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Checkable, HasLanguage, HasTrack, HasUuid, Toggleable
Defined in:
app/models/user.rb

Overview

User

Attributes:

agent_id [Agent], optional
allow_mail [boolean]
banned [boolean]
birthday [date], optional
bot [boolean]
created_at [DateTime]
data [jsonb]
deleted [boolean]
email [string], optional
email_confirmed [boolean]
image [SimpleImageUploader], optional
inviter_id [User], optional
ip_address_id [IpAddress], optional
language_id [Language], optional
last_seen [datetime], optional
notice [string], optional
password_digest [string]
phone [string], optional
phone_confirmed [boolean]
primary_id [User], optional
profile [Jsonb]
referral_link [string]
screen_name [string]
slug [string]
super_user [boolean]
updated_at [DateTime]
uuid [uuid]

Constant Summary collapse

EMAIL_LIMIT =
250
EMAIL_PATTERN =
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z0-9][-a-z0-9]+)\z/i.freeze
FLAG_SKIP_SCREEN_NAME =
'skip_screen_name_validation'
NOTICE_LIMIT =
255
PHONE_LIMIT =
50
SLUG_LIMIT =
36
SLUG_PATTERN =
/\A[_a-z0-9][-_a-z0-9]{0,34}[_a-z0-9]\z/i.freeze
SLUG_PATTERN_HTML =
'^[_a-zA-Z0-9][-_a-zA-Z0-9]{0,34}[_a-zA-Z0-9]$'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

Returns the value of attribute code.



49
50
51
# File 'app/models/user.rb', line 49

def code
  @code
end

Class Method Details

.[](login) ⇒ Object



88
89
90
# File 'app/models/user.rb', line 88

def self.[]()
  find_by(slug: ) || find_by_contact()
end

.entity_parametersObject

Administrative parameters



120
121
122
123
124
# File 'app/models/user.rb', line 120

def self.entity_parameters
  flags = %i[banned bot email_confirmed phone_confirmed]

  new_profile_parameters + flags + %i[notice screen_name slug]
end

.find_by_contact(login) ⇒ Object

Parameters:

  • login (String)


106
107
108
109
110
111
112
# File 'app/models/user.rb', line 106

def self.find_by_contact()
  if .index('@').to_i.positive?
    User.with_email().first
  elsif [0] == '+'
    User.find_by(phone: )
  end
end

.ids_rangeObject



126
127
128
129
130
# File 'app/models/user.rb', line 126

def self.ids_range
  min = User.minimum(:id).to_i
  max = User.maximum(:id).to_i
  (min..max)
end

.new_profile_parametersObject

Parameters for registration



115
116
117
# File 'app/models/user.rb', line 115

def self.new_profile_parameters
  profile_parameters + sensitive_parameters + %i[screen_name]
end

.page_for_administration(page = 1) ⇒ Object

Parameters:

  • page (Integer) (defaults to: 1)


93
94
95
# File 'app/models/user.rb', line 93

def self.page_for_administration(page = 1)
  list_for_administration.page(page)
end

.profile_parametersObject



97
98
99
# File 'app/models/user.rb', line 97

def self.profile_parameters
  %i[image allow_mail birthday]
end

.sensitive_parametersObject



101
102
103
# File 'app/models/user.rb', line 101

def self.sensitive_parameters
  %i[email phone password password_confirmation]
end

Instance Method Details

#add_role(role) ⇒ Object

Parameters:



145
146
147
# File 'app/models/user.rb', line 145

def add_role(role)
  role&.add_user(self)
end

#ageObject



209
210
211
212
213
214
215
216
217
# File 'app/models/user.rb', line 209

def age
  return if birthday.blank?

  now = Time.now.utc.to_date
  next_month = now.month > birthday.month
  next_day = (now.month == birthday.month && now.day >= birthday.day)
  delta = next_month || next_day ? 0 : 1
  now.year - birthday.year - delta
end

#can_receive_letters?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'app/models/user.rb', line 180

def can_receive_letters?
  allow_mail? && !email.blank?
end

#component_data(component_slug) ⇒ Object

Parameters:

  • component_slug (String|Symbol)


193
194
195
# File 'app/models/user.rb', line 193

def component_data(component_slug)
  data.dig('components', component_slug.to_s).to_h
end

#email_as_login?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'app/models/user.rb', line 184

def email_as_login?
  !data[Biovision::Components::UsersComponent::SETTING_EMAIL_AS_LOGIN].blank?
end

#full_name(include_patronymic = false) ⇒ Object

Parameters:

  • include_patronymic (TrueClass|FalseClass) (defaults to: false)


173
174
175
176
177
178
# File 'app/models/user.rb', line 173

def full_name(include_patronymic = false)
  result = [name_for_letter]
  result << profile['patronymic'].to_s.strip if include_patronymic
  result << profile['surname'].to_s.strip
  result.compact.join(' ')
end

#name_for_letterObject



168
169
170
# File 'app/models/user.rb', line 168

def name_for_letter
  profile['name'].blank? ? profile_name : profile['name']
end

#new_component_data(component_slug, component_data) ⇒ Object

Parameters:

  • component_slug (String|Symbol)
  • component_data (Hash)


199
200
201
202
# File 'app/models/user.rb', line 199

def new_component_data(component_slug, component_data)
  data['components'] ||= {}
  data['components'][component_slug.to_s] = component_data.to_h
end

#phone_as_login?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'app/models/user.rb', line 188

def phone_as_login?
  !data[Biovision::Components::UsersComponent::SETTING_PHONE_AS_LOGIN].blank?
end

#profile_nameString

Name to be shown as profile

This can be redefined for cases when something other than screen name should be used.

Returns:

  • (String)


160
161
162
# File 'app/models/user.rb', line 160

def profile_name
  email_as_login? ? email.to_s.split('@').first : screen_name
end

#remove_role(role) ⇒ Object

Parameters:



150
151
152
# File 'app/models/user.rb', line 150

def remove_role(role)
  role&.remove_user(self)
end

#role?(role_name) ⇒ Boolean

Parameters:

  • role_name (String)

Returns:

  • (Boolean)


133
134
135
136
137
138
# File 'app/models/user.rb', line 133

def role?(role_name)
  return true if super_user?

  role = Role[role_name]
  role_ids.include?(role&.id)
end

#role_idsObject



140
141
142
# File 'app/models/user.rb', line 140

def role_ids
  Array(data[Role::CACHE_KEY]).map(&:to_i)
end


164
165
166
# File 'app/models/user.rb', line 164

def text_for_link
  profile_name
end

#world_urlObject



204
205
206
207
# File 'app/models/user.rb', line 204

def world_url
  key = screen_name.downcase == slug ? screen_name : slug
  "/u/#{CGI.escape(key)}"
end