Skip to content

Instantly share code, notes, and snippets.

@TheScrubsFan
Last active November 20, 2023 18:05
Show Gist options
  • Select an option

  • Save TheScrubsFan/657cbf633eaf0aa755b182a50491585a to your computer and use it in GitHub Desktop.

Select an option

Save TheScrubsFan/657cbf633eaf0aa755b182a50491585a to your computer and use it in GitHub Desktop.
Test specification
class User < ApplicationRecord
has_many :interests
has_many :skills, class_name: 'Skil'
ALLOWED_GENDERS = %w[male female].freeze
MIN_AGE = 0.freeze
MAX_AGE = 90.freeze
validates
:name, :patronymic, :email, :age, :nationality, :country, :gender,
presence: true
validates :email, uniqueness: true
validates :age, numericality: { only_integer: true, in: MIN_AGE..MAX_AGE }
validates :gender, inclusion: { in: ALLOWED_GENDERS }
end
class Interest < ApplicationRecord
has_many :users
end
class Skil < ApplicationRecord
has_many :users
end
class Users::Create < ActiveInteraction::Base
hash :params do
string :name
string :surname, default: nil
string :patronymic
string :email
string :country
string :gender
string :nationality
string :skills, default: nil, desc: 'names of skills, separated with comma without spaces'
string :interests, default: nil, desc: 'names of interests, separated with comma without spaces'
integer :age
end
def execute
user = User.new user_params
interests = Interest.where(name: params[:interests].split(','))
user.interests << interests
skills = Skil.where(name: params[:skills].split(','))
user.skills << skills
unless user.save do
errors.merge!(user.errors)
end
user
end
private
def user_full_name
[
params[:surname],
params[:name],
params[:patronymic]
].join(' ')
end
def user_params
params.except(excepting_params).merge additional_params
end
def additional_params
{
fullname: user_full_name
}
end
def excepting_params
[:interests, :skills]
end
end
#User object in database
name string
surname string
patronymic string
fullname string
email string
age integer
nationality string
country string
interests array
gender string
skills string
#Interest object in database
name string
#Skil oject in database
name string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment