Skip to content

#1 ユーザー登録の実装#6

Open
toyokappa wants to merge 8 commits intomasterfrom
signup-users
Open

#1 ユーザー登録の実装#6
toyokappa wants to merge 8 commits intomasterfrom
signup-users

Conversation

@toyokappa
Copy link
Owner

fix #1

Gemfile Outdated


gem 'rails', '~> 5.1.2'
gem 'bcrypt', '3.1.11'

Choose a reason for hiding this comment

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

このあたりを読んでバージョン指定の仕方を知ると良いかも。
http://qiita.com/awakia/items/5745938c192ca1139c63#gemfile%E3%81%A7%E3%81%AEversion%E6%8C%87%E5%AE%9A%E3%81%AE%E4%BB%95%E6%96%B9

Gemfile Outdated
gem 'uglifier', '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.7.0'

Choose a reason for hiding this comment

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

依存解決のためにbundlerを使っているので、ここまでバージョンを決め打ちする必要はありません。
OSSのGemfileなどを参考にしてみてください。

~> 5.1.2 と書くと、 5.1.x 系の例えば、マイナーバージョンアップされた、 5.1.3 などがリリースされたとき、それがインストールされます。bundlerの取り扱いは、基本的な知識なので理解しないと厳しそうです。

@yhirano55
Copy link

commitの問題を指摘します。趣味ならば何でも良いと思うのですが、一応、業務想定としてマジレスしておきます。おそらく業務であれば、研修でない限り、通過しないPRになっています。

  1. Commitは、適切な粒度でまとめる必要があります。最初はわかりにくいと思うので、以下のルールで対応するとよいかもしれません。
  • 実装とテストを対にする( app/models/user.rbtest/models/user_test.rb を対にする)
  • Model, Controller, Viewを混ぜずに分ける(configも分ける)
  • そのPRに関係のないCommitは極力積まない
  1. Commitメッセージは通常は、日本語ではなく、英語を利用します。

なお、Gitの使い方に慣れていないと思うので、まずそれを慣れた方がよさそうです。

  1. Commitする
  2. Pushする

これらはできると思うのですが、それに加えて下記を覚えないと厳しそうです。

  1. Commitしたけれど、それをresetして作り直す( git reset
  2. 直前のCommitのメッセージを修正する( git commit --am
  3. Commitの順序を入れ替える (git rebase)
  4. Commit AとBを統合する(git rebase

t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email"
end

Choose a reason for hiding this comment

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

modelでuniquenessを保証するのであれば、通常は、table定義時でもそれを行う必要があります。

@yhirano55
Copy link

  • ローカルのサーバーを起動して動作すること (bin/rails server
  • テストが通ること(bin/rails test

この2つは、reviewしてもらうときの前提なので、必ず確認するようにしましょう。

@toyokappa toyokappa force-pushed the signup-users branch 2 times, most recently from f2ca6f3 to 1f8bff6 Compare July 5, 2017 21:46
gem 'sqlite3', '1.3.11'
gem 'byebug', '9.0.0', platform: :mri
gem 'sqlite3'
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]

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]

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

t.string :email
t.string :password_digest

t.timestamps

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で行います。ユニークであることもここで保証できます。

@@ -0,0 +1,15 @@
class User < ApplicationRecord
before_save { email.downcase! }

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 と書く方が一般的です。

end

test "ユーザー名:存在性" do
@user.user_name = " "

Choose a reason for hiding this comment

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

モデルの名前が User なので、その名前を表す属性は name でよいのでは?(とここで思いました)

get '/contact', to: 'static_pages#contact'
get '/signup' , to: 'users#new'
post '/signup', to: 'users#create'
resources :users

Choose a reason for hiding this comment

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

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

assert_select "a[href=?]", root_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
assert_select "a[href=?]", signup_path, count: 2

Choose a reason for hiding this comment

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

capybaraを使った方が楽そうです。

<% end %>
</ul>
</div>
<% end %>

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 といった感じで渡して、汎用的にした方がよいかなと思いました。

<%= form_for(@user, url: signup_path) do |f| %>
<%= render "shared/error_messages" %>

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

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 %> だけで十分なので、覚えてみてもよいかもしれませんね。

<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= gravatar_for @user, size: 180 %>

Choose a reason for hiding this comment

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

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


private

#ストロングパラメーター

Choose a reason for hiding this comment

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

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

assert_template "users/show"
assert_not flash.empty?
assert_match "ユーザー登録が完了しました!", response.body
end

Choose a reason for hiding this comment

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

内容的には、コントローラのテストっぽいので、コントローラでテストしてもよさそう。

@yhirano55
Copy link

schema.rb を手で編集している部分を、migrationで表現して、それ以外は特に問題ないです。

以降の方針として、今回の目的をアプリケーションを作ることに焦点をあてて、テストコードは一切書かずに モンキーテストのみ でもよいかなと思いました(もちろん、どういうやり方をするかはおまかせします)テストは今回の要件に含めずに、 RSpecやCapybaraと共に学習してみる方が効率良いかなと思いました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ユーザー登録の実装

2 participants