Add basic invitations

This commit is contained in:
2020-12-02 15:22:58 +01:00
parent 18df8fe449
commit d7fbda0855
12 changed files with 152 additions and 4 deletions

View File

@@ -0,0 +1,33 @@
class InvitationsController < ApplicationController
before_action :require_user_signed_in
# GET /invitations
def index
@invitations = current_user.invitations
end
# POST /invitations
def create
@invitation = Invitation.new(user: current_user)
respond_to do |format|
if @invitation.save
format.html { redirect_to @invitation, notice: 'Invitation was successfully created.' }
format.json { render :show, status: :created, location: @invitation }
else
format.html { render :new }
format.json { render json: @invitation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invitations/1
def destroy
@invitation = current_user.invitations.find(params[:id])
@invitation.destroy
respond_to do |format|
format.html { redirect_to invitations_url }
format.json { head :no_content }
end
end
end

View File

@@ -0,0 +1,2 @@
module InvitationsHelper
end

14
app/models/invitation.rb Normal file
View File

@@ -0,0 +1,14 @@
class Invitation < ApplicationRecord
# Relations
belongs_to :user
validates_presence_of :user
before_create :generate_token
private
def generate_token
self.token = SecureRandom.hex(8)
end
end

View File

@@ -1,4 +1,7 @@
class User < ApplicationRecord
# Relations
has_many :invitations, dependent: :destroy
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :ldap_authenticatable,

View File

@@ -0,0 +1,23 @@
<section>
<h2>Invitations</h2>
<table>
<thead>
<tr>
<th>Token</th>
<th>Created at</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @invitations.each do |invitation| %>
<tr>
<td><%= invitation.token %></td>
<td><%= invitation.created_at %></td>
<td><%= link_to 'Delete', invitation, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</section>