Skip to content

Commit 4bec0b5

Browse files
committed
Introduce role column on User
1 parent 15b648d commit 4bec0b5

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ class User < ActiveRecord::Base
55
:recoverable, :rememberable, :trackable, :validatable,
66
:confirmable, :lockable
77

8+
ROLES = ["standard", "premium"].freeze
9+
10+
validates_inclusion_of :role, in: ROLES
11+
812
has_many :access_tokens, dependent: :destroy
913

1014
def issue_access_token
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddRoleToUsers < ActiveRecord::Migration
2+
def change
3+
add_column :users, :role, :string, default: "standard", null: false
4+
end
5+
end

db/schema.rb

Lines changed: 6 additions & 5 deletions
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: 20151024100339) do
14+
ActiveRecord::Schema.define(version: 20151206175412) do
1515

1616
create_table "access_tokens", force: :cascade do |t|
1717
t.string "locator", null: false
@@ -47,12 +47,12 @@
4747
add_index "subscriptions", ["email"], name: "index_subscriptions_on_email", unique: true
4848

4949
create_table "users", force: :cascade do |t|
50-
t.string "email", default: "", null: false
51-
t.string "encrypted_password", default: "", null: false
50+
t.string "email", default: "", null: false
51+
t.string "encrypted_password", default: "", null: false
5252
t.string "reset_password_token"
5353
t.datetime "reset_password_sent_at"
5454
t.datetime "remember_created_at"
55-
t.integer "sign_in_count", default: 0, null: false
55+
t.integer "sign_in_count", default: 0, null: false
5656
t.datetime "current_sign_in_at"
5757
t.datetime "last_sign_in_at"
5858
t.string "current_sign_in_ip"
@@ -61,11 +61,12 @@
6161
t.datetime "confirmed_at"
6262
t.datetime "confirmation_sent_at"
6363
t.string "unconfirmed_email"
64-
t.integer "failed_attempts", default: 0, null: false
64+
t.integer "failed_attempts", default: 0, null: false
6565
t.string "unlock_token"
6666
t.datetime "locked_at"
6767
t.datetime "created_at"
6868
t.datetime "updated_at"
69+
t.string "role", default: "standard", null: false
6970
end
7071

7172
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true

spec/models/user_spec.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
describe User do
44

5-
context 'validations' do
5+
describe 'validations' do
66
it { should validate_presence_of :email }
77
it { should validate_presence_of :password }
88
it { should validate_confirmation_of :password }
99
it { should validate_uniqueness_of(:email).case_insensitive }
10+
it { should validate_inclusion_of(:role).in_array(["standard", "premium"]) }
1011
end
1112

12-
context 'associations' do
13+
describe 'associations' do
1314
it { should have_many(:access_tokens).dependent(:destroy) }
1415
end
1516

16-
context '#issue_access_token' do
17+
describe '#issue_access_token' do
1718
it 'creates access token belonging to user' do
1819
user = create(:user)
1920

@@ -26,4 +27,9 @@
2627
end
2728
end
2829

30+
describe '#role' do
31+
it 'defaults to standard' do
32+
expect(User.new.role).to eq "standard"
33+
end
34+
end
2935
end

0 commit comments

Comments
 (0)