Class: Maquina::InvitationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/maquina/invitations_controller.rb

Instance Method Summary collapse

Methods included from Authenticate

#after_authentication_url, #authenticated?, #current_user, #require_authentication

Instance Method Details

#createObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/maquina/invitations_controller.rb', line 16

def create
  authorize! with: policy_class if policy_class.present?

  email = params.dig(:invitation, :email)&.strip
  # management = Maquina::Current.management?

  @resource = Maquina::Invitation.order(created_at: :desc).find_or_initialize_by(email: email)
  if @resource.new_record?
    @resource.save
  elsif @resource.accepted?
    @resource = Maquina::Invitation.new(email: email)
    @resource.errors.add(:email, :invalid)
  end

  create! do |success|
    success.response do
      url = maquina.new_accept_invitations_url(token: CGI.escape(@resource.signed_id(purpose: :invitation, expires_in: 3.days)))
      Maquina::UserNotificationsMailer.with(email: @resource.email, inviteer: Maquina::Current.user.email, url: url).invitation_email.deliver_later

      flash[:notice] = {title: t("flash.#{@resource.model_name.i18n_key}.create.notice.title"), description: t("flash.#{@resource.model_name.i18n_key}.create.notice.description", email: email)}
      redirect_to collection_path
    end
  end
end