Allow user to creates named auth keys with token

This commit is contained in:
Thibault Jouan
2015-05-04 04:34:13 +00:00
parent 7d1274cbb3
commit 763c9789ec
12 changed files with 174 additions and 1 deletions

View File

@@ -0,0 +1,51 @@
class KeysController < ApplicationController
before_filter :set_key, only: %i[show edit update destroy]
def index
@keys = current_user.keys
end
def show
end
def new
@key = current_user.keys.new
end
def create
@key = current_user.keys.build key_params
if @key.save
redirect_to action: :index
else
render :new
end
end
def edit
end
def update
if @key.update_attributes key_params
redirect_to action: :index
else
render :edit
end
end
def destroy
@key.destroy
redirect_to keys_path
end
private
def set_key
@key = current_user.keys.find(params[:id])
end
def key_params
params.require(:key).permit :name
end
end

7
app/models/key.rb Normal file
View File

@@ -0,0 +1,7 @@
class Key < ActiveRecord::Base
belongs_to :user
has_secure_token :token
validates :name, presence: true
end

View File

@@ -4,6 +4,7 @@ class User < ActiveRecord::Base
attr_reader :password
has_many :playlists
has_many :keys
validates :email,
presence: true,

19
app/views/keys/_form.haml Normal file
View File

@@ -0,0 +1,19 @@
= form_for @key do |f|
%fieldset
%p
= f.label :name
%br
= f.text_field :name
- if @key.errors.any?
.errors
%p
= pluralize @key.errors.count, 'error'
prohibited this post from being saved:
%ul
- @key.errors.full_messages.each do |error|
%li= error
%ul
%li
= f.button

6
app/views/keys/edit.haml Normal file
View File

@@ -0,0 +1,6 @@
%h1
Edit
key
= render 'form'

42
app/views/keys/index.haml Normal file
View File

@@ -0,0 +1,42 @@
%h1
Listing
keys
- if @keys.empty?
%p No record found.
- else
%table
%thead
%tr
%th
Name
%th
Created at
%th
Updated at
%th
Actions
%tbody
- @keys.each do |e|
%tr
%td
= link_to e.name, e
%td
= e.created_at
%td
= e.updated_at
%td
%ul
%li
= link_to 'Edit', edit_key_path(e)
%li
= link_to 'Destroy', e, method: :delete,
data: { confirm: 'Are you sure?' }
%ul
%li
= link_to 'New key', new_key_path

6
app/views/keys/new.haml Normal file
View File

@@ -0,0 +1,6 @@
%h1
New
key
= render 'form'

13
app/views/keys/show.haml Normal file
View File

@@ -0,0 +1,13 @@
%h1
Showing
key
%pre= @key.attributes.to_yaml
%ul.actions
%li
= link_to 'Edit', edit_key_path(@key)
%li
= link_to 'Destroy', e, method: :delete, data: { confirm: 'Are you sure?' }

View File

@@ -12,6 +12,8 @@
- if current_user?
%nav
%ul
%li
= link_to 'Keys', keys_path
%li
= link_to 'Playlists', playlists_path
%li