Skip to content

Code review#14

Open
philsof wants to merge 31 commits intocode-reviewfrom
master
Open

Code review#14
philsof wants to merge 31 commits intocode-reviewfrom
master

Conversation

@philsof
Copy link

@philsof philsof commented Apr 6, 2017

DO NOT MERGE

La-Keisha Towner and others added 30 commits April 5, 2017 09:54
user controller & show page, fixed post association
made like and friends controller
changed some controller methods on likes
Merge branch 'master' into user
added links to nav and user login works
Copy link
Author

@philsof philsof left a comment

Choose a reason for hiding this comment

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

@jordanyryan @mercurom1 @LM-Towner See inline notes for tips and comments


def update
@post = Post.find(params[:id])
if @post.update(post_params)
Copy link
Author

Choose a reason for hiding this comment

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

Heads up: because of how you set up your post_params method, these two lines actually allow the logged in user to become the owner of any post in your database, including posts the logged in user did not create. You need to protect the update before calling it:

@post = Post.find(params[:id])
if @post.user == current_user
  # update goes in here
  # etc

private

def post_params
params.require(:post).permit(:content, :user_id => current_user.id)
Copy link
Author

Choose a reason for hiding this comment

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

merge may be a better choice here (i.e. merge the additional key/value pair into the hash that permit returns). But it would be even better if you did this merge inside your create action, since that is the only action in which you will want this merge to happen. (See note above in update action.)


def destroy
@post = Post.find(params[:id])
@post.destroy
Copy link
Author

Choose a reason for hiding this comment

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

Protect the destroy! This is very simple:

@post = Post.find(params[:id])
if @post.user == current_user
  @post.destroy
  #etc.

def show
if current_user
@user = User.find_by(id: params[:id])
else
Copy link
Author

Choose a reason for hiding this comment

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

Watch indentation

@@ -0,0 +1,35 @@
class Friend < ApplicationRecord
Copy link
Author

Choose a reason for hiding this comment

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

The name of this class is throwing me off. This model is not of a friend, but rather of a friendship. The friend is what you call friend_user. Be sure to be clear in your naming.

end

def find_friend
User.find(self.friend_user_id)
Copy link
Author

@philsof philsof Apr 6, 2017

Choose a reason for hiding this comment

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

Let's not call User inside a Friend method. Instead, utilize your Friend associations to get to the desired User.


<% @user.user_friends.each do |friend| %>
<div class="friends-list">
<%= friend.find_friend.name %>
Copy link
Author

@philsof philsof Apr 6, 2017

Choose a reason for hiding this comment

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

This is super confusing because of the naming

def change
create_table :posts do |t|
t.text :content, null:false
t.references :user, null: false
Copy link
Author

Choose a reason for hiding this comment

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

FYI general best practice for foreign keys:

t.references :user, null: false, foreign_key: true
 # this note applies to all of your foreign keys

Copy link
Author

Choose a reason for hiding this comment

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

Explanation here

def change
create_table :friends do |t|
t.references :user, null: false, foreign_key: true
t.references :friend_user, null: false, index: true
Copy link
Author

Choose a reason for hiding this comment

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

FYI references automatically creates an index for each of these fields

def change
create_table :friend_requests do |t|
t.references :user, null: false, index: true
t.integer :friend_id, null: false, index: true
Copy link
Author

Choose a reason for hiding this comment

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

Is this referencing friends or another table?

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.

4 participants