Commit 86eea23a by Ba Toi Dang

Merge branch 'modeling-users' into 'master'

Make a basic User model (including secure passwords)

See merge request !5
parents 90d282dc 8c256716
AllCops:
# Rails特有のチェックを実行
Rails:
Enabled: true
# チェック対象外ディレクトリ
Exclude:
- 'db/**/*'
- 'config/**/*'
- 'bin/**/*'
- 'vendor/**/*'
- 'data/**/*'
- 'docker/**/*'
- 'spec/*'
- 'Guardfile'
- 'Dockerfile'
- 'docker-compose.yml'
# Ascii文字以外のコメントを許容
Style/AsciiComments:
Enabled: false
# 1行の最大文字列長を緩和
Metrics/LineLength:
Max: 160
# メソッド定義の行数を緩和
Metrics/MethodLength:
Max: 50
# Classの最大長制限
Metrics/ClassLength:
Max: 300
# top-classでのコメントを任意に
Style/Documentation:
Enabled: false
# 1行if文を使用可に
Style/IfUnlessModifier:
Enabled: false
# nestしたclass/moduleを許容
Style/ClassAndModuleChildren:
Enabled: false
# 後置rescueを許容
Style/RescueModifier:
Enabled: false
# and/orを許容
Style/AndOr:
Enabled: false
# ABCチェックを無効
Metrics/AbcSize:
Enabled: false
# ???
Style/RaiseArgs:
Enabled: false
# Hashの最後のカンマを許容
Style/TrailingCommaInLiteral:
Enabled: false
# Hashの最後のカンマを許容
Style/TrailingCommaInArguments:
Enabled: false
# コンマの後のスペース無しを許容
# hoge(1,2) => hoge(1, 2)
Style/SpaceAfterComma:
Enabled: false
# Stringのシングルクォートチェックを許容
# "aaa" -> 'aaa'
Style/StringLiterals:
Enabled: false
# コロンの後のスペースを許容
# hoge(x:1) -> hoge(x: 1)
Style/SpaceAfterColon:
Enabled: false
# コメントの後のスペース無しを許容
# コードの一時コメントアウト対策
Style/LeadingCommentSpace:
Enabled: false
# メソッドの最後のreturnを許容
Style/RedundantReturn:
Enabled: false
# 文字列Arrayを作成する場合には%w/%Wを使わないのを許容
# ["aaa", 'bbb', 'ccc'] => %w(aaa bbb ccc)
Style/WordArray:
Enabled: false
# Hashパラメータの前後の空白無しを許容
# ({1, 2}) => ( {1, 2} )
Style/BracesAroundHashParameters:
Enabled: false
# Hashのインデントに2つ以上の空白を許可
Style/IndentHash:
Enabled: false
# 複数行のメソッドチェーン?を許可
Style/MultilineOperationIndentation:
Enabled: false
# 複数のメソッドチェーンを許可
Style/MultilineBlockChain:
Enabled: false
# self.methodを許可
# selfの使用は意図的にローカル変数等との区別を行うことが多いため
Style/RedundantSelf:
Enabled: false
# {}周り
# 通常は良いが、メソッドチェーン等の一部に悪影響が出るため
# hoge.select{|v|v.fuga} => hoge.select{ |v|v.fuga }
Style/SpaceInsideBlockBraces:
Enabled: false
Style/SpaceInsideHashLiteralBraces:
Enabled: false
# オペレータの前後に空白無しを許可
# 通常は問題ないが、+=等の場合にも反応するため
# += => + =
Style/SpaceAroundOperators:
Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
Style/RedundantFreeze:
Enabled: false
EmptyLinesAroundBody:
Enabled: false
TrailingWhitespace:
Enabled: false
\ No newline at end of file
......@@ -65,3 +65,4 @@ end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'bcrypt', '~> 3.1', '>= 3.1.12'
\ No newline at end of file
......@@ -49,6 +49,7 @@ GEM
arel (9.0.0)
autoprefixer-rails (9.0.2)
execjs
bcrypt (3.1.12)
bindex (0.5.0)
bootsnap (1.3.1)
msgpack (~> 1.0)
......@@ -202,6 +203,7 @@ PLATFORMS
ruby
DEPENDENCIES
bcrypt (~> 3.1, >= 3.1.12)
bootsnap (>= 1.1.0)
bootstrap-sass (~> 3.3, >= 3.3.7)
byebug
......
class User < ApplicationRecord
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
class AddIndexToUsersEmail < ActiveRecord::Migration[5.2]
def change
add_index :users, :email, unique: true
end
end
class AddPasswordDigestToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :password_digest, :string
end
end
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_08_02_015202) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.index ["email"], name: "index_users_on_email", unique: true
end
end
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar"
end
test "should be valid" do
assert @user.valid?
end
test "name should be present" do
@user.name = " "
assert_not @user.valid?
end
test "email should be present" do
@user.email = " "
assert_not @user.valid?
end
test "name should not be too long" do
@user.name = "a" * 51
assert_not @user.valid?
end
test "email should not be too long" do
@user.name = "a" * 244 + "@example.com"
assert_not @user.valid?
end
test "email validation should accept valid addresses" do
valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
first.last@foo.jp alice+bob@baz.cn]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
test "email validation should reject invalid addresses" do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
foo@bar_baz.com foo@bar+baz.com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
end
end
test "email addresses should be unique" do
duplicate_user = @user.dup
duplicate_user.email = @user.email.upcase
@user.save
assert_not duplicate_user.valid?
end
test "email addresses should be saved as lower-case" do
mixed_case_email = "Foo@ExAMPle.CoM"
@user.email = mixed_case_email
@user.save
assert_equal mixed_case_email.downcase, @user.reload.email
end
test "password should be present (nonblank)" do
@user.password = @user.password_confirmation = " " * 6
assert_not @user.valid?
end
test "password should have a minimum length" do
@user.password = @user.password_confirmation = "a" * 5
assert_not @user.valid?
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment