-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Generated user model * User email validations & validate_lengths_from_database * Update database.yml * User specs & fix rubocop
- Loading branch information
Showing
9 changed files
with
112 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class User < ApplicationRecord | ||
serialize :options, JSON | ||
|
||
validates_lengths_from_database | ||
validates :email, email: true, uniqueness: { case_sensitive: false }, allow_blank: true | ||
validates :level, numericality: { greater_than_or_equal_to: 0 } | ||
validates :uid, presence: true, uniqueness: { case_sensitive: false } | ||
|
||
attr_readonly :uid, :email | ||
|
||
def email=(value) | ||
super value.try(:downcase) | ||
end | ||
|
||
def uid=(value) | ||
super value.try(:upcase) | ||
end | ||
|
||
def state | ||
super.inquiry | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateUsers < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :users do |t| | ||
t.column :email, :string, limit: 255, null: false, index: { unique: true } | ||
t.column :uid, :string, limit: 14, null: false, index: { unique: true } | ||
t.column :level, :integer, limit: 1, null: false, default: 0 | ||
t.column :state, :string, limit: 30, null: false, default: 'pending', index: true | ||
t.column :options, :string, limit: 1000, null: false, default: '{}' | ||
t.timestamps null: false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :user do | ||
email { Faker::Internet.email } | ||
level 0 | ||
state 'active' | ||
uid { Faker::Internet.password(14, 14) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe User do | ||
let(:user) { create(:user) } | ||
|
||
it 'ignores new values for email' do | ||
previous_value = user.email | ||
user.update!(email: 'new@gmail.com') | ||
expect(user.reload.email).to eq previous_value | ||
end | ||
|
||
it 'ignores new values for uid' do | ||
previous_value = user.uid | ||
user.update!(uid: '1234567890') | ||
expect(user.reload.uid).to eq previous_value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters