Files
nairobi/app/models/lock.rb
Javi Martín d0d681a44b Add and apply EmptyLineAfterGuardClause rule
We were inconsistent on this one. I consider it particularly useful when
a method starts with a `return` statement.

In other cases, we probably shouldn't have a guard rule in the middle of
a method in any case, but that's a different refactoring.
2019-10-24 17:56:03 +02:00

32 lines
513 B
Ruby

class Lock < ApplicationRecord
belongs_to :user
before_save :set_locked_until
def locked?
locked_until > Time.current
end
def set_locked_until
self.locked_until = lock_time if too_many_tries?
end
def lock_time
Time.current + (2**tries).minutes
end
def too_many_tries?
return false unless tries > 0
tries % Lock.max_tries == 0
end
def self.increase_tries(user)
find_or_create_by!(user: user).increment!(:tries).save!
end
def self.max_tries
5
end
end