Skip to content
This repository was archived by the owner on May 31, 2019. It is now read-only.

masterブランチのコードレビューをお願いします。#1

Open
bokusunny wants to merge 16 commits intogroup-chat-testfrom
master
Open

masterブランチのコードレビューをお願いします。#1
bokusunny wants to merge 16 commits intogroup-chat-testfrom
master

Conversation

@bokusunny
Copy link
Owner

既にmasterに全てマージされている為、masterブランチのコードレビューをお願いします。(merge先のブランチはかなり前のやつで関係ないです)

アプリ概要
主な機能:
メール認証によるユーザー登録機能
ログイン/ログアウト機能
登録情報の変更機能
1対1でのチャット機能(メイン)

参考にしたサイト:
Deviseの導入:https://qiita.com/cigalecigales/items/f4274088f20832252374
ActionCableの導入:https://qiita.com/jnchito/items/aec75fab42804287d71b
1対1チャット機能の実装:https://qiita.com/YN6127yn/items/7ddd966141cca195b4da
DB設計:https://railsguides.jp/association_basics.html

Copy link

@prgyukke prgyukke left a comment

Choose a reason for hiding this comment

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

主に細かい文法面と可読性についてレビューしました。


speak:(message) ->
@perform 'speak', message: message
to_id =$('input:hidden[name="to_id"]').val()
Copy link

Choose a reason for hiding this comment

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

= の後ろのスペースをあけるべきかと思います

speak:(message) ->
@perform 'speak', message: message
to_id =$('input:hidden[name="to_id"]').val()
@perform 'speak', {message: message,to_id:to_id}
Copy link

Choose a reason for hiding this comment

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

カンマとto_id:の後ろにスペースをあけるべきかと思います。


def speak(data)
Message.create! content: data['message'], user_id: current_user.id
user=User.find_by(id: data['to_id'])
Copy link

Choose a reason for hiding this comment

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

=の前後はスペースをあけましょう

Copy link

Choose a reason for hiding this comment

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

idで検索するならfind(data[‘to_id’])の方が良いかな、という気がします。
find_byだと、対象のレコードがない時にnilを返すので、この先でnull参照して落ちます。

findの場合、RecordNotFoundを発生します。


def message_room_id(first_user, second_user)
first_id = first_user.id.to_i
second_id = second_user.id.to_i
Copy link

Choose a reason for hiding this comment

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

19-20行で=位置を揃えると可読性が上がります。

first_id  = first_user.id.to_i
second_id = second_user.id.to_i

@messages = Message.all
@user=User.find_by(id: params[:id])
@room_id = message_room_id(current_user, @user)
@messages = Message.where(room_id: @room_id)
Copy link

Choose a reason for hiding this comment

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

@user後の=前後にはスペースをあけましょう
また、ここの3行でも=位置を揃えましょう

Copy link

Choose a reason for hiding this comment

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

view側の実装を見ていないのでなんとも言えないですが、
room_idをview側で使うことがないのではないかな? 🤔 と思ったので、ローカル変数でいい気がしてきました。

あと、
messageは時系列順に並んで欲しい気がするので、orderを明示的に指定してあげたほうがいいと思います!
多分よく使うと思うので、
継承元のApplicationRecordscopeを定義しておくと幸せなのかな、と思いました!

<input type="hidden" name="to_id" value="<%= @user.id %>">
<% if current_user.id==@user.id %>
<label>It's your space. Write down whatever you like here! :</label><br>
<% else%>
Copy link

Choose a reason for hiding this comment

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

おしりの%前にスペースを入れましょう

<% if current_user.id==@user.id %>
<label>It's your space. Write down whatever you like here! :</label><br>
<% else%>
<label>Send messages to <%= @user.username%>:</label><br>
Copy link

Choose a reason for hiding this comment

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

おしりの%前にスペースを入れましょう

<div class="users-index-item">
<div class="user-right">
<p><%= link_to user.username,root_url%></p>
<p><%= link_to user.username,"/rooms/#{user.id}"%></p>
Copy link

Choose a reason for hiding this comment

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

カンマの後とおしりの%前にスペースを入れましょう

t.integer "user_id"
t.integer "from_id"
t.integer "to_id"
t.string "room_id"
Copy link

Choose a reason for hiding this comment

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

カラム名の位置を揃えると可読性が上がります。

# by default. You can change it below and use your own secret key.
# config.secret_key = '36225be56b61efa00ddd192276401308319f57986469c4f528653eda4f44aed253fc887a07711360b0b3da4e641427cf3e02bd71350b62c10b83407f6acd7d1d'

config.secret_key = '6ff05a2a5b47194687fe711bcec8a4dfed7f7350c2ab7842cb7cfe8ce0bf52b259b2b72f7635341c80f6437df9fe39b0e5f75a4bbce788bc249a5b6ce1de2f38'
Copy link

Choose a reason for hiding this comment

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

このようにAuthに関連するキーなどはgitにコミットしないようにしましょう
環境変数にして、環境変数ファイルごとgitignoreすることをおすすめします

"#{first_user.id}-#{second_user.id}"
else
"#{second_user.id}-#{first_user.id}"
end
Copy link

Choose a reason for hiding this comment

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

[nits]
ワンライナーで書いた方がrubyっぽいです。


def show
@messages = Message.all
@user=User.find_by(id: params[:id])
Copy link

Choose a reason for hiding this comment

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

上のfindと同じです。

@messages = Message.where(room_id: @room_id)
end

def message_room_id(first_user, second_user)
Copy link

Choose a reason for hiding this comment

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

app/channels/room_channels.rbでも同じメソッドがあるので、dryにしましょう!

has_many :messages,dependent: :destroy

# Validations
validates :email, presence: true
Copy link

Choose a reason for hiding this comment

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

たぶんdeviseの認証かと思いますが、validationがザルなのでちゃんとしてあげた方が良いですね!


# Validations
validates :email, presence: true
validates :username, presence: true
Copy link

Choose a reason for hiding this comment

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

usersテーブルのプロパティなので、nameでいいかな、とおもいます!

Copy link

Choose a reason for hiding this comment

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

あと、ここもlengthくらいは入れた方がいいと思います!
厳密には文字コードの問題もあるので、ハイフンとかチルダもいい感じにするカスタムバリデーションを定義すると良いです!


devise_for :user
get 'rooms/show'=> 'rooms#show'
get 'rooms/:id'=> 'rooms#show'
Copy link

Choose a reason for hiding this comment

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

resource :room, only: %w(show)
の方がRails!って感じです。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants