Last active
January 22, 2025 15:37
-
-
Save o-200/585ae752da72a9562f1d97a4379c1a22 to your computer and use it in GitHub Desktop.
test_task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# refactored | |
# | |
class User < ApplicationRecord | |
has_many :user_interests | |
has_many :interests, through: :user_interests | |
has_many :user_skills | |
has_many :skills, through: :user_skills | |
end | |
class Interest < ApplicationRecord | |
has_many :user_interests | |
has_many :users, through: :user_interests | |
end | |
class Skill < ApplicationRecord | |
has_many :user_skills | |
has_many :users, through: :user_skills | |
end | |
# many-to-many models | |
class UserInterest < ApplicationRecord | |
belongs_to :users | |
belongs_to :interests | |
end | |
class UserSkill < ApplicationRecord | |
belongs_to :users | |
belongs_to :skills | |
end | |
# | |
class Users::Create < ActiveInteraction::Base | |
hash :params do | |
array :interests | |
array :skills | |
string :name, :patronymic, :email, :nationality, :country, :gender | |
integer :age | |
end | |
validate :gender_valid? | |
validate :age_valid? | |
validate :user_exists? | |
def execute | |
user_full_name = [ params["surname"], params["name"], params["patronymic"] ].compact.join(" ") | |
user_params = params.merge(user_full_name: user_full_name) | |
.except(:interests, :skills) | |
ActiveRecord::Base.transaction do | |
user = User.create!(user_params) | |
Interest.where(name: params["interests"]).each do |interest| | |
user.interests.create!(name: interest) | |
end | |
params["skills"].split(",").uniq.each do |skill| | |
user.skills << Skill.find_or_create_by!(name: skill) | |
end | |
end | |
end | |
private | |
def age_valid? | |
errors.add(:age, "You're too old or too young to use register here!") if params["age"] <= 0 || params["age"] > 90 | |
end | |
def gender_valid? | |
if params["gender"] != "male" && params["gender"] != "female" | |
errors.add(:gender, "Unknown gender!") | |
end | |
end | |
def user_exists? | |
errors.add(:gender, "User is exists!") if User.exists?(name: params["name"]) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# THIS IS A BEFORE REFACTORING VARIANT!!!!!! | |
# | |
class User < ApplicationRecord | |
has_many :interests | |
has_many :skills, class_name: 'Skil' | |
end | |
class Interest < ApplicationRecord | |
has_many :users | |
end | |
class Skil < ApplicationRecord | |
has_many :users | |
end | |
class Users::Create < ActiveInteraction::Base | |
hash :params | |
def execute | |
#don't do anything if params is empty | |
return unless params['name'] | |
return unless params['patronymic'] | |
return unless params['email'] | |
return unless params['age'] | |
return unless params['nationality'] | |
return unless params['country'] | |
return unless params['gender'] | |
########## | |
return if User.where(email: params['email']) | |
return if params['age'] <= 0 || params['age'] > 90 | |
return if params['gender'] != 'male' or params['gender'] != female | |
user_full_name = "#{params['surname']} #{params['name']} #{params['patronymic']}" | |
user_params = params.except(:interests) | |
user = User.create(user_params.merge(user_full_name)) | |
Intereset.where(name: params['interests']).each do |interest| | |
user.interests = user.interest + interest | |
user.save! | |
end | |
user_skills = [] | |
params['skills'].split(',').each do |skil| | |
skil = Skil.find(name: skil) | |
user_skills = user_skills + [skil] | |
end | |
user.skills = user_skills | |
user.save | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment