Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@ end



gem 'rails', '~> 5.1.2'
gem 'rails', '~> 5.1.2'
gem 'bcrypt', '~> 3.1'
gem 'bootstrap-sass'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'

group :development, :test do
gem 'sqlite3', '1.3.11'
gem 'byebug', '9.0.0', platform: :mri
gem 'sqlite3'
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正不要ですが、このオプション指定は下記との勘違い?(あ、でもjrubyがないか)

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あーすみませんデフォルトですね。失礼。

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

end

group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
gem 'rails-controller-testing', '0.1.1'
gem 'minitest-reporters', '1.1.9'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
gem 'rails-controller-testing'
gem 'minitest-reporters'
gem 'guard'
gem 'guard-minitest'
end

group :production do
Expand Down
14 changes: 8 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ GEM
arel (8.0.0)
autoprefixer-rails (7.1.1.2)
execjs
bcrypt (3.1.11)
bindex (0.5.0)
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
Expand Down Expand Up @@ -195,22 +196,23 @@ PLATFORMS
ruby

DEPENDENCIES
bcrypt (~> 3.1)
bootstrap-sass
byebug (= 9.0.0)
byebug
coffee-rails (~> 4.2)
guard (= 2.13.0)
guard-minitest (= 2.4.4)
guard
guard-minitest
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
minitest-reporters (= 1.1.9)
minitest-reporters
pg (= 0.18.4)
puma (~> 3.7)
rails (~> 5.1.2)
rails-controller-testing (= 0.1.1)
rails-controller-testing
sass-rails (~> 5.0)
spring
spring-watcher-listen (~> 2.0.0)
sqlite3 (= 1.3.11)
sqlite3
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/users.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
64 changes: 64 additions & 0 deletions app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ body {
text-align: center;
}

