Class: UserProfileHandler
- Inherits:
-
Object
- Object
- UserProfileHandler
- Defined in:
- app/services/user_profile_handler.rb
Overview
Handler for user profile parameters
Constant Summary collapse
- GENDERS =
{ 0 => 'female', 1 => 'male' }.freeze
Class Method Summary collapse
-
.allowed_parameters ⇒ Object
List of attributes that can be used in user profile.
-
.clean_gender(input) ⇒ Object
Restrict gender to only available values.
-
.clean_parameters(input) ⇒ Object
Normalize profile parameters for storage.
-
.normalized_parameters(input) ⇒ Object
Format parameters that have more restrictions than just “string” type.
-
.search_string(user) ⇒ Object
Prepare search string for simple user search.
Class Method Details
.allowed_parameters ⇒ Object
List of attributes that can be used in user profile
Change this method in decorators for other values
10 11 12 |
# File 'app/services/user_profile_handler.rb', line 10 def self.allowed_parameters %w[gender name patronymic surname about] end |
.clean_gender(input) ⇒ Object
Restrict gender to only available values
Defined gender is stored as integer.
51 52 53 54 |
# File 'app/services/user_profile_handler.rb', line 51 def self.clean_gender(input) gender_key = input.blank? ? nil : input.to_i GENDERS.key?(gender_key) ? gender_key : nil end |
.clean_parameters(input) ⇒ Object
Normalize profile parameters for storage
Makes consistent format of profile hash.
19 20 21 22 23 24 25 26 27 |
# File 'app/services/user_profile_handler.rb', line 19 def self.clean_parameters(input) return {} unless input.respond_to?(:key?) output = normalized_parameters(input) (allowed_parameters - output.keys).each do |parameter| output[parameter] = input.key?(parameter) ? input[parameter].to_s : nil end output end |
.normalized_parameters(input) ⇒ Object
Format parameters that have more restrictions than just “string” type
Change this method in decorator to add other fields with type enumerable, integer, etc.
42 43 44 |
# File 'app/services/user_profile_handler.rb', line 42 def self.normalized_parameters(input) { gender: clean_gender(input['gender']) } end |
.search_string(user) ⇒ Object
Prepare search string for simple user search
32 33 34 |
# File 'app/services/user_profile_handler.rb', line 32 def self.search_string(user) "#{user.data.dig('profile', 'surname')} #{user.data.dig('profile', 'name')}" end |