One possibility is to use throw
/catch
to prematurely exit the method:
module ValidationHelpers
def check t
throw :fail, t if t.error
t
end
end
And to use it in your validation method:
def add_user_to_team(user_id, team_id)
catch(:fail) do
User.transaction do
user = check(validate_user_exists(user_id)).obj
team = check(validate_team_exists(team_id)).obj
# ...lots of other validations...
result(true, nil, team_user)
end
end
end