Class: User
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- User
- 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
-
#code ⇒ Object
Returns the value of attribute code.
Class Method Summary collapse
- .[](login) ⇒ Object
-
.entity_parameters ⇒ Object
Administrative parameters.
- .find_by_contact(login) ⇒ Object
- .ids_range ⇒ Object
-
.new_profile_parameters ⇒ Object
Parameters for registration.
- .page_for_administration(page = 1) ⇒ Object
- .profile_parameters ⇒ Object
- .sensitive_parameters ⇒ Object
Instance Method Summary collapse
- #add_role(role) ⇒ Object
- #age ⇒ Object
- #can_receive_letters? ⇒ Boolean
- #component_data(component_slug) ⇒ Object
- #email_as_login? ⇒ Boolean
- #full_name(include_patronymic = false) ⇒ Object
- #name_for_letter ⇒ Object
- #new_component_data(component_slug, component_data) ⇒ Object
- #phone_as_login? ⇒ Boolean
-
#profile_name ⇒ String
Name to be shown as profile.
- #remove_role(role) ⇒ Object
- #role?(role_name) ⇒ Boolean
- #role_ids ⇒ Object
- #text_for_link ⇒ Object
- #world_url ⇒ Object
Instance Attribute Details
#code ⇒ Object
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.[](login) find_by(slug: login) || find_by_contact(login) end |
.entity_parameters ⇒ Object
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
106 107 108 109 110 111 112 |
# File 'app/models/user.rb', line 106 def self.find_by_contact(login) if login.index('@').to_i.positive? User.with_email(login).first elsif login[0] == '+' User.find_by(phone: login) end end |
.ids_range ⇒ Object
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_parameters ⇒ Object
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
93 94 95 |
# File 'app/models/user.rb', line 93 def self.page_for_administration(page = 1) list_for_administration.page(page) end |
.profile_parameters ⇒ Object
97 98 99 |
# File 'app/models/user.rb', line 97 def self.profile_parameters %i[image allow_mail birthday] end |
.sensitive_parameters ⇒ Object
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
145 146 147 |
# File 'app/models/user.rb', line 145 def add_role(role) role&.add_user(self) end |
#age ⇒ Object
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
180 181 182 |
# File 'app/models/user.rb', line 180 def can_receive_letters? allow_mail? && !email.blank? end |
#component_data(component_slug) ⇒ Object
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
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
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_letter ⇒ Object
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
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
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_name ⇒ String
Name to be shown as profile
This can be redefined for cases when something other than screen name should be used.
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
150 151 152 |
# File 'app/models/user.rb', line 150 def remove_role(role) role&.remove_user(self) end |
#role?(role_name) ⇒ 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_ids ⇒ Object
140 141 142 |
# File 'app/models/user.rb', line 140 def role_ids Array(data[Role::CACHE_KEY]).map(&:to_i) end |
#text_for_link ⇒ Object
164 165 166 |
# File 'app/models/user.rb', line 164 def text_for_link profile_name end |
#world_url ⇒ Object
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 |