Merge branch 'users-keys-token'

This commit is contained in:
Thibault Jouan 2015-05-05 02:32:04 +00:00
commit 16a377566a
14 changed files with 179 additions and 1 deletions

View File

@ -5,6 +5,8 @@ gem 'pg'
gem 'bcrypt'
gem 'unicorn-rails'
gem 'has_secure_token'
gem 'haml'
gem 'rabl'

View File

@ -90,6 +90,8 @@ GEM
spork (>= 0.8.4)
haml (4.0.6)
tilt
has_secure_token (1.0.0)
activerecord (>= 3.0)
hitimes (1.2.2)
i18n (0.7.0)
jquery-rails (4.0.3)
@ -226,6 +228,7 @@ DEPENDENCIES
guard-rspec
guard-spork
haml
has_secure_token
jquery-rails
pg
pry-rails

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

View File

@ -22,5 +22,7 @@ Rails.application.routes.draw do
get 'download', on: :member
end
resources :keys
resources :users, only: %i[new create]
end

View File

@ -0,0 +1,12 @@
class CreateKeys < ActiveRecord::Migration
def change
create_table :keys do |t|
t.timestamps null: false
t.references :user, index: true, foreign_key: true, null: false
t.string :name
t.string :token
end
add_index :keys, :token, unique: true
end
end

View File

@ -11,11 +11,22 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20110916003929) do
ActiveRecord::Schema.define(version: 20150504043016) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "keys", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id", null: false
t.string "name"
t.string "token"
end
add_index "keys", ["token"], name: "index_keys_on_token", unique: true, using: :btree
add_index "keys", ["user_id"], name: "index_keys_on_user_id", using: :btree
create_table "playlists", force: :cascade do |t|
t.integer "user_id"
t.string "name"
@ -56,4 +67,5 @@ ActiveRecord::Schema.define(version: 20110916003929) do
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_foreign_key "keys", "users"
end