Sha256: 7bef0617a99fa721b29f01fa1f36ce4f59cd0f28ba001ba9254031ac24f915e0

Contents?: true

Size: 1003 Bytes

Versions: 115

Compression:

Stored size: 1003 Bytes

Contents

module Invitations::Base
  extend ActiveSupport::Concern

  included do
    belongs_to :team
    belongs_to :from_membership, class_name: "Membership"
    has_one :membership, dependent: :nullify
    has_many :roles, through: :membership

    accepts_nested_attributes_for :membership

    validates :email, presence: true

    before_create :generate_uuid
    after_create :set_added_by_membership
    after_create :send_invitation_email
  end

  def set_added_by_membership
    membership.update(added_by: from_membership)
  end

  def send_invitation_email
    UserMailer.invited(uuid).deliver_later
  end

  def generate_uuid
    self.uuid = SecureRandom.hex
  end

  def accept_for(user)
    User.transaction do
      user.memberships << membership
      user.update(current_team: team, former_user: false)
      destroy
    end
  end

  def name
    I18n.t("invitations.values.name", team_name: team.name)
  end

  def is_for?(user)
    user.email.downcase.strip == email.downcase.strip
  end
end

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
bullet_train-1.2.21 app/models/concerns/invitations/base.rb
bullet_train-1.2.20 app/models/concerns/invitations/base.rb
bullet_train-1.2.19 app/models/concerns/invitations/base.rb
bullet_train-1.2.18 app/models/concerns/invitations/base.rb
bullet_train-1.2.17 app/models/concerns/invitations/base.rb
bullet_train-1.2.16 app/models/concerns/invitations/base.rb
bullet_train-1.2.15 app/models/concerns/invitations/base.rb
bullet_train-1.2.14 app/models/concerns/invitations/base.rb
bullet_train-1.2.13 app/models/concerns/invitations/base.rb
bullet_train-1.2.12 app/models/concerns/invitations/base.rb
bullet_train-1.2.11 app/models/concerns/invitations/base.rb
bullet_train-1.2.10 app/models/concerns/invitations/base.rb
bullet_train-1.2.9 app/models/concerns/invitations/base.rb
bullet_train-1.2.8 app/models/concerns/invitations/base.rb
bullet_train-1.2.7 app/models/concerns/invitations/base.rb
bullet_train-1.2.6 app/models/concerns/invitations/base.rb
bullet_train-1.2.5 app/models/concerns/invitations/base.rb
bullet_train-1.2.4 app/models/concerns/invitations/base.rb
bullet_train-1.2.3 app/models/concerns/invitations/base.rb
bullet_train-1.2.2 app/models/concerns/invitations/base.rb