class User::Create
attr_reader :params
def initialize(params)
@params = params
@user = User.new
end
def call
puts("User creation started!")
puts("Params provided: #{params}")
raise("Email not provided") if params[:email].nil?
raise("First Name not provided") if params[:first_name].nil?
raise("Last Name not provided") if params[:last_name].nil?
user.assign_attributes(
name: "#{params[:first_name]} #{params[:last_name]}",
email: params[:email],
is_admin: params.fetch(:is_admin, false)
)
if user.valid?
user.save
else
UserMailer.greetings(user: user).deliver_now
end
end
end
class User::Create
attr_reader :params
def initialize(params)
@params = params
@user = User.new
end
def call
display_logs
validate!
build_user
user.save || send_notification
end
private
def display_logs
puts("User creation started!")
puts("Params provided: #{params}")
end
def validate!
raise("Email not provided") if params[:email].nil?
raise("First Name not provided") if params[:first_name].nil?
raise("Last Name not provided") if params[:last_name].nil?
end
def build_user
user.assign_attributes(
name: "#{params[:first_name]} #{params[:last_name]}",
email: params[:email],
is_admin: params.fetch(:is_admin, false)
)
end
def send_notification
UserMailer.greetings(user: user).deliver_now
end
end