Skip to content

Commit 167ce4c

Browse files
author
Joel
committed
List all existing tokens, delete unused tokens
1 parent e19207e commit 167ce4c

File tree

7 files changed

+62
-1
lines changed

7 files changed

+62
-1
lines changed

app/controllers/tokens_controller.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
class TokensController < ApplicationController
2+
rescue_from ActiveRecord::RecordNotFound do
3+
flash[:error] = "Token #{params[:id]} not found."
4+
redirect_to '/tokens'
5+
end
6+
27
def new
38
@token = Token.new
49
end
510

11+
def index
12+
@tokens = Token.all(:order => 'updated_at desc')
13+
end
14+
15+
def destroy
16+
begin
17+
Token.transaction do
18+
@token = Token.find_by_slug!(params[:id])
19+
raise ActiveRecord::ActiveRecordError if @token.claimed?
20+
@token.destroy
21+
flash[:notice] = "Destroyed the #{params[:id]} token."
22+
end
23+
rescue ActiveRecord::ActiveRecordError
24+
flash[:error] = "Unable to destroy the #{params[:id]} token."
25+
end
26+
redirect_to '/tokens'
27+
end
28+
629
def create
730
token = Token.create!(params[:token])
831
redirect_to token_path(token)
@@ -13,6 +36,7 @@ def create
1336

1437
def show
1538
@token = Token.find_by_slug!(params[:id])
39+
@other_tokens = Token.other_tokens(@token)
1640
@new_token_request = TokenRequest.new
1741
end
1842

app/models/token.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def self.generate_slug(name)
1111
validates_presence_of :name, :slug
1212
validates_uniqueness_of :name
1313

14+
def self.other_tokens(t)
15+
Token.order('updated_at desc').where( 'tokens.id != ?', t).limit(10)
16+
end
17+
1418
def to_param
1519
slug
1620
end

app/models/token_request.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ class TokenRequest < ActiveRecord::Base
1010

1111
delegate :name, :to => :user, :prefix => true
1212

13+
after_create :update_token
14+
15+
def update_token
16+
token.touch
17+
end
18+
1319
def grant_claim
1420
unless claim_granted?
1521
update_attribute(:claim_granted_at, Time.now)

app/views/tokens/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%= link_to "Create a new token", new_token_path %>
2+
<h1>Existing tokens:</h1>
3+
<ul>
4+
<% @tokens.each do |token| %>
5+
<li><h3><%= link_to token.name, token %></h3>
6+
Last used: <%= token.updated_at %>
7+
<% if token.claimed? %>
8+
<br />Currently held by <%= token.claimed_by.name %> for <%= token.claim_purpose %>
9+
<% else %>
10+
<br /><%= link_to("Delete", token, :method => :delete, :confirm => "Are you sure? Clicking Delete will delete your token!") %>
11+
<% end %>
12+
</li>
13+
<% end %>
14+
</ul>

app/views/tokens/show.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
</fieldset>
3535
<% end %>
3636

37+
<div id="other-tokens">
38+
<h3><%= link_to "Other Recently Used Tokens", tokens_path %></h3>
39+
<ul>
40+
<% @other_tokens.each do |token| %>
41+
<li><%= link_to token.name, token %></li>
42+
<% end %>
43+
</ul>
44+
</div>
45+
3746
<% if @token.has_queue? %>
3847
<table id="token-queue">
3948
<thead>

config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Application < Rails::Application
2424

2525
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
2626
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
27-
# config.time_zone = 'Central Time (US & Canada)'
27+
config.time_zone = 'Pacific Time (US & Canada)'
2828

2929
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
3030
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]

public/stylesheets/application.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ div.field_with_errors {
5151
float: right;
5252
}
5353

54+
#other-tokens {
55+
float: right; clear: right; width: 400px; background-color: white; margin: 0 1em 1em; padding: 1.5em 1em 1em; border: 1px solid black;
56+
}
57+
5458
#token-queue {
5559
margin: 1em;
5660
border-spacing: 0;

0 commit comments

Comments
 (0)