Skip to content

Commit

Permalink
Added base user model & specs (closes #13) (#14)
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
ysv authored May 21, 2018
1 parent 98c7d49 commit c71bf53
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 23 deletions.
24 changes: 13 additions & 11 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

source 'https://rubygems.org'

gem 'bunny', '~> 2.9', require: false
gem 'figaro', '~> 1.1'
gem 'jwt-multisig', '~> 1.0'
gem 'memoist', '~> 0.16'
gem 'mini_racer', '~> 0.1', require: false
gem 'mysql2', '>= 0.3.18', '< 0.5'
gem 'puma', '~> 3.7'
gem 'rails', '~> 5.1.5'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '~> 4.1'
gem 'bunny', '~> 2.9', require: false
gem 'email_validator', '~> 1.6'
gem 'figaro', '~> 1.1'
gem 'jwt-multisig', '~> 1.0'
gem 'memoist', '~> 0.16'
gem 'mini_racer', '~> 0.1', require: false
gem 'mysql2', '>= 0.3.18', '< 0.5'
gem 'puma', '~> 3.7'
gem 'rails', '~> 5.1.5'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '~> 4.1'
gem 'validates_lengths_from_database', '~> 0.7.0'

group :development, :test do
gem 'faker', '~> 1.8'
gem 'pry-byebug', '~> 3.5'
end

group :test do
gem 'factory_bot_rails', '~> 4.8'
gem 'faker', '~> 1.8'
gem 'rspec-rails', '~> 3.7'
gem 'rubocop', '~> 0.55', require: false
gem 'shoulda-matchers', '~> 3.1'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ GEM
crass (1.0.4)
diff-lcs (1.3)
docile (1.1.5)
email_validator (1.6.0)
activemodel
erubi (1.7.1)
execjs (2.7.0)
factory_bot (4.8.2)
Expand Down Expand Up @@ -196,6 +198,8 @@ GEM
uglifier (4.1.10)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.3.2)
validates_lengths_from_database (0.7.0)
activerecord (>= 3)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand All @@ -206,6 +210,7 @@ PLATFORMS
DEPENDENCIES
annotate (~> 2.7)
bunny (~> 2.9)
email_validator (~> 1.6)
factory_bot_rails (~> 4.8)
faker (~> 1.8)
figaro (~> 1.1)
Expand All @@ -223,6 +228,7 @@ DEPENDENCIES
shoulda-matchers (~> 3.1)
simplecov (= 0.12.0)
uglifier (~> 4.1)
validates_lengths_from_database (~> 0.7.0)

BUNDLED WITH
1.16.1
24 changes: 24 additions & 0 deletions app/models/user.rb
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
19 changes: 9 additions & 10 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
adapter: mysql2
encoding: utf8
collation: utf8_general_ci
pool: 5
host: <%= ENV['DATABASE_HOST'] %>
username: <%= ENV.fetch('DATABASE_USER', 'root') %>
password: <%= ENV.fetch('DATABASE_PASSWORD', '') %>

development:
<<: *default
Expand All @@ -15,9 +17,6 @@ test:

production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
pool: 10
url: <%= ENV['DATABASE_URL'] %>
pool: 10
database: applogic_production
host: <%= ENV['DATABASE_HOST'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASS'] %>
14 changes: 14 additions & 0 deletions db/migrate/20180518161358_create_users.rb
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
15 changes: 14 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 0) do
ActiveRecord::Schema.define(version: 20180518161358) do

create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "email", null: false
t.string "uid", limit: 14, null: false
t.integer "level", limit: 1, default: 0, null: false
t.string "state", limit: 30, default: "pending", null: false
t.string "options", limit: 1000, default: "{}", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["state"], name: "index_users_on_state"
t.index ["uid"], name: "index_users_on_uid", unique: true
end

end
10 changes: 10 additions & 0 deletions spec/factories/user.rb
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
19 changes: 19 additions & 0 deletions spec/models/user_spec.rb
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
4 changes: 3 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
Expand Down Expand Up @@ -56,4 +56,6 @@
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
config.include FactoryBot::Syntax::Methods
end

0 comments on commit c71bf53

Please sign in to comment.