/* mixin, valuables, etc */
@mixin box_sizing {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

.debug_dump {
clear: both;
float: left;
width: 100%;
margin-top: 45px;
@include box_sizing;
}

/* Typography */

body {
Expand All @@ -23,11 +38,13 @@ h1, h2, h3, h4, h5, h6 {

h1 {
font-size: 36px;
text-align: center;
margin-bottom: 30px;
}

h2 {
font-size: 24px;
text-align: center;
margin-bottom: 20px;
}

Expand Down Expand Up @@ -78,7 +95,23 @@ header {

/* Wrapper */

/* Aside */

aside {
section.user_info {
text-align: center;
}
section{
.gravatar {
margin-bottom: 10px;
border-radius: 100%;
}
.user_name{
font-size: 20px;
font-weight: bold;
}
}
}

/* Footer */

Expand All @@ -88,6 +121,7 @@ footer {
color: #fff;
font-size: 13px;
padding: 30px 0px;
margin-top: 50px;
font-size: 14px;
.nav-menu {
text-align: center;
Expand Down Expand Up @@ -122,3 +156,33 @@ footer {
text-align: center;
}
}

/* Form */

input, textarea, select, .uneditable-input {
font-size: 18px;
border: 1px solid #bbb;
border-radius: 3px;
width: 100%;
margin-bottom: 30px;
@include box_sizing;
}

input {
height: auto !important;
}

.error_explanation {
color: red;
ul {
color: red;
margin-bottom: 30px;
}
}

.field_with_errors {
@extend .has-error;
.form-control {
color: $state-danger-text;
}
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/users.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
29 changes: 29 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class UsersController < ApplicationController
def new
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
#登録成功時のアクション
flash[:success] = "ユーザー登録が完了しました!"
redirect_to @user
else
#登録失敗時のアクション
render "new"
end
end

def show
@user = User.find(params[:id])
end

private

#ストロングパラメーター
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分用かもしれませんが、上のコメントもそうですが、コメントも保守の対象なので、何を残すかは精査したいところですね(修正不要です)

def user_params
params.require(:user).permit(:user_name, :email, :password,
:password_confirmation)
end
end
9 changes: 9 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module UsersHelper

#ユーザーからGravatarを返す
def gravatar_for(user, size: 80)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.user_name, class: "gravatar")
end
end
15 changes: 15 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class User < ApplicationRecord
before_save { email.downcase! }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • validationを通過した後なので、save前にdowncaseする必要はなさそう。
  • もしもdowncaseを強制したいなら、before_validation あたりでもよさそう。
  • 値を破壊的変更をする email.downcase! よりも self.email = email.downcase と書く方が一般的です。

VALID_USER_NAME_REGEX = /\A(\w+)\z/i
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :user_name, presence: true,
length: { maximum: 30 },
uniqueness: { case_sensitive: false},
format: { with: VALID_USER_NAME_REGEX }
validates :email, presence: true,
length: { maximum: 255 },
uniqueness: { case_sensitive: false},
format: { with: VALID_EMAIL_REGEX }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
2 changes: 1 addition & 1 deletion app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= link_to "Knowledge Stocker", root_path, id: "logo" %>
<nav>
<ul class="nav-menu">
<li><%= link_to "新規登録", "#" %></li>
<li><%= link_to "新規登録", signup_path %></li>
<li><%= link_to "ログイン", "#" %></li>
</ul>
</nav>
Expand Down
4 changes: 4 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
<body>
<%= render "layouts/header" %>
<div class="container">
<% flash.each do |msg_type, msg| %>
<%= content_tag(:div, msg, class: "alert alert-#{msg_type}") %>
<% end %>
<%= yield %>
</div>
<%= render "layouts/footer" %>
<%= debug(params) if Rails.env.development? %>
</body>
</html>
12 changes: 12 additions & 0 deletions app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% if @user.errors.any? %>
<div class="error_explanation">
<div class="alert alert-danger">
<%= "入力エラー:#{@user.errors.count}項目" %>
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正するまでもないですが、パーシャルから、アサインされた変数を参照する場合、直接インスタンス変数を呼ばずに、 render "shared/error_messages", user: @user といった感じで、必要な変数をbindして、渡す方が望ましいです。なお、 shared なパーシャルであれば、 render "shared/error_message", resource: @user といった感じで渡して、汎用的にした方がよいかなと思いました。

2 changes: 1 addition & 1 deletion app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="center jumbotron">
<h1>Knowledge Stocker</h1>
<h2>効率的かつ意欲的な学習体験を。</h2>
<%= link_to "新規登録", "#", class: "btn btn-lg btn-primary" %>
<%= link_to "新規登録", signup_path, class: "btn btn-lg btn-primary" %>
</div>
24 changes: 24 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%= provide(:title, "新規登録") %>
<h1>新規登録</h1>

<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: signup_path) do |f| %>
<%= render "shared/error_messages" %>

<%= f.label :user_name, "ユーザー名" %>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18nを設定すれば、 <%= f.label :user_name %> だけで十分なので、覚えてみてもよいかもしれませんね。

<%= f.text_field :user_name, class: "form_control" %>

<%= f.label :email, "メールアドレス" %>
<%= f.email_field :email, class: "form_control" %>

<%= f.label :password, "パスワード" %>
<%= f.password_field :password, class: "form_control" %>

<%= f.label :password_confirmation, "パスワード(確認)" %>
<%= f.password_field :password_confirmation, class: "form_control" %>

<%= f.submit "登録する", class: "btn btn-primary" %>
<% end %>
</div>
</div>
10 changes: 10 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% provide(:title, @user.user_name) %>
<h1>ユーザー情報</h1>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= gravatar_for @user, size: 180 %>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • provideもgravatar_forも、メソッドの名前から何を返すのか想像できませんでした。修正不要ですが、実際のチーム作業のときは恐らくNGになるかと思います。
  • 理想を言うと、コードを読まなくてもメソッド名を見て、どういう結果が返されるのかが分かること、となります。

<div class="user_name"><%= @user.user_name %></div>
</section>
</aside>
</div>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
root 'static_pages#home'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup' , to: 'users#new'
post '/signup', to: 'users#create'
resources :users
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しなくてもよいのですが、routingは、controllerが実装されてはじめて使えるようになるので、コミット順序からしたら、コントローラの実装の後でよいかと思います。

end
11 changes: 11 additions & 0 deletions db/migrate/20170705223629_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
t.string :user_name
t.string :email
t.string :password_digest

t.timestamps
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • テーブルの作成・更新・削除など、DBに関する変更は、すべてmigrationに書きます。
  • schema.rb は、 rails db:migrate すると自動生成されるので、手作業で修正してはなりません。
  • Modelに presence: true と必須であることを保証する場合は、 テーブル定義でも t.string :user_name, null: false と「Nullを許容しませんよ」という定義が必要です。
  • インデックスを張るときも、migrationで行います。ユニークであることもここで保証できます。

end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20170706211140_add_index_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddIndexToUsers < ActiveRecord::Migration[5.1]
def change
add_index :users, :user_name, unique: true
add_index :users, :email, unique: true
end
end
25 changes: 25 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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: 20170706211140) do

create_table "users", force: :cascade do |t|
t.string "user_name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["user_name"], name: "index_users_on_user_name", unique: true
end

end
9 changes: 9 additions & 0 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get signup_path
assert_response :success
end

end
Empty file added test/fixtures/users.yml
Empty file.
Loading