Show unused invitations list

This commit is contained in:
Basti 2020-12-03 14:48:43 +01:00
parent f7e48ad3a6
commit a792d66c90
4 changed files with 37 additions and 11 deletions

View File

@ -6,7 +6,8 @@ class InvitationsController < ApplicationController
# GET /invitations # GET /invitations
def index def index
@invitations = current_user.invitations @invitations_unused = current_user.invitations.unused
@invitations_used = current_user.invitations.used
end end
# GET /invitations/a-random-invitation-token # GET /invitations/a-random-invitation-token

View File

@ -2,10 +2,13 @@ class Invitation < ApplicationRecord
# Relations # Relations
belongs_to :user belongs_to :user
# Validations
validates_presence_of :user validates_presence_of :user
# Hooks
before_create :generate_token before_create :generate_token
# Scopes
scope :unused, -> { where(used_at: nil) } scope :unused, -> { where(used_at: nil) }
scope :used, -> { where.not(used_at: nil) } scope :used, -> { where.not(used_at: nil) }

View File

@ -1,23 +1,46 @@
<section> <section>
<h2>Invitations</h2> <h2>Invitations</h2>
<% if @invitations_unused.any? %>
<table>
<thead>
<tr>
<th>URL</th>
</tr>
</thead>
<tbody>
<% @invitations_unused.each do |invitation| %>
<tr>
<td><%= invitation_url(invitation.token) %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<p>
You do not have any invitations to give away yet. All good
things come in time.
</p>
<% end %>
</section>
<% if @invitations_used.any? %>
<h3>Accepted Invitations</h3>
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Token</th> <th>URL</th>
<th>Created at</th> <th>Used at</th>
<th colspan="3"></th> <th>Invited user</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% @invitations.each do |invitation| %> <% @invitations_used.each do |invitation| %>
<tr> <tr>
<td><%= invitation.token %></td> <td><%= invitation_url(invitation.token) %></td>
<td><%= invitation.created_at %></td> <td><%= invitation.used_at %></td>
<td><%= link_to 'Delete', invitation, method: :delete, data: { confirm: 'Are you sure?' } %></td> <td><%= User.find(invitation.invited_user_id).address %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
</section> <% end %>

View File

@ -1,6 +1,5 @@
FactoryBot.define do FactoryBot.define do
factory :invitation do factory :invitation do
id { 1 }
token { "abcdef123456" } token { "abcdef123456" }
user user
end end