Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit f207685

Browse files
author
bgrove1974
committed
Generate UUID when user is created
1 parent 2c216c3 commit f207685

File tree

8 files changed

+26
-4
lines changed

8 files changed

+26
-4
lines changed

app/controllers/v1/users_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def index
1313
def show
1414
@user = User.find(params[:id])
1515

16-
render json: @user, only: [:id]
16+
render json: @user, only: [:id, :token]
1717
# render json: User.find(params[:id]), only: [:id]
1818
# render json: @user.id
1919
end
@@ -69,7 +69,7 @@ def issues
6969
#Require strong_params/replace attr_accessible
7070
private
7171
def user_params
72-
params.require(:id, :address1, :address2, :city, :state, :zipcode).permit(:id, :address1, :address2, :city, :state, :zipcode)
72+
params.require(:id, :address1, :address2, :city, :state, :zipcode, :token).permit(:id, :address1, :address2, :city, :state, :zipcode, :token)
7373
end
7474
end
7575
end

app/models/concerns/tokenable.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Tokenable
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
before_create :generate_token
6+
end
7+
8+
protected
9+
10+
def generate_token
11+
self.token = loop do
12+
random_token = SecureRandom.uuid
13+
break random_token unless self.class.exists?(token: random_token)
14+
end
15+
end
16+
end

app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class User < ActiveRecord::Base
2+
include Tokenable
23
end

app/serializers/user_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class UserSerializer < ActiveModel::Serializer
2-
attributes :id, :address1, :address2, :city, :state, :zipcode
2+
attributes :id, :address1, :address2, :city, :state, :zipcode, :token
33
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
2+
resources :users, except: [:new, :edit]
23
api_version(:module => "V1", :path => {:value => "v1"}, :default => true) do
34

45
get 'users/index' => 'users#index'

db/migrate/20140802070454_create_users.rb renamed to db/migrate/20140817074932_create_users.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def change
66
t.string :city
77
t.string :state
88
t.integer :zipcode
9+
t.string :token
910

1011
t.timestamps
1112
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20140810072749) do
14+
ActiveRecord::Schema.define(version: 20140817074932) do
1515

1616
create_table "candidates", force: true do |t|
1717
t.string "name"
@@ -41,6 +41,7 @@
4141
t.string "city"
4242
t.string "state"
4343
t.integer "zipcode"
44+
t.string "token"
4445
t.datetime "created_at"
4546
t.datetime "updated_at"
4647
end

test/fixtures/users.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ one:
66
city: MyString
77
state: MyString
88
zipcode: 1
9+
token: MyString
910

1011
two:
1112
address1: MyString
1213
address2: MyString
1314
city: MyString
1415
state: MyString
1516
zipcode: 1
17+
token: MyString

0 commit comments

Comments
 (0)