Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active January 11, 2019 04:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulcsmith/5ca29744ab9ebfae88985394171ededf to your computer and use it in GitHub Desktop.
Save paulcsmith/5ca29744ab9ebfae88985394171ededf to your computer and use it in GitHub Desktop.
class SessionForm < LuckyRecord::VirtualForm
virtual email : String
virtual password : String
def submit
user = find_user
validate_required email, password
validate_user_and_password_match(user)
yield self, user
end
private def validate_user_and_password_match(user : User?)
# Usually ok to say what went wrong: https://github.com/luckyframework/lucky_cli/issues/192
if user && !user.valid_password?(password.value.to_s)
password.add_error "is incorrect"
elsif user.nil?
email.add_error "was not found"
end
end
private def find_user
email.value.try do |value|
UserQuery.new.email(value).first?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment