From f6d1201c6e463380fad2f7bd219e1d82c6fd18ea Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Fri, 18 May 2018 19:27:08 +0300 Subject: [PATCH 1/9] Add user --- app/models/user.rb | 2 ++ db/migrate/20180518161358_create_users.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20180518161358_create_users.rb diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..379658a --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ApplicationRecord +end diff --git a/db/migrate/20180518161358_create_users.rb b/db/migrate/20180518161358_create_users.rb new file mode 100644 index 0000000..64a5914 --- /dev/null +++ b/db/migrate/20180518161358_create_users.rb @@ -0,0 +1,12 @@ +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 From 2622c7a670d03c5e6aa69a01ea4989b739cba2a5 Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Mon, 21 May 2018 13:33:58 +0300 Subject: [PATCH 2/9] validate_lengths_from_database --- Gemfile | 22 ++++++++++++---------- Gemfile.lock | 3 +++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 7643280..1886f7e 100644 --- a/Gemfile +++ b/Gemfile @@ -2,16 +2,18 @@ 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 '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 'pry-byebug', '~> 3.5' diff --git a/Gemfile.lock b/Gemfile.lock index 51fa34a..4fe6360 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -196,6 +196,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) @@ -223,6 +225,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 From 8684a47f6649054c6dbf994ac473c635ab1444fe Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Mon, 21 May 2018 16:03:26 +0300 Subject: [PATCH 3/9] User modle validations and uid= email= methods --- Gemfile | 1 - app/models/user.rb | 22 ++++++++++++++++++++++ db/migrate/20180518161358_create_users.rb | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 1886f7e..f91cc48 100644 --- a/Gemfile +++ b/Gemfile @@ -14,7 +14,6 @@ gem 'sass-rails', '~> 5.0' gem 'uglifier', '~> 4.1' gem 'validates_lengths_from_database', '~> 0.7.0' - group :development, :test do gem 'pry-byebug', '~> 3.5' end diff --git a/app/models/user.rb b/app/models/user.rb index 379658a..ccdff00 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +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 diff --git a/db/migrate/20180518161358_create_users.rb b/db/migrate/20180518161358_create_users.rb index 64a5914..7ecdffa 100644 --- a/db/migrate/20180518161358_create_users.rb +++ b/db/migrate/20180518161358_create_users.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateUsers < ActiveRecord::Migration[5.1] def change create_table :users do |t| From 997e90f42d44b221557f956ba439a5961c98269c Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Mon, 21 May 2018 16:06:39 +0300 Subject: [PATCH 4/9] Update database.yml & schema --- config/database.yml | 19 +++++++++---------- db/schema.rb | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/config/database.yml b/config/database.yml index 0146496..1742fdd 100644 --- a/config/database.yml +++ b/config/database.yml @@ -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['DATABASE_USER'] %> + password: <%= ENV['DATABASE_PASS'] %> development: <<: *default @@ -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'] %> diff --git a/db/schema.rb b/db/schema.rb index 52ab025..c57585f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 From 44ccba660337cb6cfe84c309beda9a166e00a971 Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Mon, 21 May 2018 16:08:18 +0300 Subject: [PATCH 5/9] Add email validator --- Gemfile | 1 + Gemfile.lock | 3 +++ config/database.yml | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index f91cc48..38ee63a 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ source 'https://rubygems.org' gem 'bunny', '~> 2.9', require: false gem 'figaro', '~> 1.1' +gem 'email_validator', '~> 1.6' gem 'jwt-multisig', '~> 1.0' gem 'memoist', '~> 0.16' gem 'mini_racer', '~> 0.1', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 4fe6360..c849652 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -208,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) diff --git a/config/database.yml b/config/database.yml index 1742fdd..c661f68 100644 --- a/config/database.yml +++ b/config/database.yml @@ -4,8 +4,8 @@ default: &default collation: utf8_general_ci pool: 5 host: <%= ENV['DATABASE_HOST'] %> - username: <%= ENV['DATABASE_USER'] %> - password: <%= ENV['DATABASE_PASS'] %> + username: <%= ENV.fetch('DATABASE_USER', 'root') %> + password: <%= ENV.fetch('DATABASE_PASSWORD', '') %> development: <<: *default From 59424fb04a75e337eda494820041b41cfa352710 Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Mon, 21 May 2018 16:50:14 +0300 Subject: [PATCH 6/9] User specs --- spec/factories/user.rb | 10 ++++++++++ spec/models/user_spec.rb | 19 +++++++++++++++++++ spec/rails_helper.rb | 4 +++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 spec/factories/user.rb create mode 100644 spec/models/user_spec.rb diff --git a/spec/factories/user.rb b/spec/factories/user.rb new file mode 100644 index 0000000..becd1c6 --- /dev/null +++ b/spec/factories/user.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 0000000..926fe9b --- /dev/null +++ b/spec/models/user_spec.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 169ccd5..08dde14 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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. @@ -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 From 22749810d9418c3b633719029bb8962c8b30406e Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Mon, 21 May 2018 16:51:05 +0300 Subject: [PATCH 7/9] Fix rubocop --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 38ee63a..d91d9ff 100644 --- a/Gemfile +++ b/Gemfile @@ -3,8 +3,8 @@ source 'https://rubygems.org' gem 'bunny', '~> 2.9', require: false -gem 'figaro', '~> 1.1' 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 From 36ad01526811c358b51f14ba626c1480a5cb7915 Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Mon, 21 May 2018 17:04:44 +0300 Subject: [PATCH 8/9] faker to dev group --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index d91d9ff..aa6ffdd 100644 --- a/Gemfile +++ b/Gemfile @@ -16,12 +16,12 @@ 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' From 6e71642f0967d73d4aa3edcc20fa74b453c2d111 Mon Sep 17 00:00:00 2001 From: Yaroslav Savchuk Date: Mon, 21 May 2018 17:11:56 +0300 Subject: [PATCH 9/9] Allign --- db/migrate/20180518161358_create_users.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/db/migrate/20180518161358_create_users.rb b/db/migrate/20180518161358_create_users.rb index 7ecdffa..eb23a5e 100644 --- a/db/migrate/20180518161358_create_users.rb +++ b/db/migrate/20180518161358_create_users.rb @@ -3,12 +3,12 @@ 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 + 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