Class: Biovision::Components::Users::ProfileHandler
- Inherits:
-
Object
- Object
- Biovision::Components::Users::ProfileHandler
- Defined in:
- app/lib/biovision/components/users/profile_handler.rb
Overview
Handling user profiles
Constant Summary collapse
- GENDERS =
{ 0 => 'female', 1 => 'male', 2 => 'other' }.freeze
Instance Attribute Summary collapse
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
-
.allowed_parameters ⇒ Object
List of attributes that can be used in user profile.
- .array_value(input, keys) ⇒ Object
-
.clean_gender(input) ⇒ Object
Restrict gender to only available values.
-
.clean_parameters(input) ⇒ Object
Normalize profile parameters for storage.
- .gender(gender_id) ⇒ Object
- .genders_for_select ⇒ Object
-
.normalized_parameters(input) ⇒ Object
Format parameters that have more restrictions than just “string” type.
-
.permitted_for_request ⇒ Object
List of parameters to be used in controllers for create/update.
- .scalar_value(input, key) ⇒ Object
Instance Method Summary collapse
- #create(user_data, profile_data) ⇒ Object
-
#initialize(component) ⇒ ProfileHandler
constructor
A new instance of ProfileHandler.
- #profile=(profile_data) ⇒ Object
- #update(user_data, profile_data) ⇒ Object
Constructor Details
#initialize(component) ⇒ ProfileHandler
Returns a new instance of ProfileHandler.
13 14 15 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 13 def initialize(component) @component = component end |
Instance Attribute Details
#user ⇒ Object
Returns the value of attribute user.
10 11 12 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 10 def user @user end |
Class Method Details
.allowed_parameters ⇒ Object
List of attributes that can be used in user profile
Change this method in decorators for other values
20 21 22 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 20 def self.allowed_parameters %w[gender name patronymic surname about] end |
.array_value(input, keys) ⇒ Object
59 60 61 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 59 def self.array_value(input, keys) keys.each.map { |key| [key, scalar_value(input, key)] }.to_h end |
.clean_gender(input) ⇒ Object
Restrict gender to only available values
Defined gender is stored as integer.
92 93 94 95 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 92 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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 36 def self.clean_parameters(input) return {} unless input.respond_to?(:key?) output = normalized_parameters(input) (allowed_parameters - output.keys).each do |parameter| if parameter.respond_to?(:shift) key = parameter.shift output[key] = array_value(input[key].to_h, parameter) else output[parameter] = scalar_value(input, parameter) end end output end |
.gender(gender_id) ⇒ Object
64 65 66 67 68 69 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 64 def self.gender(gender_id) prefix = 'activerecord.attributes.user_profile.genders' gender_key = gender_id.blank? ? '' : gender_id.to_i postfix = GENDERS[gender_key] || 'not_set' I18n.t("#{prefix}.#{postfix}") end |
.genders_for_select ⇒ Object
71 72 73 74 75 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 71 def self.genders_for_select default_key = 'activerecord.attributes.user_profile.genders.not_set' genders = [[I18n.t(default_key), '']] genders + GENDERS.keys.map { |k| [gender(k), k] } 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.
83 84 85 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 83 def self.normalized_parameters(input) { gender: clean_gender(input['gender']) } end |
.permitted_for_request ⇒ Object
List of parameters to be used in controllers for create/update
25 26 27 28 29 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 25 def self.permitted_for_request allowed_parameters.map do |key| key.respond_to?(:to_h) ? [[key.shift, key]].to_h : key end end |
.scalar_value(input, key) ⇒ Object
53 54 55 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 53 def self.scalar_value(input, key) input.key?(key) ? input[key].to_s : nil end |
Instance Method Details
#create(user_data, profile_data) ⇒ Object
104 105 106 107 108 109 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 104 def create(user_data, profile_data) self.user = User.new(user_data) self.profile = profile_data Biovision::Components::UsersComponent.created_user(user) if user.save user end |
#profile=(profile_data) ⇒ Object
98 99 100 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 98 def profile=(profile_data) user.profile = self.class.clean_parameters(profile_data).to_h end |
#update(user_data, profile_data) ⇒ Object
113 114 115 116 117 118 |
# File 'app/lib/biovision/components/users/profile_handler.rb', line 113 def update(user_data, profile_data) return if user.nil? self.profile = profile_data user.update(user_data) end |