1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-02 20:15:22 +02:00

Family invites (#1397)

* Initial pass at household invites

* Invitee setup

* Clean up add member form

* Lint and other tweaks

* Security cleanup

* Lint

* i18n fixes

* More i18n cleanup

* Show pending invites

* Don't use turbo on the form

* Improved email design

* Basic tests

* Lint

* Update onboardings_controller.rb

* Registration + invite cleanup

* Lint

* Update brakeman.ignore

* Update brakeman.ignore

* Self host invite links

* Test tweaks

* Address missing param error
This commit is contained in:
Josh Pigford 2024-11-01 10:23:27 -05:00 committed by GitHub
parent 09b269273a
commit 793bd852a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 502 additions and 45 deletions

View file

@ -4,6 +4,7 @@ class Family < ApplicationRecord
include Providable
has_many :users, dependent: :destroy
has_many :invitations, dependent: :destroy
has_many :tags, dependent: :destroy
has_many :accounts, dependent: :destroy
has_many :institutions, dependent: :destroy

37
app/models/invitation.rb Normal file
View file

@ -0,0 +1,37 @@
class Invitation < ApplicationRecord
belongs_to :family
belongs_to :inviter, class_name: "User"
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :role, presence: true, inclusion: { in: %w[admin member] }
validates :token, presence: true, uniqueness: true
validate :inviter_is_admin
before_validation :generate_token, on: :create
before_create :set_expiration
scope :pending, -> { where(accepted_at: nil).where("expires_at > ?", Time.current) }
scope :accepted, -> { where.not(accepted_at: nil) }
scope :most_recent_for_email, ->(email) { where(email: email).order(accepted_at: :desc).first }
def pending?
accepted_at.nil? && expires_at > Time.current
end
private
def generate_token
loop do
self.token = SecureRandom.hex(32)
break unless self.class.exists?(token: token)
end
end
def set_expiration
self.expires_at = 3.days.from_now
end
def inviter_is_admin
inviter.admin?
end
end