I have a usecase where I am looping over the array of values from hash using key as shown in the below code. I am creating object based on @params key and returning, because in request I am either getting params[:internal], params[:external] or params[:email]
def create_users
if @params['internal'].present?
return @params['internal'].map do |id|
User.new(type: :internal, user: find_employee(id))
end
elsif @params['external'].present?
return @params['external'].map do |id|
User.new(type: :external, contact: find_person(id))
end
elsif @params['emails'].present?
return @params['emails'].map do |email|
User.new(type: :external, contact: contact: fetch_or_create(email))
end
end
end
However, now my usecase has changed and now in request I might get params[:internal] or params[:external], params[:email] both together.
My ask here is, if in request I get params[:external], params[:email] both, would it be possible to do something like that:
return @params['external','email'].map do |id|
User.new(type: :external, contact: find_person(id))
User.new(type: :external, contact: contact: fetch_or_create(email))
end
However there is a possibility that only params[:external] or params[:email] is present in request. So for these cases, handling should also be done.
I am calling this method UserForm.new(params, user).create_users
and initialising the object :
def initialize(parameter_hash, user)
@params = parameter_hash
@user = user
end
Any help would be appreciated here, since I am new to ruby so exploring ways here to find the best solution.
Aucun commentaire:
Enregistrer un commentaire